Examples
Agent with Structured Outputs
Hackathon Resources
- Introduction
- Setup
- Examples
- Simple Text Agent
- Agent with Tools
- Agent with Knowledge
- Agent with Structured Outputs
- Research Agent
- YouTube Agent
- Image Input + Tools
- Image Generation using DALL-E
- Image to Structured Output
- Image Generate Audio
- Image Input + Output
- Image Transcription
- Image search using Giphy
- Audio Input
- Audio Input Output
- Audio Sentiment
- Audio Transcript
- Audio Multi Turn
- Audio Generate Podcast
- Video Input
- Video Generation with Models Lab
- Video Generation with Replicate
- Video Captions
- Video to Shorts
- Models
- Pre-built Replit Template
- 🏆 Prizes
Examples
Agent with Structured Outputs
This agent returns its response as a structured output (pydantic object).
structured_output.py
from typing import List
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from pydantic import BaseModel, Field
# Define a Pydantic model for the structured output
class MovieScript(BaseModel):
setting: str = Field(
...,
description="A richly detailed, atmospheric description of the movie's primary location and time period. Include sensory details and mood.",
)
genre: str = Field(
...,
description="The film's primary and secondary genres (e.g., 'Sci-fi Thriller', 'Romantic Comedy'). Should align with setting and tone.",
)
name: str = Field(
...,
description="An attention-grabbing, memorable title that captures the essence of the story and appeals to target audience.",
)
characters: List[str] = Field(
...,
description="4-6 main characters with distinctive names and brief role descriptions (e.g., 'Sarah Chen - brilliant quantum physicist with a dark secret').",
)
storyline: str = Field(
...,
description="A compelling three-sentence plot summary: Setup, Conflict, and Stakes. Hook readers with intrigue and emotion.",
)
# Agent that uses JSON mode
json_mode_agent = Agent(
model=OpenAIChat(id="gpt-4o"),
response_model=MovieScript,
)
# Agent that uses structured outputs
structured_output_agent = Agent(
model=OpenAIChat(id="gpt-4o"),
response_model=MovieScript,
structured_outputs=True,
)
if __name__ == "__main__":
json_mode_agent.print_response("Tokyo", stream=True)
structured_output_agent.print_response("Ancient Rome", stream=True)
Usage
1
Install libraries
pip install -U agno openai
2
Export API keys
export OPENAI_API_KEY=***
3
Run the agent
python structured_output.py
On this page