Documentation
¶
Overview ¶
Package schema provides types and utilities for structured output constraints.
StructuredOutputInfo defines a JSON Schema that can be passed to providers supporting native JSON schema output (OpenAI json_schema mode, Gemini responseSchema). Providers without native support receive an injected system message asking them to respond with conforming JSON.
Usage with a Go struct:
type BugReport struct {
Title string `json:"title" desc:"One-line summary of the bug"`
Severity string `json:"severity" desc:"critical|high|medium|low" enum:"critical,high,medium,low"`
Steps []string `json:"steps" desc:"Reproduction steps"`
}
s := schema.FromStruct("bug_report", "A structured bug report", BugReport{})
Usage with a manual schema:
s := schema.New("result", "Task result", map[string]any{
"properties": map[string]any{
"done": map[string]any{"type": "boolean"},
"message": map[string]any{"type": "string"},
},
}, []string{"done", "message"})
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type StructuredOutputInfo ¶
type StructuredOutputInfo struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]any `json:"parameters"`
Required []string `json:"required"`
}
StructuredOutputInfo defines a JSON Schema constraint for model output. Name is used as the schema identifier in the provider request.
func FromStruct ¶
func FromStruct(name, description string, v any) *StructuredOutputInfo
FromStruct derives a StructuredOutputInfo by reflecting on a Go struct.
Supported struct tags:
- json:"name" — overrides the JSON property name (required)
- desc:"…" — adds a "description" field to the property schema
- enum:"a,b" — restricts the value to the listed strings
- required:"false" — marks the field as nullable (pointer or omitempty also do this)
All non-pointer, non-omitempty fields are included in `required` and OpenAI strict mode is satisfied because every property appears in `required`.
func New ¶
func New(name, description string, parameters map[string]any, required []string) *StructuredOutputInfo
New creates a StructuredOutputInfo from an explicit parameters map.
func (*StructuredOutputInfo) GeminiResponseSchema ¶
func (s *StructuredOutputInfo) GeminiResponseSchema() map[string]any
GeminiResponseSchema returns the `responseSchema` object for the Gemini generateContent API.
func (*StructuredOutputInfo) OpenAIResponseFormat ¶
func (s *StructuredOutputInfo) OpenAIResponseFormat() map[string]any
OpenAIResponseFormat returns the `response_format` object for the OpenAI Messages API json_schema mode.
func (*StructuredOutputInfo) SystemPromptHint ¶
func (s *StructuredOutputInfo) SystemPromptHint() string
SystemPromptHint returns a compact instruction suitable for injection into a system prompt when the provider does not support native structured output. It renders the schema as inline JSON so the model knows the expected shape.