schema

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package schema is the structured, transformable tool-schema value type. It contains no runtime reflection and no generated code.

Index

Constants

This section is empty.

Variables

View Source
var ErrArgumentValidation = errors.New("agentkit: tool arguments failed validation")
View Source
var ErrInvalidSchema = errors.New("agentkit: schema cannot be converted")

Functions

func DeleteOptionalNulls

func DeleteOptionalNulls(s *Schema, in map[string]any) map[string]any

DeleteOptionalNulls is step 2 of the argument pipeline (core.PrepareArguments). Constrained sampling forces the model to emit every declared property, so optional fields arrive as explicit nulls; treating them as present is a validation failure on well-formed output. Returns a copy.

func StrictSubsetOK

func StrictSubsetOK(s *Schema) bool

StrictSubsetOK is the pre-send probe: whether s has a strict subset.

func Validate

func Validate(s *Schema, v any) error

Validate is step 4 of the argument pipeline. It reports ALL issues, not the first, so one round trip fixes the whole call. v is a value as encoding/json decodes it (an object is a map[string]any); numbers may be json.Number or any Go number.

Types

type AdditionalProperties

type AdditionalProperties struct {
	Allowed bool
	Schema  *Schema // nil => a bare boolean
}

type Coercion

type Coercion struct {
	Path string
	From Type
	To   Type
}

func Coerce

func Coerce(s *Schema, in map[string]any) (map[string]any, []Coercion)

Coerce is step 3 of the argument pipeline: string->number, string->bool, number->string, against the declared schema only. Never guesses without a declared type. A string becomes a number only when it is a JSON number literal ("NaN", "+5", ".5", "5." and "1_0" stay strings for Validate to refuse); a number coerced to integer is an int64, to number a float64.

type Extra

type Extra []Keyword

Extra is a schema's passthrough keywords, in authored order.

func (Extra) Get

func (e Extra) Get(key string) (any, bool)

Get returns the value of key, decoded, and whether it is present.

type Field

type Field struct {
	Name     string
	Schema   *Schema
	Required bool
}

Field is one entry of an Object(...) call.

func Opt

func Opt(name string, s *Schema) Field

func Prop

func Prop(name string, s *Schema) Field

type Issue

type Issue struct {
	Path    string
	Message string
}

type Keyword

type Keyword struct {
	Key   string
	Value json.RawMessage
}

Keyword is one passthrough keyword: its name and its value as JSON.

type Schema

type Schema struct {
	Type        Type
	Description string
	Title       string

	Properties    map[string]*Schema
	PropertyOrder []string
	Required      []string
	// AdditionalProperties is nil for absent; non-nil distinguishes
	// false / true / a subschema.
	AdditionalProperties *AdditionalProperties

	Items *Schema

	// Enum values are verbatim bytes in declaration order.
	Enum []json.RawMessage
	// Const + HasConst distinguish `const: null` from an absent const. This is
	// the case reflection cannot express, and it is the reason the package
	// builds schemas without reflection.
	Const    json.RawMessage
	HasConst bool

	// Nullable on a concrete type marshals as a type array, e.g.
	// "type":["string","null"]. JSON Schema has no `nullable` keyword (that is
	// OpenAPI 3.0) and nothing else fixes this form; it is pinned by a golden.
	Nullable bool

	AnyOf []*Schema
	OneOf []*Schema
	AllOf []*Schema

	Minimum          *float64
	Maximum          *float64
	ExclusiveMinimum *float64
	ExclusiveMaximum *float64
	MultipleOf       *float64

	MinLength *int
	MaxLength *int
	Pattern   string
	Format    string

	MinItems    *int
	MaxItems    *int
	UniqueItems *bool

	// Extra carries passthrough keywords in authored order — the other case
	// reflection cannot express. It is also how a forbidden keyword such as
	// $ref sneaks past a strict rewrite that only inspects modelled fields,
	// which is why StrictSubset scans it.
	Extra Extra
}

Schema is a JSON Schema value AgentKit can rewrite, not a blob it can only forward. PropertyOrder is authoritative for emission order: a bare map cannot carry it because Go marshals maps with sorted keys unconditionally, and property order is model-visible.

func AnyOf

func AnyOf(alts ...*Schema) *Schema

func Array

func Array(items *Schema, desc ...string) *Schema

func Bool

