Lesson 1.2 · 8 min read · about 55 min to master at a very slow pace
Prompts, Roles and Structured Output
How a prompt is structured into system and user messages, how few-shot examples steer behaviour, and how to get reliable JSON out of a model with a schema and validation.
Messages and roles
Chat APIs take a list of messages, each with a role:
- System: who the model is, the rules, the output format. Written by you, the developer. Stable across requests.
- User: the end user's input, plus any documents you insert for this request.
- Assistant: the model's earlier replies (resent for multi-turn chat).
A prompt that works in production
Good prompts are specific and boring. A reliable pattern:
- Role and goal: "You classify support emails for an online bike shop."
- Rules: the categories, what to do when unsure, what never to do.
- Examples (few-shot): two to five real inputs with the exact output you want, including one tricky case.
- Output format: a schema, not a description.
Worked example: a ticket classifier
SYSTEM:
You classify customer emails for a bike shop.
Categories: order_status, return, product_question, complaint, other.
If the email fits two categories, choose the one the customer
needs answered first. If unsure, use "other".
Reply only with JSON matching the schema.
USER:
Email: "My order #4471 still hasn't arrived and the
tracking link is broken. Also, do you sell kids' helmets?"
Expected: {"category": "order_status", "order_id": "4471", "urgent": false}. The rule about two categories handles the ambiguity before it happens.
Structured output
Downstream code needs fields, not prose. Ask for JSON and give a schema. Most current APIs support a structured output or JSON schema mode that constrains the model to produce valid JSON matching the schema. Then:
- Validate every response in code (for example with Pydantic in Python or Zod in TypeScript).
- On failure, retry once with the validation error in the prompt.
- If it still fails, fall back: route to a human or return a safe default. Never pass unvalidated output on.
Valid JSON is not correct JSON
A schema guarantees the shape: the right keys and types. It does not guarantee the values are right. An order ID can be well-formed and invented. Values are checked with evals and, where possible, against your own data (does order 4471 exist?).
Stuck? Common doubts
Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.
First passShould the instructions go in the system or the user message?
Put stable rules, persona and format in the system message; put the per-request input and documents in the user message. This keeps the system prompt identical across calls, which also makes it cacheable (Section 6) and easier to version and test.
Going deeperHow many few-shot examples should I use?
Usually two to five. More examples cost tokens on every call and can make the model copy surface details. Pick examples that cover the edge cases you actually see, and check with an eval set whether adding one helps; sometimes a clearer rule beats another example.
Cheatsheet
Roles
Structured output recipe
Schema ≠ truth
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.