Structured Output
A technique using constrained decoding to guarantee that an LLM's output strictly conforms to a predefined JSON Schema or formal grammar for reliable software integration
Table of Contents
Structure Diagram
What is Structured Output?
A technique using constrained decoding to guarantee that an LLM's output strictly conforms to a predefined JSON Schema or formal grammar for reliable software integration. It is a core concept in LLM Fundamentals within AI / ML.
Core Principles
Structured Output goes beyond asking an LLM to "reply in JSON." Under the hood, it compiles a JSON Schema into a Finite State Machine (FSM) or grammar and applies Constrained Decoding, masking out invalid tokens (-∞ logit) at every generation step.
This guarantees syntactically valid, schema-compliant output without broken brackets, markdown wrappers, or hallucinated keys, allowing LLMs to integrate cleanly with typed backend code.
Practical Takeaways
- Place a
reasoningfield before the final answer: Forcing an immediatelabeldegrades accuracy on complex judgments. Put a briefreasoningstring first in the schema so the model can think before committing. - Use
enumand nullable fields deliberately: Allownullfor missing information so the model does not fabricate values just to satisfy required keys. - Keep schemas flat and concise: Deeply nested unions and complex regex patterns increase grammar compilation latency and hurt model adherence.
Notable Examples
- Invoice & Contract Extraction: Parse unstructured PDFs into strongly typed Pydantic/Zod objects ready for database insertion.
- Support Ticket Triage: Classify incoming messages into
{ intent, priority, team, summary }JSON payloads. - Generative UI: Return validated widget specs that frontend components render deterministically.