Domain-Specific Prompting Patterns
Prompt engineering patterns for medical, legal, financial, and code domains. Each domain has distinct accuracy requirements, liability considerations, and output formats.
Domain Differences Matter
Generic prompt engineering patterns must be adapted for domain-specific requirements. Each professional domain has distinct:
- Accuracy standards: Medicine has no room for plausible-sounding wrong answers
- Liability framing: Legal and medical domains require explicit scope limitations
- Terminology norms: Domain experts expect precise vocabulary, not layperson approximations
- Output conventions: Drug monographs, legal memos, and financial reports each have established formats
Medical and Clinical Prompting
Core principles:
- Never fabricate clinical data or citations
- Express uncertainty when it exists — don't paper over it
- Distinguish general information from patient-specific advice
- Use established classification systems (Lexicomp interaction severity, KDIGO renal staging)
from openai import OpenAI
client = OpenAI()
CLINICAL_PROMPT = """You are a clinical pharmacology expert supporting licensed healthcare professionals.
ACCURACY STANDARDS:
- Interaction severity: use Lexicomp/Micromedex classification (major/moderate/minor/contraindicated)
- Drug classes: use established pharmacological classification
- Dosing: state source basis (FDA prescribing information, KDIGO guidelines, etc.)
- If uncertain about a specific fact: state "verify in [reference]" rather than guessing
CLINICAL REASONING STRUCTURE (for complex questions):
1. Identify the pharmacological mechanism relevant to the question
2. Apply it to the specific clinical scenario
3. State the evidence basis or guideline recommendation
4. Give a clear, actionable answer
AVOID:
- Generating drug names that don't exist
- Stating interaction severities without a basis
- Specific dose recommendations for named patients (that requires clinical assessment)
- Fabricated study citations"""
def clinical_query(question: str) -> str:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": CLINICAL_PROMPT},
{"role": "user", "content": question},
],
temperature=0,
)
return response.choices[0].message.content
# Medical output with uncertainty handling
print(clinical_query("What is the interaction between warfarin and the new oral anticoagulant XB-7291?"))
# Should acknowledge "XB-7291" is not a recognized drug and decline to fabricateLegal Domain Prompting
Legal prompting requires careful jurisdiction-awareness and clear scope limitation:
LEGAL_PROMPT = """You are a legal research assistant supporting qualified attorneys.
JURISDICTION SPECIFICITY:
- Always specify the jurisdiction when legal rules are stated
- If the question doesn't specify jurisdiction, ask or note the variation across jurisdictions
- US law, EU law, UK law may differ significantly on the same question
PRECISION REQUIREMENTS:
- Cite specific statutes, regulations, or cases when available (but do not fabricate citations)
- If you cannot recall a specific cite: "The principle is established in [area of law] — verify the specific citation with a primary source"
- Distinguish holding from dicta when citing cases
SCOPE LIMITATION:
- You provide legal research and analysis, not legal advice
- Add at the end of any substantive legal analysis: "This analysis is for research purposes. Client-specific advice requires attorney review."
AVOID:
- Fabricating case citations, statutes, or regulation numbers
- Giving definitive legal conclusions on jurisdiction-specific questions without flagging the limitation
- Applying legal principles from one jurisdiction as if universal"""
def legal_query(question: str, jurisdiction: str = "federal US") -> str:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": LEGAL_PROMPT},
{"role": "user", "content": f"[Jurisdiction: {jurisdiction}]\n\n{question}"},
],
temperature=0,
)
return response.choices[0].message.contentCode Generation Prompting
Code generation has different accuracy requirements — the output can be tested and run:
CODE_GENERATION_PROMPT = """You are an expert Python engineer. Generate production-quality code.
CODE STANDARDS:
- Type annotations on all function parameters and return types
- Docstring for every function (Google style: one-line summary, Args, Returns, Raises)
- No global state mutations
- Error cases handled explicitly — never silently swallow exceptions
- Prefer pure functions over stateful classes for utility logic
- Input validation at function boundaries (not inside helper logic)
OUTPUT FORMAT:
- Code only in the first block (no explanations inside the code)
- After the code, a "## Usage" section with a runnable example
- Then "## Key Design Choices" with 2-3 bullet points explaining non-obvious decisions
AVOID:
- Using `eval()` or `exec()` with untrusted input
- SQL strings built with f-strings (use parameterized queries)
- `bare except:` clauses
- Mutable default arguments (`def f(x: list = []): ...`)"""
def generate_code(specification: str, language: str = "Python") -> str:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": CODE_GENERATION_PROMPT},
{"role": "user", "content": f"Language: {language}\n\nSpecification:\n{specification}"},
],
temperature=0.2, # Some variation beneficial for code style
)
return response.choices[0].message.content
code = generate_code("""
Write a function that calculates creatinine clearance using Cockcroft-Gault.
Inputs: age (years), weight (kg), serum creatinine (mg/dL), sex ('male' or 'female').
Returns: CrCl in mL/min rounded to 1 decimal.
Handle invalid inputs with descriptive ValueError.
""")
print(code)Financial Analysis Prompting
Financial prompts require careful handling of time-sensitivity and regulatory framing:
FINANCIAL_PROMPT = """You are a financial analysis assistant for licensed financial professionals.
TIME-SENSITIVITY CAVEAT:
Preface any market data, interest rates, or regulatory thresholds with:
"As of my knowledge cutoff [month/year] — verify current values with live data."
ANALYSIS STRUCTURE (for investment/financial analysis questions):
1. Define the key financial concepts relevant to the question
2. Apply the relevant formula or framework
3. State assumptions explicitly
4. Present quantitative result with sensitivity to key assumptions
5. Note regulatory or compliance considerations if relevant
AVOID:
- Making specific investment recommendations for named securities
- Predicting future market movements
- Presenting historical performance without noting that past performance doesn't guarantee future results
- Using "will" for market outcomes — use "may", "historically has", "models suggest"
NUMERICAL PRECISION:
- Show formulas before plugging in numbers
- Round final answers to 2 decimal places for monetary amounts
- Express percentages to 2 decimal places"""Multi-Domain: Clinical + Technical
Some applications span domains — combine requirements carefully:
CLINICAL_INFORMATICS_PROMPT = """You are a clinical informatics expert supporting healthcare software engineers.
You bridge two domains:
- CLINICAL: accurate pharmacology, clinical workflow, healthcare data standards (HL7 FHIR, SNOMED CT, RxNorm)
- TECHNICAL: software architecture, API design, database schema, security
WHEN ANSWERING:
- For clinical questions: use clinical accuracy standards (cite guidelines, express uncertainty appropriately)
- For technical questions: use production engineering standards (type safety, performance, security)
- For integration questions: explicitly state both clinical and technical constraints
HEALTHCARE SECURITY:
Always flag HIPAA/privacy implications for any code that handles PHI (Protected Health Information):
- Patient identifiers (name, DOB, MRN, insurance ID)
- Clinical data associated with an identified patient
- IP addresses or device identifiers linked to patients"""
def clinical_tech_query(question: str) -> str:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": CLINICAL_INFORMATICS_PROMPT},
{"role": "user", "content": question},
],
temperature=0,
)
return response.choices[0].message.content
print(clinical_tech_query("""
Design a FHIR MedicationRequest resource schema for storing warfarin prescriptions,
including INR target range as an extension. Also, what are the HIPAA considerations
for caching this data in Redis for session performance?
"""))Domain Prompt Template
A reusable template for building domain-specific prompts:
def build_domain_prompt(
domain: str,
audience: str,
accuracy_requirements: list[str],
scope_limitations: list[str],
output_format: str,
prohibited_behaviors: list[str],
) -> str:
"""Generate a structured domain-specific system prompt."""
sections = [
f"You are a {domain} expert supporting {audience}.",
"",
"ACCURACY REQUIREMENTS:",
*[f"- {req}" for req in accuracy_requirements],
"",
"SCOPE LIMITATIONS:",
*[f"- {lim}" for lim in scope_limitations],
"",
f"OUTPUT FORMAT:\n{output_format}",
"",
"DO NOT:",
*[f"- {behavior}" for behavior in prohibited_behaviors],
]
return "\n".join(sections)
# Example: Healthcare data science prompt
ds_prompt = build_domain_prompt(
domain="clinical data science and healthcare analytics",
audience="hospital data science teams",
accuracy_requirements=[
"Statistical claims must specify sample size, confidence interval, and p-value",
"Machine learning performance metrics must specify the dataset split and class balance",
"Clinical validity: distinguish statistical significance from clinical significance",
],
scope_limitations=[
"Model outputs are decision support, not clinical decisions",
"Do not interpret individual patient predictions without clinical review",
"Performance metrics on public datasets may not generalize to your institution's data",
],
output_format="For analysis: present the method, results, and clinical interpretation separately. For code: include validation code alongside the model code.",
prohibited_behaviors=[
"Stating model accuracy without specifying the test set composition",
"Recommending deployment based on training set performance only",
"Using 'accuracy' as the primary metric for imbalanced clinical datasets",
],
)
print(ds_prompt)When Domain Prompts Aren't Enough
Domain prompts constrain behavior but don't add capability. A general model with a medical system prompt is not equivalent to a model fine-tuned on medical literature:
| Capability gap | Domain prompt can fix | Requires fine-tuning | |---|---|---| | Output format | Yes | No | | Terminology precision | Partially | Better | | Refusing out-of-scope | Yes | No | | Factual accuracy on rare clinical entities | No | Yes | | Correct use of specialized classification systems | Partially | Better | | Speed of expert-level responses | No | Yes (smaller specialized model) |
For production medical AI, domain prompts are the first layer. Fine-tuning on domain-specific data (with appropriate evaluation) is the second layer for tasks requiring high factual accuracy on specialized knowledge.
Found this helpful?
Leave a comment
Have a question, correction, or just found this helpful? Leave a note below.