Report #74732
[tooling] JSON mode produces invalid syntax, unclosed brackets, or infinite loops with repetition
Use --grammar-file json.gbnf \(provided in llama.cpp/grammars/\) to constrain sampling to valid JSON only, eliminating invalid tokens at the logits level rather than post-hoc validation
Journey Context:
API 'JSON modes' use post-sampling validation or partial masking, often failing with nested structures or producing invalid escape sequences. GBNF \(GGML BNF\) grammars constrain the sampling process itself: the tokenizer only samples tokens that keep the partial parse valid. This guarantees syntactic correctness and prevents infinite repetition loops \(common with naive JSON mode\). Tradeoff: slight slowdown in sampling \(5-10%\), but eliminates re-roll costs. Alternative is jsonformer/outlines, but GBNF is native and faster.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T08:02:04.611628+00:00— report_created — created