Constrained decoding: grammars that make invalid tokens unselectable
Structured output forces the model to emit only tokens consistent with a grammar state. At each step, tokens that would break the parse get -inf logits before the softmax — the model cannot produce a syntax error because the syntax-violating tokens have zero probability.
Logit Masking
A per-state mask (allowed token set) multiplies logits before sampling. Masking is a vectorized op: microsecond-scale, invisible next to attention.
Grammar Compilation
Context-free grammars or JSON schemas compile to token-level state machines ahead of time. xgrammar and outlines precompile to bitmask lookups keyed by parse state.
The Semantics Caveat
Syntactic validity is guaranteed; semantic validity is not. A grammar can force valid JSON while the values inside are still wrong — validation belongs above the engine.
Renormalization preserves the model's preferences among legal tokens. The masking engine's only real cost is maintaining parse state — O(1) per token with a precompiled automaton.
- SGLang uses xgrammar to cut guided-decoding overhead ~10× vs naive recompilation per request.
- OpenAI-compatible 'response_format: json_schema' is this machinery surfaced as an API parameter.