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 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.
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 ¶
Field is one JSON object property, emitted in declaration order.
func FromJSONSchema ¶
FromJSONSchema builds Fields from a minimal JSON-schema object (used by the dynamic `extract` task). It handles type: string/number/integer/boolean, arrays of strings, and string enums. Property order follows the schema's "required" list if present, else map iteration is sorted by required then name.