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
- Define the smallest schema that carries what the next step needs.
- Ask for it explicitly, and use the provider's structured output mode where one exists.
- Validate against the schema in code, always. Never trust the shape.
- On failure, repair once with the validation error included in the prompt.
- 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 when | Skip it when |
|---|---|
| Output feeds another program | A human reads the output directly |
| You need to branch on the result | The result is prose by nature, where a schema fights the task |
| Fields are few and flat | The schema has thirty nested fields, which small models will not fill correctly |
| The provider supports structured mode | You 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
| Tokens | Slightly higher, since schemas add prompt length. |
|---|---|
| Reliability | Substantially better, which is why this is standard practice now. |
| Flexibility | Lower 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
Related: Tool calling · Verification · Testing agents · all patterns · agent jobs
Free from AI School - no signup, everything runs in your browser.