Documentation
¶
Overview ¶
Package structuredagent wraps ADK agents with JSON-schema-constrained I/O.
NewAgent validates structured input before invoking the wrapped agent and validates structured output before returning the final ADK event. The package is intended for higher-level workflows that require deterministic machine- readable JSON instead of free-form model text.
Index ¶
- Variables
- func NewAgent(wrapped adkagent.Agent, setters ...Option) (adkagent.Agent, error)
- type Option
- func WithInputSchema(inputSchema string) Option
- func WithMaxAccumulatedOutputBytes(maxBytes int) Option
- func WithOutputSchema(outputSchema string) Option
- func WithOutputValidationRetries(retries int) Option
- func WithSystemInstruction(instruction string) Option
- func WithoutInputSchema() Option
- func WithoutOutputSchema() Option
- type OutputValidationError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrStructuredIOSchemaValidation = errors.New("structured I/O schema validation error")
ErrStructuredIOSchemaValidation is the umbrella error for all structured I/O schema validation failures.
var ErrStructuredInputSchemaValidation = errors.Join( errors.New("structured input schema validation error"), ErrStructuredIOSchemaValidation, )
ErrStructuredInputSchemaValidation is returned when input JSON fails to match the expected schema. It satisfies errors.Is(err, ErrStructuredIOSchemaValidation).
var ErrStructuredOutputSchemaValidation = errors.Join( errors.New("structured output schema validation error"), ErrStructuredIOSchemaValidation, )
ErrStructuredOutputSchemaValidation is returned when output JSON fails to match the expected schema. It satisfies errors.Is(err, ErrStructuredIOSchemaValidation).
Functions ¶
func NewAgent ¶
NewAgent creates an ADK agent wrapper around another agent and validates structured input/output using configured schemas.
Example ¶
inner, err := adkagent.New(adkagent.Config{
Name: "formatter",
Description: "Formats structured output",
Run: func(ctx adkagent.InvocationContext) iter.Seq2[*session.Event, error] {
return func(yield func(*session.Event, error) bool) {
ev := session.NewEvent(ctx.InvocationID())
ev.Content = genai.NewContentFromText(`{"output":"done"}`, genai.RoleModel)
ev.TurnComplete = true
_ = yield(ev, nil)
}
},
})
if err != nil {
fmt.Println(err)
return
}
wrapped, err := NewAgent(inner)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(wrapped.Name())
Output: formatter_structured
Types ¶
type Option ¶
type Option func(*options)
Option customizes wrapper behavior at creation time.
func WithInputSchema ¶
WithInputSchema overrides default input JSON schema.
func WithMaxAccumulatedOutputBytes ¶
WithMaxAccumulatedOutputBytes sets the maximum number of output text bytes accumulated for schema validation in a single turn.
func WithOutputSchema ¶
WithOutputSchema overrides default output JSON schema.
func WithOutputValidationRetries ¶
WithOutputValidationRetries sets the number of retries for output schema validation failures. Default is 1 retry (2 total attempts).
func WithSystemInstruction ¶
WithSystemInstruction sets the system instruction for the agent. This instruction is prepended to the I/O requirements prompt.
func WithoutInputSchema ¶ added in v0.0.10
func WithoutInputSchema() Option
WithoutInputSchema disables input JSON schema validation and forwards the raw user text into the structured wrapper prompt.
func WithoutOutputSchema ¶ added in v0.0.10
func WithoutOutputSchema() Option
WithoutOutputSchema disables output JSON extraction and schema validation. The wrapped agent's accumulated text is returned as-is.
type OutputValidationError ¶ added in v0.0.11
OutputValidationError carries output validation context that callers may use for provider-aware error handling.
func (*OutputValidationError) Error ¶ added in v0.0.11
func (e *OutputValidationError) Error() string
func (*OutputValidationError) Unwrap ¶ added in v0.0.11
func (e *OutputValidationError) Unwrap() error