Skip to the workspace
Pebellslearn by building
Working with Model APIs  ›  Lesson

Structured Output

Structured Output 12 minWhen code will read a model's answer, ask for a defined format such as JSON matching a schema, then parse and validate it before trusting it.

ai engineer / Structured Output

Explore each scene at your own pace.

01 · The rule

The rule

Structured Output

Pedal Point wants every incoming email sorted automatically: which category, which order, how urgent. The first attempt asked the model to "summarise the email", and the replies came back as friendly paragraphs. A person can read a paragraph. Code cannot route one. When software is the reader, you need structured output.

The rule

If code will read it, ask for a schema, then validate.

Tell the model the exact shape you want, usually JSON, with named fields and allowed values:

{
  "category": "order_status | returns | product_question | other",
  "order_id": "string of digits, or null",
  "urgency": "low | normal | high"
}

(In this description the | means "one of", not literal text.)

Then the email "My order 4417 still hasn't arrived and it's a birthday gift for Saturday!" becomes:

{"category": "order_status", "order_id": "4417", "urgency": "high"}

Three design habits make this reliable:

  • Fixed values where code branches. Categories and urgency come from short lists, so your routing code always matches.
  • Allow "nothing". order_id may be null. If every field is required, the model may invent a value to fill it.
  • Validate every time. Parse the JSON and check each field against the rules before using it.

Name fields plainly and describe each one in a sentence. The model reads names and descriptions as instructions, so urgency with the note "high only when a deadline or safety issue is mentioned" gives far more consistent labels than a bare field name.

Many model APIs can now enforce a JSON schema or a JSON-only mode, which makes invalid output far rarer. Validate anyway: correct shape does not guarantee correct content.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.