GbnfBuilder class

Builds GBNF grammar strings from JSON Schema definitions.

The generated grammars constrain llama.cpp's token sampling to only produce tokens that form valid JSON matching the schema. This guarantees structural correctness (valid JSON, correct types) though not semantic correctness (the values may be nonsensical).

Example:

final grammar = GbnfBuilder.fromJsonSchema({
  'type': 'object',
  'properties': {
    'name': {'type': 'string'},
    'age': {'type': 'integer'},
  },
  'required': ['name'],
});
// grammar is a GBNF string ready for llama_sampler_init_grammar()

Constructors

GbnfBuilder()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

fromJsonSchema(Map<String, dynamic> schema, {String rootRule = 'root'}) String
Convert a JSON Schema to a GBNF grammar string.
jsonGrammar() String
Returns a pre-built GBNF grammar that accepts any valid JSON.