Structured Output
Structured output makes an LLM return data in a guaranteed shape — JSON matching your schema — so code can consume model responses without parsing prose.
Structured output is getting typed, machine-consumable data from an LLM — the model's response constrained to match a schema you define, instead of prose your code has to parse and pray over.
It's the feature that turns models into software components. Extraction, classification, routing, agent decisions — all of it wants {"category": "billing", "priority": 2}, not three paragraphs containing that information somewhere. Providers offer escalating guarantees: prompt-and-hope, JSON mode (valid JSON, arbitrary shape), and schema-constrained generation (decoding restricted so output must match your schema) — with function calling as the closely related mechanism where the "output" is a tool invocation.
The engineering around it: design schemas the model can fill well (described fields, enums over free strings — the llm-output-schema-generator skill infers one from an example), validate semantics even when syntax is guaranteed, and wrap a validate-and-retry loop — the pattern libraries like Instructor and BAML productize. Which guarantee to use when, per provider, is the Structured Output vs JSON Mode vs Function Calling decision guide.
Frequently asked questions
- What's the difference between JSON mode and structured outputs?
- JSON mode guarantees syntactically valid JSON — but any JSON: fields can be missing, renamed, or mistyped. Structured outputs (schema-constrained generation) guarantee conformance to your specific schema, enforced during decoding. If code consumes the result, schema enforcement is the one you want.
- Do I still need validation with structured outputs?
- Yes — schema conformance isn't semantic correctness. The shape can be right while the values are wrong (a plausible-but-invented ID, a date outside your range). Validate semantics in code, and keep a retry path that feeds validation errors back to the model; libraries like Instructor package that loop.
Related
- Structured Output vs JSON Mode vs Function Calling: Which to Use in 2026The reliable ways to get typed data out of an LLM — what JSON mode, function calling, and native structured outputs each guarantee, and when to use which.
- Function Calling (Tool Calling)Function calling lets an LLM request structured invocations of your code: describe tools with schemas, the model emits typed calls, your app executes them.
- LLM Output Schema GeneratorTurn an example of the data you want from an LLM into a precise, validated output schema (Pydantic / Zod / JSON Schema) and wire it into structured-output calls. Use when adding typed LLM output, replacing brittle JSON parsing, or designing an extraction shape.
- InstructorGet structured, validated output from LLMs using plain type definitions, with automatic retries on validation failure.
- BAMLA domain-specific language for type-safe LLM functions, with generated clients and schema-aligned parsing.
- Pydantic AIThe type-safe agent framework from the Pydantic team — validated structured outputs, dependency injection, durable execution, and 'that FastAPI feeling' for agents.
- StagehandBrowserbase's open-source SDK for browser agents — act, extract, observe, and agent primitives that mix natural language with code-level control.
- GroundingGrounding ties a model's output to verifiable sources — retrieved documents, tool results, citations — instead of training-data memory.
- GuardrailsGuardrails are programmatic checks around an LLM — validating inputs and outputs in code — enforcing safety and format rules a prompt alone can't guarantee.
- TemperatureTemperature controls how random an LLM's token choices are: low values make output focused and repeatable, high values make it varied and creative.
- Top-p (Nucleus Sampling)Top-p sampling restricts an LLM's next-token choices to the smallest set whose probabilities sum to p — cutting the long tail of unlikely tokens adaptively.