LESSON 24 ยท PROMPT ENGINEERING

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.

27 min read โ€ข Beginner โ€ข Generative AI

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.
1

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.

UNSTRUCTURED

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.

STRUCTURED

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.

Core idea: Structure makes a prompt easier for both the model and the developer to understand, test, maintain, and reuse.
2

The Anatomy of a Good Structured Prompt

There is no single mandatory template. Choose the sections that are useful for your task.

ROLE Perspective Support agent
+
TASK Action Classify ticket
+
CONTEXT Useful data Customer message
+
CONSTRAINTS Boundaries Allowed labels
โ†’
OUTPUT Format JSON
Instructions

Tell the model what action to perform.

Context

Provide the information needed for the task.

Constraints

Define limits, rules, or things the model must avoid.

Output format

Specify exactly how the answer should be returned.

3

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.

WEAK

Too vague

Analyze this customer review.

Analyze what? Sentiment, topic, urgency, or product quality?

CLEAR

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.

Practical rule: If another developer cannot tell what the prompt is asking for by reading it once, make the task more explicit.
4

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.

Example: Support Ticket Prompt sections
INSTRUCTIONSClassify the ticket POLICYAllowed categories INPUTCustomer message OUTPUTJSON object

This separation becomes especially useful when the same instruction is reused with hundreds or thousands of different user inputs.

5

Add Constraints and Boundaries

Constraints reduce ambiguity. They can define length, allowed values, tone, source boundaries, prohibited behavior, or validation requirements.

LengthMaximum 100 words.
Allowed valuesUse only HIGH, MEDIUM, LOW.
Source boundaryUse only the supplied document.
StyleUse clear beginner-friendly language.
Prompt Structured constraints
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.
6

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.

FREE-FORM

Harder to consume

The customer is unhappy with
the delivery. Priority is high
and the next step is to contact
the carrier.
STRUCTURED JSON

Easier for software

{
  "sentiment": "negative",
  "priority": "HIGH",
  "category": "DELIVERY",
  "next_action": "contact_carrier"
}
Important: Prompting for JSON does not by itself guarantee valid JSON. When your API or SDK supports structured outputs or schemas, prefer those mechanisms for production systems.
7

Python Example: Build a Structured Prompt

Instead of constructing a large prompt as one confusing string, keep the sections visible in Python.

Python Structured prompt
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)
8

Create Reusable Prompt Templates

In real applications, most prompts are templates. The structure stays stable while user data changes.

Python Reusable function
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.

9

Structured Does Not Mean Extremely Long

Adding more words does not automatically improve a prompt. The goal is useful structure, not unnecessary complexity.

Too vague Clear task Useful structure Extra complexity Over-engineered
Good structure

Sections directly connected to the task.

Bad structure

Many repeated instructions saying the same thing.

Good constraint

A rule that reduces a real ambiguity.

Bad constraint

A rule that adds words without improving the result.

10

Evaluate Your Structured Prompt

Do not judge a prompt from one successful response. Test it against a representative set of inputs.

CorrectnessDid it produce the right result?
FormatDid it follow the output contract?
ConsistencyDoes it work across different inputs?
EfficiencyCan unnecessary prompt text be removed?
Prompt engineering mindset: Design โ†’ test โ†’ measure โ†’ improve. A prompt is part of an application, not a magic sentence.
11

Real-World Applications

Customer support

Classify tickets and return predictable fields for workflows.

Data extraction

Extract names, dates, amounts, and categories into a defined structure.

Content generation

Control audience, tone, length, facts, and formatting.

RAG systems

Separate retrieved context from instructions and require grounded answers.

Example Document extraction
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.
HANDS-ON PRACTICE

Design a Structured Prompt

Build a prompt for an AI assistant that reads a customer message and returns a support classification.

  1. Define the task.
  2. Add the allowed categories.
  3. Add two or three useful constraints.
  4. Define a JSON output format.
  5. Test the prompt with at least five different messages.
Challenge: Add a rule for missing information. The model should return null rather than inventing a value.
QUICK CHECK

Test Your Understanding

1. Why separate context from instructions?

It makes the prompt easier to understand, maintain, reuse, and modify.

2. What is the benefit of specifying an output schema?

It makes the expected response shape explicit and easier for software to consume.

3. Does a longer prompt always produce a better answer?

No. Useful structure matters more than unnecessary prompt length.

4. Should important production outputs be validated?

Yes. Prompt instructions are not a substitute for application-level validation.

30-SECOND RECAP

Structure the prompt so the model knows what to do and what to return.

InstructionWhat should happen?
ContextWhat information is available?
ConstraintsWhat rules must be followed?
OutputWhat should the response look like?

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.