Structured Output
Constraining LLM output to a defined schema (JSON) for downstream reliability.
Step 1 of 4
Define schema
Write {summary: str, confidence: float, sources: list[str]} once, for example as a Pydantic or Zod model.
Treat the schema as the single source of truth and still validate what comes back.
Simple explanation
Instead of free text, you tell the LLM to output JSON matching a schema. This lets your code parse the response reliably. 'Return {summary, confidence, sources} as JSON.'
Engineering explanation
Structured output uses a JSON schema the API enforces (OpenAI's Structured Outputs: response_format type 'json_schema', or text_format with a Pydantic model in the SDK) or tool calling. The older JSON mode (type 'json_object') only guarantees valid JSON, not your schema. Validate with Pydantic/Zod and handle refusals, which do not follow the schema (OpenAI's Structured Outputs guide, checked 28 October 2026). Production: schema is the single source of truth (used for both model definition and validation), repair function for common malformations.
Example
Schema: {summary: str, confidence: float, sources: list[str]}. Model returns valid JSON. Your code parses without try/catch on format.
When to use it
Any LLM output that downstream code needs to parse. Any RAG, extraction, classification or agent system.
Common misconception
Structured output is not guaranteed in every setup. Without schema-enforced decoding, models can return malformed JSON, and refusals or truncated outputs do not follow the schema. Always validate + retry.