Documentation
¶
Overview ¶
Package structured implements a LangChain-v1 style structured output framework for Go. It mirrors langchain's ResponseFormat with ToolStrategy, ProviderStrategy, and AutoStrategy, plus schema compilation and validation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidSchema = errors.New("structured: invalid schema")
ErrInvalidSchema is returned when a schema cannot be compiled.
Functions ¶
func ToolSchemaFor ¶
ToolSchemaFor returns the tool binding schema for a ToolStrategy. This is the schema wrapped as a tool whose output is the structured response.
func Validate ¶
func Validate(r *ResponseFormat) error
Validate checks the response format for internal consistency.
Types ¶
type AutoStrategy ¶
AutoStrategy selects ToolStrategy or ProviderStrategy at runtime based on the model's capabilities.
func NewAuto ¶
func NewAuto(schema Schema) *AutoStrategy
NewAuto builds an AutoStrategy from a schema.
type MultipleOutputsError ¶
type MultipleOutputsError struct {
ToolNames []string
}
MultipleOutputsError is returned when a ToolStrategy produced more than one structured tool call.
func (*MultipleOutputsError) Error ¶
func (e *MultipleOutputsError) Error() string
type ProviderStrategy ¶
ProviderStrategy drives structured output using provider-native structured output (e.g. OpenAI json_schema, Anthropic). Only providers that support it can use it.
func NewProvider ¶
func NewProvider(schema Schema) *ProviderStrategy
NewProvider builds a ProviderStrategy from a schema.
func (*ProviderStrategy) Schema ¶
func (p *ProviderStrategy) Schema() Schema
Schema implements Strategy.
type ResponseFormat ¶
type ResponseFormat struct {
Strategy Strategy
}
ResponseFormat wraps a strategy and the schema it applies to.
func NewResponseFormat ¶
func NewResponseFormat(s Strategy) *ResponseFormat
NewResponseFormat wraps a strategy into a ResponseFormat.
type Strategy ¶
type Strategy interface {
// Kind returns the strategy kind: "tool", "provider", or "auto".
Kind() string
// Schema returns the JSON schema the strategy constrains output to.
Schema() Schema
}
Strategy is the interface implemented by all structured output strategies.
func ResolveAuto ¶
func ResolveAuto(r *ResponseFormat, supportsProvider bool) Strategy
ResolveAuto selects the strategy to use for a given model. If the strategy is Auto and the model supports provider-native structured output, a Provider strategy is returned; otherwise a Tool strategy.
type ToolStrategy ¶
type ToolStrategy struct {
SchemaDoc Schema
Name string
Description string
ToolMessageContent string
// HandleErrors controls retry behavior on validation failure. nil means
// default (retry with a generic error message).
HandleErrors any
}
ToolStrategy drives structured output by binding the schema to a tool and asking the model to call it. It works across all providers.
func NewTool ¶
func NewTool(schema Schema) *ToolStrategy
NewTool builds a ToolStrategy from a schema.
type ValidationError ¶
ValidationError is returned when a model output does not match the schema.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Unwrap ¶
func (e *ValidationError) Unwrap() error