str input and str output:
For more advanced patterns, see:
Structured Output
One of our favorite features is using Agents to generate structured data (i.e. a pydantic model). This is generally called “Structured Output”. Use this feature to extract features, classify data, produce fake data etc. The best part is that they work with function calls, knowledge bases and all other features. Structured output makes agents reliable for production systems that need consistent, predictable response formats instead of unstructured text. Let’s create a Movie Agent to write aMovieScript for us.
1
Structured Output example
movie_agent.py
2
Run the example
Install librariesExport your keyRun the example
MovieScript class, here’s how it looks:
Streaming Structured Output
Streaming can be used in combination withoutput_schema. This returns the structured output as a single RunContent event in the stream of events.
1
Streaming Structured Output example
streaming_agent.py
2
Run the example
Install librariesExport your keyRun the example
Structured Input
An agent can be provided with structured input (i.e a pydantic model or aTypedDict) by passing it in the Agent.run() or Agent.print_response() as the input parameter.
1
Structured Input example
2
Run the example
Install librariesExport your keyRun the example
Validating the input
You can setinput_schema on the Agent to validate the input. If you then pass the input as a dictionary, it will be automatically validated against the schema.
1
Validating the input example
validating_input_agent.py
2
Run the example
Install librariesExport your keyRun the example
Typesafe Agents
When you combine bothinput_schema and output_schema, you create a typesafe agent with end-to-end type safety - a fully validated data pipeline from input to output.
Complete Typesafe Research Agent
Here’s a comprehensive example showing a fully typesafe agent for research tasks:1
Create the typesafe research agent
typesafe_research_agent.py
2
Run the agent
Install librariesSet your API keyRun the agent
ResearchOutput object:
Using a Parser Model
You can use a different model to parse and structure the output from your primary model. This approach is particularly effective when the primary model is optimized for reasoning tasks, as such models may not consistently produce detailed structured responses.parser_model_prompt to your Parser Model to customize the model’s instructions.
Using an Output Model
You can use a different model to produce the run output of the agent. This is useful when the primary model is optimized for image analysis, for example, but you want a different model to produce a structured output response.output_model_prompt to your Output Model to customize the model’s instructions.
Developer Resources
- View the Agent schema
- View Cookbook