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.

Not startedJump to quiz

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:

  1. Role and goal: "You classify support emails for an online bike shop."
  2. Rules: the categories, what to do when unsure, what never to do.
  3. Examples (few-shot): two to five real inputs with the exact output you want, including one tricky case.
  4. 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:

  1. Validate every response in code (for example with Pydantic in Python or Zod in TypeScript).
  2. On failure, retry once with the validation error in the prompt.
  3. 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

System: developer rules and format (stable). User: the request and documents. Assistant: earlier model replies.

Structured output recipe

Schema in the request → validate in code → retry once with the error → fall back to a human or safe default.

Schema ≠ truth

A JSON schema guarantees shape, not correct values. Check values against your data and with evals.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1conceptWhich content belongs in the system message?
Q2diagnoseYour classifier returns valid JSON every time, but 6% of the order IDs it extracts don't exist in the client's database. What is going on?
Q3applyA response fails your Pydantic validation. What should the code do first?
Q4conceptWhat are few-shot examples in a prompt?
Q5applyAn email asks both about a late order and about a product. Your prompt has no rule for this and results flip between categories. What is the best fix?