IT lexicon › AI & ML › Guided decoding

Guided decoding

AI & ML På svenska → Updated: 2026-07-29

Forcing a language model to produce output that follows a schema by masking out, at every step, every token that would violate the grammar. Invalid JSON becomes literally impossible.

The schema — a JSON Schema, a regular expression or a formal grammar — is compiled into a state machine. At each position the machine knows which characters are permitted, the corresponding tokens keep their probabilities and all others are set to negative infinity before sampling. The model still chooses freely, but only among valid options. The difference from asking for JSON in the prompt is categorical: the latter sometimes fails, the former cannot. The libraries Outlines and XGrammar, plus built-in support in llama.cpp, vLLM and most APIs, implement this. The cost is compiling the schema and the fact that the model is occasionally forced into phrasings it would not have chosen.

← Back to the lexicon