Structured AI Agents
Learn how to create AI agents that return structured, type-safe outputs using Pydantic models and JSON.
Quick Start
Install Package
First, install the PraisonAI Agents package:
Set API Key
Set your OpenAI API key as an environment variable in your terminal:
Create a file
Create a new file app.py
with the basic setup:
Start Agents
Type this in your terminal to run your agents:
Requirements
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key here. Use Other models using this guide.
- Basic understanding of Python and Pydantic
Understanding Structured Outputs
What are Structured Outputs?
Structured outputs allow you to:
- Define exact shape of data using Pydantic models
- Get type-safe, validated responses
- Choose between Pydantic objects or JSON
- Ensure consistent output format across agent responses
Features
Pydantic Models
Define exact data structures with validation.
JSON Output
Get structured JSON responses.
Type Safety
Ensure type-safe, validated outputs.
Format Options
Choose between Pydantic or JSON.
Multi-Agent Structured Analysis
Install Package
First, install the PraisonAI Agents package:
Set API Key
Set your OpenAI API key as an environment variable in your terminal:
Create a file
Create a new file app.py
with the basic setup:
Start Agents
Type this in your terminal to run your agents:
Configuration Options
Troubleshooting
Validation Errors
If model validation fails:
- Check data types match model
- Verify required fields
- Enable verbose mode for debugging
Output Format
If output format is incorrect:
- Verify model definition
- Check output_pydantic vs output_json
- Review field specifications
Next Steps
AutoAgents
Learn about automatically created and managed AI agents
Mini Agents
Explore lightweight, focused AI agents
For optimal results, ensure your Pydantic models accurately represent your desired output structure.
Was this page helpful?