grammar

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package grammar converts a subset of JSON Schema into a context-free grammar state machine that can constrain token-by-token generation to produce only valid JSON conforming to the schema.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TokenMask

func TokenMask(g *Grammar, vocab []string) []bool

TokenMask returns a boolean mask over vocab. mask[i] is true if vocab token i is a valid next token at the current grammar state.

For each token, every byte must advance the grammar successfully. If any byte is rejected by Grammar.Advance, the token is invalid.

Types

type Grammar

type Grammar struct {
	// contains filtered or unexported fields
}

Grammar is an immutable state machine that tracks the current parse position within a JSON value constrained by a JSON Schema. Each call to Advance consumes one byte and returns a new Grammar (or reports the byte as invalid).

func Convert

func Convert(schema *JSONSchema) (*Grammar, error)

Convert transforms a JSONSchema into a Grammar state machine. It returns an error if the schema uses unsupported features.

func (*Grammar) Advance

func (g *Grammar) Advance(b byte) (next *Grammar, ok bool)

Advance consumes byte b and returns the resulting grammar state. If b is not a valid next byte, ok is false.

func (*Grammar) IsComplete

func (g *Grammar) IsComplete() bool

IsComplete returns true when the grammar is in an accepting state — i.e. a complete, valid JSON value has been consumed.

func (*Grammar) ValidBytes

func (g *Grammar) ValidBytes() []byte

ValidBytes returns every byte that is a valid next character in the current state. This is used by constrained decoding to mask logits.

type JSONSchema

type JSONSchema struct {
	Type       string                 // "object", "array", "string", "number", "integer", "boolean", "null"
	Properties map[string]*JSONSchema // for "object"
	Required   []string               // for "object"
	Items      *JSONSchema            // for "array"
	Enum       []any                  // enum values
	Const      any                    // const value
	MinLength  int                    // for "string"
	MaxLength  int                    // for "string", 0 = unlimited

	// Unsupported fields - presence triggers an error from Convert.
	Ref                  string `json:"$ref,omitempty"`
	OneOf                []any  `json:"oneOf,omitempty"`
	AnyOf                []any  `json:"anyOf,omitempty"`
	AllOf                []any  `json:"allOf,omitempty"`
	Pattern              string `json:"pattern,omitempty"`
	AdditionalProperties *bool  `json:"additionalProperties,omitempty"`
}

JSONSchema represents the subset of JSON Schema supported by the converter. Unsupported features ($ref, oneOf, anyOf, allOf, pattern, additionalProperties) cause Convert to return an error.

Jump to

Keyboard shortcuts

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