At every step the sampler masks out tokens that would break the declared structure, leaving the model to choose only among legal continuations. llama.cpp implements this with GBNF grammar files, a BNF-like notation, which makes malformed JSON, unbalanced quotes, or invalid shell syntax structurally impossible rather than merely improbable. It fixes form, not meaning: a grammatically valid output can still be semantically wrong.