Skip to content

Learning path · Models & Tokens · 14

Structured Output

Constraining model responses to machine-parseable schemas—JSON, enums, or tool-call payloads—so downstream code can act reliably.

Why it matters

  • Agents and workflows break when free-form text replaces typed interfaces.
  • Schema validation catches model mistakes before side effects.
  • Pairs with function-calling and programmatic orchestration.

Key ideas

  • JSON schema
  • Constrained decoding
  • Repair loops

Structured output turns LLM responses into API contracts. Use provider-native JSON modes or tool schemas when available; otherwise specify schemas in prompts with examples and validate with a strict parser. On failure, retry with the validation error as feedback—a small repair loop beats hoping the model self-corrects. Non-deterministic tool JSON serialization order can bust prefix caches; stable key ordering matters in optimized pipelines. Prefer schema validation at the boundary over regex scraping of markdown fences, which breaks silently when models add polite preamble.

Updated 2026-08-09 · Full learning path