Structured Output
Constrain the model's output to conform to a JSON Schema (or similar typed shape).
Problem
When the model is asked to emit JSON via natural-language instructions alone, the output is close but not quite right in inventive ways: smart quotes instead of straight ones, a stray sentence of explanation before the opening brace, a trailing comma, an extra field the schema does not allow. Strict parsers reject this; permissive parsers smuggle bugs forward. Writing post-hoc fixers turns into a tar pit of regular expressions chasing each new failure mode, and the application picks up a class of "flaky model" bugs that are really shape bugs the team has no clean way to prevent at decode time.
Solution
Define a JSON Schema (or Pydantic / Zod / equivalent). Pass it to the model via the provider's structured-output mode. Validate the output. Reject and retry on validation failure. Cap retries.
When to use
- Downstream code consumes typed data and free-form text would break parsers.
- A JSON Schema or equivalent typed shape can be defined for the output.
- The provider supports structured-output mode or function calling.
Open the full interactive page →
Diagram, neighbourhood map, code examples, related patterns and full provenance.
Related
- Tool Use
- Frozen Rubric Reflection
- Deterministic-LLM Sandwich
- Schema-Free Output
- Plan-and-Execute
- DSPy Signatures
- Input/Output Guardrails
- Streaming Typed Events
- Hallucinated Tools
- Tool Output Trusted Verbatim
- SOP-Encoded Multi-Agent Workflow
- Mobile UI Agent
- Dual-System GUI Agent
- Code-as-Action Agent
- Multilingual Voice Agent Stack
- Code-Switching-Aware Agent
- Prompt/Response Optimiser
- Citation Attribution
- Deterministic Control Flow, Not Prompt
- Context Minimization
- LLM Map-Reduce Isolation
- Missing max_tokens Cap
- Performative Message