How Structured Output Works
When you provide a Zod schema to the agent:- Schema awareness: When using
streamEvents(), the agent receives schema information in the query to understand what data structure to return - Automatic validation: Responses are validated against your schema using Zod
- Type safety: Full TypeScript type inference from your schema
- Formatting retries: If the output doesn’t match the schema format, the system retries up to 3 times to format it correctly
- Guaranteed structure: You always get data matching your schema or an error
Benefits
Type Safety
Get full TypeScript types automatically inferred from your schema:Formatting Retry
If the agent’s response doesn’t match the schema format, the system automatically retries up to 3 times to format the output correctly. This handles cases where the LLM output needs to be restructured to match your schema.Validation
Responses are validated at runtime, ensuring your code always receives correctly structured data.Basic Example
Schema-Aware Prompts: When using
streamEvents(), the query is automatically enhanced with schema information before execution, helping the agent understand what data structure is needed. When using run() or stream(), structured output conversion happens after execution completes.Streaming with Structured Output
When usingstreamEvents() with structured output, you’ll receive special events during the conversion process:
on_structured_output_progress: Emitted every 2 seconds while converting to structured formaton_structured_output: Emitted when structured output is successfully generatedon_structured_output_error: Emitted if structured output conversion fails
Key Benefits
- Type Safety: Get Zod (TypeScript) models with full IDE support and validation
- Schema-Aware Prompts: When using
streamEvents(), the agent receives enhanced prompts that include schema information, helping it understand what data structure is needed - Formatting Retry: Up to 3 automatic retries if the output format doesn’t match the schema
- Field Validation: Built-in validation for required fields, data types, and constraints using Zod
streamEvents()), and the output is validated and formatted to match your schema. If validation fails after retries, an error is thrown.