Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Four beats: schema in, parse, check, retry. If the retry also fails, fall back to a person.

Processing one emailStep 1 of 4
  1. Schema in

    The request includes the email plus the field definitions and allowed values.

The trap is trusting output because it parsed. Valid JSON with a made-up order number is still wrong, which is why the schema must permit null. Retries also cost an extra call, so log how often they happen; a rising retry rate is an early warning that something has drifted.

Drills

Answer fast — trust the rule.

quick drill

Why limit category to a fixed list of values?

quick drill

An email mentions no order. What should the schema allow for order_id?

quick drill

The model returns valid JSON but urgency is "super urgent". What happens next in the four beats?

quick drill

The API enforces your JSON schema. Do you still need your own checks?

Remember

  • If code will read it, ask for a schema, then validate.
  • Schema in, parse, check, retry, then a person.
  • Allow null; valid JSON is not the same as true JSON.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA schema for incoming tickets

Pedal Point wants every incoming email turned into a record with a category, an order number (if any) and an urgency. Design the fields, say which ones should be limited to fixed values and why, and describe what the app does when the model's output fails validation.