What are structured outputs in LLMs?
Structured outputs are model replies that are guaranteed to follow a fixed format, such as JSON that matches a given schema. The usual method is constrained decoding: at each step, any token that would break the format gets its logit set to −∞ before sampling. The schema compiles to a state machine, and the machine decides which tokens are legal next.
Programs that call a language model usually need data, not prose. Extracting fields from a document, filling a form, classifying a ticket, and calling a tool or function all expect a reply that a parser can read on the first try. Constrained decoding removes the parse-and-retry loop, keeps field types correct, and lets small local models produce output that downstream code can trust.
Key ideas
- Sampling freely from a language model gives valid JSON most of the time, but never every time.
- Constrained decoding sets the logit of every format-breaking token to −∞, so those tokens get zero probability.
- A regex or simple JSON schema compiles to a finite-state machine that lists the legal next characters in each state.
- Tokens often span several grammar symbols, so the allowed tokens per state are found by walking each token through the machine, once, ahead of time.
- The mask costs almost nothing per step, but the field order in a schema can change answer quality.
- JSON mode guarantees valid syntax only; schema-constrained decoding also guarantees the fields and types.
Common questions
Does constrained decoding guarantee correct answers?
No. It guarantees the shape of the output, not the truth of its values. A field typed as an integer will hold an integer, but it can still be the wrong integer. Validation of the content, such as range checks or cross-checks against a source, is still needed.
What is the difference between JSON mode and structured outputs?
JSON mode constrains the reply to be syntactically valid JSON, with any keys and value types. Structured outputs constrain it to one specific schema: the required keys, their types, and allowed enum values. Code that uses JSON mode still has to check the schema itself.
Does constrained decoding slow down generation?
Per token the overhead is small once the index or grammar is compiled, since the mask is a lookup plus an operation over the vocabulary. Compiling a large or complex schema can take noticeable time on the first request, which is why libraries cache compiled grammars.
Can constrained decoding work with any model?
It works with any model that exposes its next-token logits, because it only changes which tokens may be sampled. That includes local open-weight models. With a hosted model, it depends on whether the provider runs the constraint on its side.
Related explainers
- Sampling: Temperature, Top-k & Top-p — How a language model picks its next token
- Tokenization & BPE — How text becomes numbers — from characters to subword tokens via byte-pair encoding
- Softmax, Logits & Temperature — Turning raw scores into probabilities
- Decoder-Only Transformers — The architecture behind GPT and modern LLMs
- Retrieval-Augmented Generation — Ground language model outputs in retrieved documents to reduce hallucination
- KV Cache — Why LLM generation caches keys and values, and what it costs
References
- Willard and Louf (2023), "Efficient Guided Generation for Large Language Models"
- Dong et al. (2024), "XGrammar: Flexible and Efficient Structured Generation Engine for Large Language Models"
- Tam et al. (2024), "Let Me Speak Freely? A Study on the Impact of Format Restrictions on Performance of Large Language Models"
Last updated