Structured Prompts
Structured prompts organize instructions, context, constraints, examples, and output requirements so an LLM knows exactly what the task is and how the response should be formatted.
By the end of this lesson, you will be able to:
- Explain why structure matters in prompt engineering.
- Separate instructions, context, constraints, and output requirements.
- Design prompts for predictable text and JSON responses.
- Build structured prompts dynamically with Python.
- Evaluate and improve prompts instead of relying on one successful response.
What Is a Structured Prompt?
A structured prompt is a prompt whose parts are deliberately organized. Instead of putting every requirement into one paragraph, you give the model recognizable sections.
Everything mixed together
Write a product description for a laptop. Keep it short, mention the battery and display, make it suitable for a beginner and don't make claims that are not provided.
The requirements are present, but they are harder to inspect and maintain.
Clear sections
ROLE: Product copywriter TASK: Write a short laptop description. FACTS: - 14-inch display - 10-hour battery CONSTRAINTS: - Beginner-friendly - Do not invent specifications OUTPUT: One paragraph, 80 words maximum.
Each requirement has a clear purpose and can be changed independently.
The Anatomy of a Good Structured Prompt
There is no single mandatory template. Choose the sections that are useful for your task.
Tell the model what action to perform.
Provide the information needed for the task.
Define limits, rules, or things the model must avoid.
Specify exactly how the answer should be returned.
Write Clear Instructions
A strong instruction tells the model what to do, what information to use, and what a successful answer looks like. Avoid instructions that leave important decisions undefined.
Too vague
Analyze this customer review.
Analyze what? Sentiment, topic, urgency, or product quality?
Specific task
Analyze the customer review. Identify: 1. Sentiment 2. Main topic 3. Urgency Use only the information in the review.
The task has explicit fields and a clear boundary.
Separate Context From Instructions
Context is information the model should use. Instructions describe what the model should do with that information. Keeping them separate makes prompts easier to maintain.
This separation becomes especially useful when the same instruction is reused with hundreds or thousands of different user inputs.
Add Constraints and Boundaries
Constraints reduce ambiguity. They can define length, allowed values, tone, source boundaries, prohibited behavior, or validation requirements.
CONSTRAINTS: - Use only the information in the customer message. - Do not invent an order number. - Priority must be HIGH, MEDIUM, or LOW. - Explanation must be one sentence. - Return valid JSON only.
Control the Output Format
One of the biggest advantages of structured prompts is predictable output. If another program will consume the response, define the required fields and format explicitly.
Harder to consume
The customer is unhappy with the delivery. Priority is high and the next step is to contact the carrier.
Easier for software
{
"sentiment": "negative",
"priority": "HIGH",
"category": "DELIVERY",
"next_action": "contact_carrier"
}
Python Example: Build a Structured Prompt
Instead of constructing a large prompt as one confusing string, keep the sections visible in Python.
from openai import OpenAI
client = OpenAI()
customer_message = """
My package arrived two days late and the box was damaged.
"""
prompt = f"""
ROLE:
You are a customer support classifier.
TASK:
Classify the customer's issue.
CATEGORIES:
- DELIVERY
- PRODUCT
- BILLING
- ACCOUNT
CONSTRAINTS:
- Use only the customer's message.
- Choose exactly one category.
- Priority must be HIGH, MEDIUM, or LOW.
- Do not invent facts.
OUTPUT:
Return valid JSON with:
category, priority, explanation
CUSTOMER MESSAGE:
{customer_message}
"""
response = client.responses.create(
model="gpt-5.6",
input=prompt
)
print(response.output_text)
Create Reusable Prompt Templates
In real applications, most prompts are templates. The structure stays stable while user data changes.
def build_classification_prompt(message: str) -> str:
return f"""
TASK:
Classify the customer message.
CATEGORIES:
- DELIVERY
- PRODUCT
- BILLING
- ACCOUNT
RULES:
- Select exactly one category.
- Return a priority from HIGH, MEDIUM, LOW.
- Use only the supplied message.
OUTPUT:
Return JSON:
{{
"category": "...",
"priority": "...",
"explanation": "..."
}}
MESSAGE:
{message}
"""
prompt = build_classification_prompt(
"I was charged twice for the same order."
)
print(prompt)
The benefit is maintainability: you can update the instruction, rules, or output contract in one place instead of rewriting every request.
Structured Does Not Mean Extremely Long
Adding more words does not automatically improve a prompt. The goal is useful structure, not unnecessary complexity.
Sections directly connected to the task.
Many repeated instructions saying the same thing.
A rule that reduces a real ambiguity.
A rule that adds words without improving the result.
Evaluate Your Structured Prompt
Do not judge a prompt from one successful response. Test it against a representative set of inputs.
Real-World Applications
Classify tickets and return predictable fields for workflows.
Extract names, dates, amounts, and categories into a defined structure.
Control audience, tone, length, facts, and formatting.
Separate retrieved context from instructions and require grounded answers.
ROLE: You are an invoice data extraction assistant. TASK: Extract the invoice information from the supplied text. FIELDS: - invoice_number - invoice_date - vendor - total_amount - currency RULES: - Do not guess missing values. - Use null when a field is not present. - Preserve the original invoice number. - Return JSON only.
Design a Structured Prompt
Build a prompt for an AI assistant that reads a customer message and returns a support classification.
- Define the task.
- Add the allowed categories.
- Add two or three useful constraints.
- Define a JSON output format.
- Test the prompt with at least five different messages.
null rather than inventing a value.
Test Your Understanding
It makes the prompt easier to understand, maintain, reuse, and modify.
It makes the expected response shape explicit and easier for software to consume.
No. Useful structure matters more than unnecessary prompt length.
Yes. Prompt instructions are not a substitute for application-level validation.
Structure the prompt so the model knows what to do and what to return.
Key Takeaways
- Structured prompts separate the important parts of a task.
- Clear instructions reduce ambiguity.
- Context should be distinct from the action the model must perform.
- Constraints help control boundaries and reduce unwanted behavior.
- Explicit output formats make AI responses easier to integrate into software.
- Prompt quality should be evaluated across representative inputs.