deep · 7 min
Constrained decoding
Forcing valid output by blocking, at every step, the tokens that would break the schema.
updated 29 Sept 2026
A model is writing JSON. It has just written the opening {, and the next
thing it would naturally write is a single token made of a space and a
double quote, the start of "name".
But we've switched on a JSON constraint, and this particular engine only allows a lone quote or some whitespace at this point. The space-plus-quote token isn't on its list. So the model, picking from what's left, writes a tab. Then a lone quote. Then another tab. Each pick is legal JSON, each is stranger than what the model would write by itself, and as it drifts the content gets worse.
Problems like this decide whether a schema helps our answers or hurts them. To see why it happens, let's build the machinery one piece at a time.
The mask
The basic move was in structured output. At each step, the model scores every token in its vocabulary. Before it picks, an engine works out which tokens could still lead to valid output, and builds a mask: yes or no for every token. Blocked tokens get a score of minus infinity, so after softmax their chance is zero, and the allowed ones keep their odds relative to each other. Then sampling happens as usual over what's left.
The simple way to build that mask is to test all of the vocabulary, 128,000 tokens or more, against the grammar before every token we generate. Most of the engineering is about not doing that.
When tokens and grammar don't line up
Let's shrink it right down. Say the rule is "a decimal number", like 12.5
or .2. As a little state machine, it has a start, a "reading the whole
part" state, a "just read the dot" state, and a "reading the decimals"
state.
Now give the model a toy vocabulary of five tokens: A, ., 42, .2
and 1.
At the start, A is blocked and the rest are fine. Say the model picks
.2. That one token reads a dot and a digit, so the machine jumps two
states at once, straight to the decimals, where only 42 and 1 are
allowed now.
That's the heart of it. The grammar thinks in characters. The model thinks in tokens, and one token can cover the end of one rule and the start of the next. Our space-plus-quote token spans "whitespace" and "start of a string", and the engine that only looked one grammar piece ahead didn't allow it.
The fix that the Outlines library is built on (2023) is to do the work ahead of time. For every state, walk every token through the machine and write down which ones are accepted from there. It has to start from every state, because a token can begin anywhere in the pattern. The result is an index: state in, allowed tokens out. During generation we just track the state and look the mask up, so each step costs a lookup instead of a scan.
JSON needs a stack
That works for patterns like our decimal. JSON is harder, because it nests. An object can hold an array that holds an object, as deep as we like, and to know what's allowed next we have to remember which brackets are still open. That needs a stack. And a stack with no depth limit means an endless number of possible states, so we can't precompute a mask for each one.
The engines people actually use answer this in different ways:
- Outlines turns the JSON schema into one big pattern and precomputes as before. Fast once it's built, but building can be slow. Some schema features take it from 40 seconds to 10 minutes.
- XGrammar (2024) noticed that you can judge most tokens from your position in the current rule alone, without looking at the whole stack. Those get precomputed. Only the few that could close a rule and carry on in the parent need the stack at run time. For Llama 3.1 with a JSON grammar, that's about 1,100 of 128,000 tokens.
- llguidance (the engine inside Microsoft's Guidance library) barely precomputes at all. It works the mask out fresh at each step with a fast parser, walking a tree of the vocabulary, so there's almost no startup cost.
On a hosted API we don't choose: as of 2025-05, llguidance runs OpenAI's Structured Outputs for JSON Schema. Serving open models ourselves, vLLM, SGLang and llama.cpp let us pick.
Why microseconds matter
The mask is CPU work, and it has to be ready when the GPU has scored the next token, or the GPU waits. On a busy server that's one mask per sequence in the batch, every step.
So the engines quote tiny numbers: around 50 microseconds per token for llguidance, under 40 for XGrammar on JSON. They also hide their work behind the GPU's, compiling the grammar while the prompt is being read (the prefill step) and building each mask while the model is scoring.
It can even beat no constraint. When the grammar allows only one continuation, like the fixed text of a key name, the engine can write those tokens without running the model. In one side-by-side test, Guidance took 6 to 9 ms per output token against 15 to 17 ms unconstrained, thanks to that skipping. Outlines took 30 to 47 ms, after seconds of compiling.
Each engine's speed claims come from its own authors, and that side-by-side test was run by the Guidance team. Times for our own schemas on our own setup are the only ones worth trusting.
Does the engine even support our schema?
The same test checked something the speed numbers skip. It separates schemas an engine accepts from schemas where its output is actually valid. On one set of simple schemas from GitHub, OpenAI's API accepted only 30%, but 97% of what it accepted came back valid. Open engines accepted far more, and let more invalid output through. So hosted APIs support a smaller slice of JSON Schema and enforce it tightly, which is why we hit "unsupported schema" errors there.
An engine can also be wrong in either direction: blocking output the schema allows, or letting through output it forbids. In the second case we're back to checking and retrying anyway.
Back to the tab
So does constraining hurt the answers? It only removes invalid tokens, so in theory it shouldn't. The argument has gone back and forth since 2024.
One 2024 paper found reasoning got worse under format restrictions. But its
"constrained" setting was JSON mode, which promises valid JSON and no
schema, and on one task GPT-3.5 put the answer key before the reason
key every single time. It answered before it thought (that's
chain of thought run backwards). The same paper found
JSON mode held up, sometimes helped, on classification, where a smaller
answer space means fewer ways to be wrong.
Later re-runs, with the reasoning field first and better prompts, had constrained output matching or beating plain text on the same tasks.
And the tab explains how both sides could see something real. On a JSON version of a math benchmark, with a Mistral 7B model:
The naive engine, the one that forced the tab, cost about 11 points. An engine that allows tokens spanning grammar pieces cost nothing.
Notice who ran the biggest re-run: the Guidance team, and Guidance came out on top. As far as we could find, nobody neutral has settled it. What fits all of it so far: a good engine with the reasoning field first doesn't hurt and may help, and a naive engine, or a schema that forces the answer before the reasoning, can hurt a lot.
Even a good engine can nudge things, though. A model that wants to write
89,000 into an integer field can't write the comma, and might carry on
as 890000. Valid field, wrong number. That's why checking values
afterwards still matters (schema validation).
Further reading
- Efficient Guided Generation for Large Language Models, Brandon Willard and Rémi Louf, 2023. The state-machine index behind Outlines. The decimal example is Figure 1.
- XGrammar: Flexible and Efficient Structured Generation Engine for Large Language Models, Yixin Dong et al., 2024. Why JSON needs a stack, and the trick of prechecking most of the vocabulary anyway.
- Low-level Guidance (llguidance), guidance-ai (Microsoft), 2025. The engine behind OpenAI's Structured Outputs, and why microseconds matter. Its comparison with the others is a competitor's.
- JSONSchemaBench, Saibo Geng et al., 2025. Speed, schema coverage and quality side by side, from the Guidance team. The coverage tables are the most useful part.
- Guiding LLMs The Right Way, Luca Beurer-Kellner, Marc Fischer and Martin Vechev, 2024. Where the tab example comes from. Figure 1 is worth a look.
- Let Me Speak Freely?, Zhi Rui Tam et al., 2024. The study that found format limits hurt reasoning but not classification. Read the part about key order.