Structured output

Free text between components is where agent systems rot. Structured output turns a model into a component you can validate, and the whole discipline is keeping the schema small enough that the model gets it right.

How it works

  1. Define the smallest schema that carries what the next step needs.
  2. Ask for it explicitly, and use the provider's structured output mode where one exists.
  3. Validate against the schema in code, always. Never trust the shape.
  4. On failure, repair once with the validation error included in the prompt.
  5. On a second failure, fall back to a deterministic path rather than looping.

Optional: load a real model

See it work

When it pays, and when it does not

Use it whenSkip it when
Output feeds another programA human reads the output directly
You need to branch on the resultThe result is prose by nature, where a schema fights the task
Fields are few and flatThe schema has thirty nested fields, which small models will not fill correctly
The provider supports structured modeYou are on a tiny local model, where a two field schema is the practical limit

How it fails

Schema too large

Every extra field lowers the chance all of them are right. Split into two calls before you add a fifteenth field.

Trusting the shape

Valid JSON with a number where a string belongs still crashes the next step. Validate types, not just parseability.

Repair loops

Retrying validation failures forever turns a formatting problem into a cost incident. One repair, then fall back.

What it costs

TokensSlightly higher, since schemas add prompt length.
ReliabilitySubstantially better, which is why this is standard practice now.
FlexibilityLower by design, which is the point of a component boundary.
Small schemas, hard validation, one repair. That sequence removes most of the flakiness people blame on the model.

Related: Tool calling · Verification · Testing agents · all patterns · agent jobs

Free from AI School - no signup, everything runs in your browser.