gbnf

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

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

func Object(fields []Field) string

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

func WrapThinking(grammar string) string

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
}

Field is one JSON object property, emitted in declaration order.

func FromJSONSchema

func FromJSONSchema(schema map[string]any) []Field

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.

type FieldType

type FieldType int

FieldType enumerates the value kinds we constrain.

const (
	TString FieldType = iota
	TNumber
	TInteger
	TBool
	TStringArray
	TEnum // string limited to Enum values
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL