What is it?
GBNF is a small language for describing allowed text, used by llama.cpp (a popular local model engine). You write rules like "an answer is either yes or no", or "a record has a name and an age", and the engine will only let the model write text that fits.
Also known as: GBNF, llama.cpp grammar, llama cpp grammars, gbnf format, GGML BNF, grammar file llama.cpp, json schema to gbnf.
Technical definition
GBNF extends Backus–Naur Form with regex-like character classes and repetition operators; the root rule defines the language, and rules can reference one another. llama.cpp also ships a helper to convert JSON Schema into GBNF. The syntax and tooling are defined by the llama.cpp project and may change; consult its current grammar documentation rather than relying on this summary for exact syntax.
Entity type: language · Freshness class: volatile.
When should I use it?
Use when you run models through llama.cpp and need strict formats without relying on prompts.
Implementation patterns
- Root rule + small rule set, tested on valid/invalid strings.
- Generate GBNF from a JSON Schema for JSON outputs.
- Pass the grammar through the llama.cpp CLI/server parameters.
- Keep a regression set of expected outputs.
Technologies and frameworks
Commonly associated: llama.cpp, llama.cpp server, JSON-Schema-to-GBNF converters. Names are examples of what exists, not recommendations, and projects change quickly.
Practical guide
- Start with a one-rule grammar.
- Run llama.cpp with the grammar parameter.
- Add rules incrementally.
- Convert a JSON Schema when you need JSON.
- Validate outputs afterwards.
Common failure modes
- Syntax errors in the grammar file.
- Grammar too permissive or too strict.
- Assuming other runtimes accept the same grammar format.
- Expecting grammar to correct factual errors.
Troubleshooting
- Grammar fails to load — Check syntax against current llama.cpp docs; test with minimal grammar first.
- Output ends abruptly — Check for a path to the end of the root rule and the max token limit.
- Runtime ignores grammar — Confirm that your wrapper (e.g. Ollama or a UI) exposes and applies grammar/format controls.
Contrasted with
- JSON Schema — GBNF describes any formal language; JSON Schema only JSON documents (and can be converted into GBNF).
Questions this page answers
- What is GBNF?
- How do I use grammars in llama.cpp?
- How do I convert a JSON schema to GBNF?
- Can Ollama use GBNF grammars?
Typical goals: write a grammar for llama.cpp; force local models to follow a format.
Sources and verification
Status: not independently verified. This entry was authored on 2026-10-06 from general technical knowledge, without live checking of the sources below. Treat every source as a lead to confirm (identifier, title, authorship, current version) before publication or citation.
Freshness class: volatile — Products, versions, protocols, benchmarks, leaderboards, regulations and dates. Verify against the primary source before relying on specifics; re-review every 3 months.
Time-sensitive claims to verify before relying on them:
- Exact GBNF syntax and available tooling.
- Which wrappers expose grammar controls.
- The repository's current organisation/owner.
Canonical sources (unverified leads):
- llama.cpp project grammar documentation — github.com/ggml-org/llama.cpp (grammars README; verify current location)