func Bool(desc ...string) *Schema

func Const

func Const(v any) *Schema

func ConstNull

func ConstNull() *Schema

ConstNull is the case a bool cannot express.

func Enum

func Enum(desc string, values ...string) *Schema

func Int

func Int(desc ...string) *Schema

func Number

func Number(desc ...string) *Schema

func Object

func Object(fields ...Field) *Schema

func OneOf

func OneOf(alts ...*Schema) *Schema

func Parse

func Parse(data []byte) (*Schema, error)

Parse decodes a JSON Schema document. It reads the document token by token with encoding/json/jsontext, so the order the author declared properties in becomes PropertyOrder, and a keyword Schema does not model is kept in Extra, in their authored order. Malformed JSON, trailing data, a duplicate key, or a modelled keyword holding the wrong kind of value is an error and no schema.

func StrictSubset

func StrictSubset(s *Schema) (*Schema, error)

StrictSubset rewrites s into the target API's strict subset, on a Clone, so a probe never mutates the tool's shared schema.

func String

func String(desc ...string) *Schema

desc is variadic so both String() and String("...") read well. Only the first element is used.

func (*Schema) Clone

func (s *Schema) Clone() *Schema

Clone is a full deep copy. It is mandatory rather than convenient: the strict rewrite, per-provider dialect translation and the per-session schema cache all operate on a copy, and a Tool's schema is shared across turns and across agents.

func (*Schema) Closed

func (s *Schema) Closed() *Schema

func (*Schema) Describe

func (s *Schema) Describe(d string) *Schema

func (*Schema) IsRequired

func (s *Schema) IsRequired(name string) bool

func (*Schema) MarshalJSON

func (s *Schema) MarshalJSON() ([]byte, error)

MarshalJSON emits a fixed keyword order with `properties` written in PropertyList order, then Extra in authored order. Pinned by a golden.

func (*Schema) Max

func (s *Schema) Max(v float64) *Schema

func (*Schema) Min

func (s *Schema) Min(v float64) *Schema

func (*Schema) MinItemsN

func (s *Schema) MinItemsN(n int) *Schema

func (*Schema) Nullable_

func (s *Schema) Nullable_() *Schema

Modifiers mutate and return the receiver. That is safe because every combinator above returns a freshly allocated Schema.

func (*Schema) PropertyList

func (s *Schema) PropertyList() []string

PropertyList returns PropertyOrder completed with any property absent from it (appended in sorted order) so a hand-built Schema still marshals deterministically.

func (*Schema) WithExtra

func (s *Schema) WithExtra(key string, v any) *Schema

WithExtra sets a passthrough keyword. It panics on a value JSON cannot carry: a schema is the caller's own code, and NaN in it is a bug.

type StrictRewriteError

type StrictRewriteError struct {
	Path    string
	Keyword string
	// Reason, when set, replaces the default "uses <Keyword>, which the
	// strict subset forbids" wording: some rejections are about a keyword
	// being ABSENT (an array with no items) or carrying a shape the subset
	// cannot express (additionalProperties as a schema), and "uses items,
	// which the strict subset forbids" would send the author the wrong way.
	Reason string
}

StrictRewriteError names the keyword that made a schema unconvertible. The reason is a value, not a log line, so a caller can tell an unconvertible schema from any other failure and say exactly why.

func (*StrictRewriteError) Error

func (e *StrictRewriteError) Error() string

func (*StrictRewriteError) Is

func (e *StrictRewriteError) Is(target error) bool

type Type

type Type string
const (
	TypeNone    Type = ""
	TypeObject  Type = "object"
	TypeArray   Type = "array"
	TypeString  Type = "string"
	TypeInteger Type = "integer"
	TypeNumber  Type = "number"
	TypeBoolean Type = "boolean"
	TypeNull    Type = "null"
)

type ValidationError

type ValidationError struct {
	Issues []Issue
	Args   json.RawMessage
}

ValidationError carries the arguments as JSON so Error() can echo them and the message is self-correcting. Validate fills Args from the value; a caller holding the model's own bytes (core.PrepareArguments) replaces them, so the echo is in the order the model wrote.

func (*ValidationError) Error

func (e *ValidationError) Error() string

func (*ValidationError) Is

func (e *ValidationError) Is(target error) bool

Jump to

Keyboard shortcuts

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