↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

A format the model must not break

Programs that read a model's output need it in a fixed format: a JSON object with the right keys, a call with valid arguments, a label from a list. Asking for the format in the prompt usually works, but nothing guarantees it; a small model may wrap the JSON in a Markdown fence, add a sentence before it, or answer in prose. Constrained decoding makes the format a property of decoding instead of a request: at every step only the tokens that keep the text a prefix of something valid may be chosen.

If is the logit of token and the set of tokens the format allows at step , the masked distribution is

which is the model's softmax with every disallowed logit set to and the rest renormalised. Greedy decoding then takes the largest allowed logit. The page decodes each prompt twice with Qwen3-1.7B, greedily and with thinking off: once as is and once with the mask, and draws both.

The page records three prompts with a five-key schema for a chemical element or a four-key one for a product review. Unconstrained, the model wraps its JSON in a Markdown fence when the schema is in the prompt, invents its own keys for the review, and writes prose when no format is asked for. None of the three outputs parses as JSON.

Schema to regular expression

The format here is a JSON schema: an object with named properties, each a string, an integer or one value from a list. The panel Schema to token mask shows it and the regular expression that outlines-core's build_regex_from_schema writes for it. A string becomes " followed by any number of characters other than a quote, a backslash or a control code (or an escape such as \\n) and a closing "; an integer is an optional minus followed by 0 or a digit 1–9 and more digits; an enum is an alternation of its JSON-encoded values. Properties come in the schema's order, all required, with at most one space around : and ,.

A schema without unbounded nesting describes a regular language, which a finite automaton can recognise; recursive schemas need a context-free grammar, which XGrammar handles and outlines-core unrolls to a fixed depth.

Expression to automaton to token mask

A tokenizer's vocabulary is made of byte strings, not characters, so the automaton reads bytes. Thompson's construction turns the expression into a nondeterministic automaton with one small piece per operator; a character class becomes the UTF-8 byte sequences of its characters, so a byte-level token that stops in the middle of a multi-byte character still leaves the automaton in a live state. Following every possible path at once (the subset construction) gives a deterministic automaton whose state is the set of places the text so far could have reached.

For each state, a token is allowed if reading its bytes one by one never leaves the automaton with no live path, and the end token is allowed only in an accepting state. Willard and Louf's insight, which outlines-core implements in Rust, is that this depends only on the state and the vocabulary, so the allowed set of every state can be computed once and then looked up at each step. The drawer's code builds the same automaton in plain Python and computes the allowed set by walking each of Qwen3's 151,643 byte-level tokens; the recorder checks that it equals outlines-core's set at every recorded step. Qwen3's control tokens are never allowed, except <|im_end|> once the JSON is complete.

The mask at each step

The panel The mask at token t shows the eight most likely tokens before the mask, with the masked ones marked, and the eight most likely allowed tokens after it, renormalised. The probability the mask removes is

where is the log-sum-exp over the allowed logits and over all 151,936. Where the format leaves one choice, such as the opening { or a key's name, only a handful of tokens are allowed and the model has no say; inside a string almost the whole vocabulary is allowed and the model writes what it would have written anyway. The strip Every constrained step draws and the size of for every token of the run.

With the schema in the prompt, the model's first token unconstrained is three backticks, the start of a Markdown fence, with probability 0.96. Only { and {" are allowed there, so the mask removes all of that probability, and from then on it removes almost nothing: the model writes the same values it put inside the fence. The review is different. At 8 of its 28 steps the mask removes more than half the probability, mostly where the model wanted a key of its own, such as review, instead of the schema's.

Same prompt, two decodings

The column Same prompt, two decodings puts the unconstrained output above the constrained one, each checked against the schema. The mask guarantees the format, not the content: the values inside a valid object are still the model's guesses.

With no format asked, the masked run writes the right name, symbol, atomic number and state for oxygen, then never finishes. After "year_discovered": 1774 the model's favourite token is ,, to start another key, but all five keys are written and the schema has no room for a sixth. Among the 13 allowed tokens (ten digits, a space, } and }), another digit is far more likely than the closing brace, and an integer may have any number of digits. So the run keeps writing digits until the 256-token budget cuts it. The grammar guarantees that every prefix can still become valid, not that the model ever gets there; a length limit in the schema or a budget is needed for that.

Masking also changes the distribution in a less obvious way. Renormalising at each step gives every prefix the share of probability the model put on its allowed continuations, not on the complete valid texts below it, so the result is not the model's distribution conditioned on being valid; Park et al. call this distortion and correct it with grammar-aligned decoding. Tam et al. find that strict format restrictions can lower accuracy on reasoning tasks.

Where it came from

PICARD checked a model's SQL against a parser while decoding, and Geng et al. constrained generation with grammars for structured NLP tasks. Willard and Louf's Outlines made it fast by indexing a regular expression's automaton over the vocabulary, the method recorded here; XGrammar extends it to context-free grammars, and DOMINO aligns the grammar with subword tokens so the mask does not force tokenisations the model never saw.

The Tool calling tab shows the other half of the story: a model that writes its calls as JSON with no mask at all, and a caller that has to parse whatever comes back.

Papers

  1. Efficient Guided Generation for Large Language Models Brandon T. Willard, Rémi Louf · arXiv 2023 Compiles a regular expression or grammar to an automaton and indexes the vocabulary by its states, the method outlines-core implements and this page records.
  2. XGrammar: Flexible and Efficient Structured Generation Engine for Large Language Models Yixin Dong et al. · MLSys 2025 Context-free grammars with a pushdown automaton, splitting tokens into those checked once and those that depend on the stack.
  3. Guiding LLMs The Right Way: Fast, Non-Invasive Constrained Generation Luca Beurer-Kellner, Marc Fischer, Martin Vechev · ICML 2024 DOMINO: aligns grammar terminals with subword tokens so the mask does not force tokenisations the model never learned.
  4. Grammar-Aligned Decoding Kanghee Park et al. · NeurIPS 2024 Shows that per-step masking distorts the model's distribution over valid outputs and samples from the conditional instead.
  5. Let Me Speak Freely? A Study on the Impact of Format Restrictions on Performance of Large Language Models Zhi Rui Tam et al. · arXiv 2024 Measures how strict output formats, including constrained decoding, change models' accuracy on reasoning and classification tasks.
  6. PICARD: Parsing Incrementally for Constrained Auto-Regressive Decoding from Language Models Torsten Scholak, Nathan Schucher, Dzmitry Bahdanau · EMNLP 2021 An early constrained decoder: rejects SQL tokens an incremental parser cannot accept.
  7. Grammar-Constrained Decoding for Structured NLP Tasks without Finetuning Saibo Geng et al. · EMNLP 2023 Grammars as a general way to force structured outputs (information extraction, parsing) from an unchanged model.
  8. Qwen3 Technical Report An Yang et al. · arXiv 2025 The model recorded here, in its non-thinking mode.

See also