Skip to main content
Structured output enables your agents to return data in a predefined schema instead of free-form text. This is essential for integrating agents with databases, APIs, or any system that requires consistent data formats.

Overview

When you enable structured output, your agent will:
  1. Process the input normally
  2. Extract information according to your schema
  3. Return validated, typed data matching your specification
  4. Ensure type safety and consistency
Structured output uses runtime schema generation and validation to ensure your agents return properly formatted data.
Important: Structured output is NOT included in the conversation history array. This means structured output from previous agents is only accessible through template variables in prompts (e.g., {{ agent_name.output.field }}) and will not appear in the {{ history }} or {{ history[N] }} variables.

When to Use Structured Output

Data Extraction

Extract structured information from unstructured text (resumes, invoices, documents)

API Integration

Generate responses that match your API schema requirements

Database Inserts

Create records with validated fields for database operations

Multi-Agent Pipelines

Pass typed data between agents in complex workflows

Basic Configuration

Add the structured_output field to your agent configuration:

Type Notation

Define field types using our type notation syntax. The common types are str, int, float, bool, list[T], dict[K, V], and their nullable forms T | None / Optional[T]:
See Type Notation for the full reference — every supported scalar and collection form, nesting, coercion rules, and the nullability interaction between structured output fields and variables.
Enum types are not currently supported. Use str type with clear descriptions for fields that should have restricted values. Guide the LLM via your system prompt to output specific allowed values.
Any field can come back null. Every field is marked optional at the schema-building layer regardless of the type you declare, so an agent that omits a field — or answers null for a field you declared str — does not crash the run. This is deliberate: without it a single null would fail the whole generation call with “Expected string, got null” and take the agent turn down with it.Consequences to design for:
  • Treat every field as potentially absent in downstream prompts and conditional edges.
  • A null is not written into a non-nullable variable by variable_assignments — the assignment is skipped and the previous value kept. Declare the variable T | None if storing null is what you want. See Type Notation.
  • Declaring the field itself T | None is good practice for documenting intent, but it is not what makes the parse succeed.

Root Schemas

Normally a schema defines named fields and the agent returns a JSON object. Use the reserved field name __root__ when you want the agent to return a bare value instead — most usefully a bare array:
The agent’s output is then the list itself — ["billing", "refunds"] — not {"topics": [...]}.
__root__ must be the only key in the schema. Mixing it with named fields fails validation:
Any type notation works as the root type, not just lists:

Referencing a root schema downstream

A root-schema output is stored as a bare list or value rather than a field map, so there is no field name to address. Reference the whole output:
The same applies in prompts — use {{ topic_extractor.output }}, not {{ topic_extractor.output.field }}.
Prefer a named field when you are unsure. A root schema buys you a cleaner payload, but it gives up per-field addressing in prompts, variable assignments and conditional edges.

Quick Start Example

Extract contact information from text:
Input:
Output:

Multi-Agent Data Passing

Use structured output to pass typed data between agents in workflows. Downstream agents can access structured outputs from previous agents using template variables in their system prompts.

Accessing Structured Output in Prompts

Use the {{ agent_name.output.field }} syntax to inject structured output into agent prompts:
Supported template variables:
  • {{ agent_name.output.field }} — Access specific field from structured output
  • {{ agent_name.output.nested.field }} — Access nested fields (dot-separated path)
  • {{ agent_name.output }} — Access entire structured output as JSON string
  • {{ variables.name }} — Access custom variables (see Variables System)
  • {{ user_input }} — Latest user message
  • {{ history[N] }} — Last N messages from conversation (structured output NOT included)
Critical: Structured output is excluded from history variables. Use the specific {{ agent_name.output.field }} template variables to access structured data from previous agents.
Complex data types (lists, dicts) are automatically serialized to JSON when injected into prompts.

Multi-Agent Example

Pass structured data between agents:
Template variables: Use {{ agent_name.output.field }} to access structured output from previous agents

Best Practices

Clear Descriptions: Always provide detailed descriptions for each field. The agent uses these to understand what to extract.
Appropriate Types: Choose the most specific type possible. For fields with restricted values, use str type and specify allowed values in the description and system prompt.
System Prompt Alignment: Ensure your system prompt explicitly mentions the structured output requirements.
Type Validation: All output is validated against your schema. Invalid data will cause errors. Test your schemas thoroughly.
Nested Structures: For complex nested objects, consider breaking them into multiple agents or using dict types with clear descriptions.

Configuration Reference

Schema Structure

The structured_output field has the following structure:
object
Configuration for structured output generation
enabled defaults to true, so providing a schema is enough to turn structured output on. Set enabled: false to keep a schema in the config while temporarily reverting the agent to free-form text.No other keys are accepted under structured_output — a typo fails validation rather than being ignored.

Field Specification

Each field in the schema must have:
string
required
Type notation for the field. Common types: str, int, float, bool, list[T], dict[K, V], T | None, Optional[T], Any.See Type Notation for the full reference.
string
Human-readable description of what this field represents (optional but strongly recommended).The agent uses this description to understand what data to extract. Be specific and clear. Fields without descriptions may produce less accurate results.

Agent Type Support

llm_agent: ✅ Fully supported - LLM agents can generate structured output for any schema
react_agent: ✅ Fully supported - ReAct agents generate structured output after completing their tool-calling iterations
supervisor: ❌ Not supported - Supervisor agents focus on coordination and do not produce structured output

Validation and Error Handling

The system automatically validates structured output:
Type Checking: Values must match the declared type
Required Fields: All fields in the schema must be present in the output
Format Validation: Basic format checking for strings, numbers, etc.

Common Errors

Example Error Response

If validation fails, you’ll receive a detailed error:

Variable Assignments

Structured output fields can be automatically mapped to system variables using variable_assignments. This creates a data pipeline: structured output → variables → downstream prompt substitution.
Variable assignments also support static values and non-structured output. The system applies automatic type coercion during variable assignment, converting strings to types like integers, booleans, or JSON arrays to ensure they match the variable’s defined type. See Variables System for details.

Limitations

  • Structured output is currently supported for llm_agent and react_agent types
  • Complex nested objects may require careful prompt engineering
  • Very large schemas (>20 fields) may impact performance
  • Every field is effectively optional — any field can return null regardless of its declared type
  • Output must be valid JSON
  • Enum types are not supported
  • A __root__ schema gives up per-field addressing in prompts, variable assignments and conditional edges

Next Steps

Conditional Edges

Learn how to route agents based on structured output values