Guide

Downstream code does not want a paragraph. It wants a typed object: an enum category, a nullable invoice ID, a list of line items with exact amounts. If you ask a chat model for "JSON please" and call JSON.parse in a loop, you will spend your time on trailing commas, markdown fences, missing fields and renamed keys.

Structured outputs are the techniques that make a model's response checkable by a program. The goal is not prettier text. It is a contract between the model and your code.

Concepts in this section

Read them in order. Each one builds on the one before.

StepConceptWhat it answers
1Constrained decodingHow can the sampler be forced to stay valid?
2Schema designHow should the shape be written so the model fills it well?
3Validation and repairWhat happens when the object is valid but wrong, or cannot be produced?
4Field-level evalsHow do you measure the fields that move money?

The approaches, weakest to strongest

ApproachGuaranteeTypical use
Prompt-only ("respond with JSON")NonePrototypes
JSON modeValid JSON syntax, not your schemaLoose objects
Schema-constrained decodingOutput matches a JSON SchemaProduction extraction
Tool call with a strict schemaSame guarantee, framed as a tool callAgents and side effects

Prompt-only JSON fails more often than you expect. Even 1% invalid JSON at 100,000 requests a day is 1,000 broken calls. JSON mode fixes the syntax but not the keys, types or enums. Only schema-constrained decoding and strict tools fix the shape. Even then, you still validate.

Tools fit extraction well. Define a tool such as record_invoice whose parameters are the fields you want. The model "calls" it, and your code reads the arguments. Nothing runs.

Common mistakes

  • Trusting the shape means trusting the values. A valid object can still hold a wrong invoice ID.
  • Assuming every provider supports the same schema features. Check the subset before you rely on a feature.
  • Using JSON mode and skipping validation. Syntax is only the first gate.

Key takeaways

  • Structure exists so deterministic code can trust model output.
  • Climb the ladder: prompt-only JSON, then JSON mode, then schema constraints or strict tools.
  • Validate every output in code, even when the provider guarantees the shape.
  • Score the fields, not just whether the JSON parses.

Further learning

OpenAI's guide covers the API surface and which schema features work under strict mode.

OpenAI: Structured OutputsUnderstand how to ensure model responses follow specific JSON Schema you define.OpenAI Developers

Outlines is a common open-source library for structured generation on self-hosted models.

Outlines (structured generation library)Structured Outputs. Contribute to dottxt-ai/outlines development by creating an account on GitHub.GitHub

Start with the first concept page: constrained decoding.