Course contentsShow
← AI Engineering
Lesson 782 of 1,88619. Structured Output and ValidationFree lesson

GBNF (GGML BNF) for llama.cpp

Define custom grammars in GBNF format to constrain local model outputs in llama.cpp deployments.

GBNF (GGML BNF) for llama.cpp

What You'll Learn

You'll learn how to write custom grammar rules in GBNF format to guarantee that llama.cpp models generate only valid, structured outputs.

What is GBNF?

GBNF (GGML Backus-Naur Form) is llama.cpp's native grammar specification language for constrained generation. It's a variant of BNF (Backus-Naur Form) specifically designed for the GGML runtime that powers llama.cpp.

Think of GBNF as a blueprint that tells the model "you can only build sentences using these exact pieces, arranged in these exact ways." Instead of hoping the LLM outputs valid JSON or follows a format, GBNF enforces it at the token level by constraining which tokens can appear next.

How GBNF Works

GBNF defines rules using production syntax. Each rule names a pattern and describes what it can contain:

  • Terminals: Literal strings in quotes ("true", "{")
  • Non-terminals: References to other rules (value, number)
  • Operators: | for alternatives, * for zero-or-more, + for one-or-more, ? for optional

For example, a simple boolean grammar:

root ::= "true" | "false"

A basic JSON object structure:

root ::= "{" ws string-pair ws "}"
string-pair ::= "\"key\"" ws ":" ws value
value ::= "\"" [^"]* "\"" | [0-9]+
ws ::= [ \t\n]*

When you pass this grammar to llama.cpp, it uses logit biasing (which you learned earlier) to mask invalid tokens at each generation step, guaranteeing output matches your rules.

Why Use GBNF

GBNF gives you deterministic control over local model outputs without relying on external parsing or post-processing. Perfect for mission-critical structured data extraction when running llama.cpp locally.

Key Takeaway: GBNF lets you write custom grammar rules that llama.cpp enforces during generation, guaranteeing structurally valid outputs through token-level constraints.