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 ¶
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 ¶
Advance consumes byte b and returns the resulting grammar state. If b is not a valid next byte, ok is false.
func (*Grammar) IsComplete ¶
IsComplete returns true when the grammar is in an accepting state — i.e. a complete, valid JSON value has been consumed.
func (*Grammar) ValidBytes ¶
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.