Documentation
¶
Overview ¶
Package gbnf generates GBNF grammars that constrain llama.cpp output to a fixed JSON shape. We hand-roll generation (no external dep) because Gemma-4 crashes on --json-schema/json_schema/response_format (llama.cpp #22396) and only a raw grammar is a safe path.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JSONSchema ¶ added in v0.206.0
JSONSchema is Object's counterpart for a vLLM seat: the same Fields rendered as the JSON Schema vLLM's `structured_outputs.json` takes, instead of the GBNF llama.cpp's `grammar` takes (register D-129 — vLLM accepts and DISCARDS `grammar`, so a vLLM seat behind llama-swap answered unconstrained). Every field is required and additionalProperties is false, which is exactly what Object constrains: the grammar admits the listed keys in declaration order and nothing else.
`required` carries the fields in DECLARATION order, not sorted, because that list is the only place the order survives — a JSON object's keys have none, and orderedKeys reads `required` first when FromJSONSchema turns this back into Fields. So JSONSchema -> FromJSONSchema round-trips, order and all, and the two constraint expressions of one task cannot drift apart in which field comes first. A nested object is rendered the same way at its own level, so the order survives at every depth.
func Object ¶
Object returns a complete GBNF grammar for a JSON object with the given fields in order. All fields are required and emitted in declaration order, at every depth: a nested object admits its own keys, in order, and nothing else.
The root production stays on ONE line (`root ::= ...`), which WrapThinking relies on; whatever a nested shape needs follows the common rules.
func WrapThinking ¶
WrapThinking turns a grammar G (whose root is "root ::= <prod>") into a reasoning grammar "root ::= \"<think>\" think \"</think>\" ws <prod>" plus a `think` rule. A thinking model then reasons freely inside the <think> span and emits exactly G's structured output after it. `think` matches any run of NON-'<' characters, so the first '<' after the span must begin the literal </think> — reasoning can never derail on stray markup (e.g. </div>). The reasoning span itself therefore cannot contain a literal '<'; the JSON answer still can. Pair with a thinking-OFF server (the grammar — not the chat template — supplies the think tags, so the JSON lands in `content`) and StripThink() on the response. Input that is not a "root ::= " grammar is returned unchanged so the caller can fall back to the plain grammar.
Types ¶
type Field ¶
type Field struct {
Name string
Type FieldType
Enum []string // used when Type == TEnum
Props []Field // used when Type == TObject; empty means any JSON object
Items *Field // used when Type == TArray; nil means strings
}
Field is one JSON object property, emitted in declaration order.
A TObject field carries its own fields in Props and a TArray field the shape of its elements in Items (Items.Name is unused), so a schema of any depth is a tree of Fields. Both are what the strings used to stand in for: before they existed an object and a list of objects compiled to a string and a list of strings, and the seat was constrained into a shape the schema then refused (register F43, job-error class C12).
func FromJSONSchema ¶
FromJSONSchema builds Fields from a minimal JSON-schema object (used by the dynamic `extract` task and the agent re-pack). It handles type: string/number/integer/boolean, string enums, nested objects (their own "properties", to any depth) and arrays of objects or of arrays. Property order follows the schema's "required" list if present, then the remaining properties by name; a nested object is ordered the same way at its own level.
What it still reads as a plain string, and the grammar therefore forces to be one: a type it has no field kind for (a type list such as ["string","null"], an anyOf/oneOf/allOf/$ref schema), and the ITEMS of an array that are not objects or arrays. An array of strings, numbers, booleans or enum values compiles to an array of strings, as it always has: the re-pack's coercion turns the quoted scalars back into the schema's type. Every property a schema lists is required by the grammar, at every depth, whether or not the schema's own "required" names it.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package gbnftest is a small reference interpreter for the GBNF subset that internal/gbnf emits: it answers "does this grammar admit exactly this text?", so a test can check what a grammar-constrained seat could have written without a llama.cpp build in the loop.
|
Package gbnftest is a small reference interpreter for the GBNF subset that internal/gbnf emits: it answers "does this grammar admit exactly this text?", so a test can check what a grammar-constrained seat could have written without a llama.cpp build in the loop. |