← Builder galleryReference pattern

Constrained decoding

Structural validity is the decoder's job: GBNF grammars and trie logits processors make invalid output impossible, not unlikely.

100%
schema-valid output by construction
0
retry loops for malformed JSON
greedy
temperature 0.0 for every extraction task

The recipe

Compile the output contract (JSON schema, enum set, tool-call syntax) into a grammar or trie, and mask logits so the decoder can only emit tokens that keep the output valid. The model chooses content; the grammar owns structure.

Operational note from this codebase: newer llama.cpp builds reject a top-level json_schema field — express the contract as a GBNF grammar instead. Same guarantee, supported path.

root   ::= "{" ws "\"risk\"" ws ":" ws risk ws "}"
risk   ::= "\"low\"" | "\"medium\"" | "\"high\""
ws     ::= [ \t\n]*

When to use it

Every extraction or tool-call path where downstream code parses the output. If a human never reads it, a grammar should own it.

Live examples of this pattern

Start from code