structuredagent

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: MIT Imports: 13 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

View Source
var ErrStructuredIOSchemaValidation = errors.New("structured I/O schema validation error")

ErrStructuredIOSchemaValidation is the umbrella error for all structured I/O schema validation failures.

View Source
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).

View Source
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

func NewAgent(wrapped adkagent.Agent, setters ...Option) (adkagent.Agent, error)

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

func WithInputSchema(inputSchema string) Option

WithInputSchema overrides default input JSON schema.

func WithMaxAccumulatedOutputBytes

func WithMaxAccumulatedOutputBytes(maxBytes int) Option

WithMaxAccumulatedOutputBytes sets the maximum number of output text bytes accumulated for schema validation in a single turn.

func WithOutputSchema

func WithOutputSchema(outputSchema string) Option

WithOutputSchema overrides default output JSON schema.

func WithOutputValidationRetries

func WithOutputValidationRetries(retries int) Option

WithOutputValidationRetries sets the number of retries for output schema validation failures. Default is 1 retry (2 total attempts).

func WithSystemInstruction

func WithSystemInstruction(instruction string) Option

WithSystemInstruction sets the system instruction for the agent. This instruction is prepended to the I/O requirements prompt.

Jump to

Keyboard shortcuts

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