Skip to main content
← Lab

grammarexact

mixedupstream bugPython · CPUInference & servingJuly 12, 2026

the question

Is llama.cpp's GBNF constrained sampler an exact and sound token-level recognizer of the grammar language?

what came out

Mixed. At states on whole-codepoint boundaries the byte-walking matcher is exactly the viable-prefix recognizer across the corpus. But llama_grammar_match_partial_char omits a lead-byte-length check, so a character class mixing UTF-8 lengths admits a spurious continuation byte (predicted 0x80|(C>>12)) and can emit an overlong, invalid-UTF-8 string while the sampler signals EOS.

method & receipts

  • Result: mixed
  • Pre-registered the prediction was written down and committed before the run.
  • Reproducible one script re-runs the whole thing from scratch.
  • Tested — a correctness/benchmark suite ships alongside the code.

→ read the code and re-run it

github.com/v-code01/grammarexact