Grammar / schema
compiled ahead of time
Parser state
tracks valid continuations
Token mask
per-step valid token set
Masked logits
invalid tokens set to -inf
Softmax + sample
guaranteed valid choice
Formally, a JSON Schema (or any context-free grammar) can be compiled into a finite-state or pushdown automaton over *tokens* rather than characters. At each decoding step, the automaton's current state defines exactly the set of tokens that keep the output on a path to a valid completion; every other token is masked out before sampling.