Example 5: Structured Output
Step 1: Create structured_ouput.py
from agno.agent import Agentfrom pydantic import BaseModel, Fieldfrom rich.pretty import pprint # noqafrom agno.models.ollama import Ollamafrom ollama import Client as OllamaClient
class MovieScript(BaseModel): setting: str = Field( ..., description="Provide a nice setting for a blockbuster movie." ) ending: str = Field( ..., description="Ending of the movie. If not available, provide a happy ending.", ) genre: str = Field( ..., description="Genre of the movie. If not available, select action, thriller or romantic comedy.", ) name: str = Field(..., description="Give a name to this movie") characters: list[str] = Field(..., description="Name of characters for this movie.") storyline: str = Field( ..., description="3 sentence storyline for the movie. Make it exciting!" )
# Agent that uses JSON modejson_mode_agent = Agent( # model=OpenAIChat(id="gpt-4o"), model=Ollama(id="llama3.2", client=OllamaClient()), description="You write movie scripts.", response_model=MovieScript,)
# Agent that uses structured outputsstructured_output_agent = Agent( # model=OpenAIChat(id="gpt-4o-2024-08-06"), model=Ollama(id="llama3.2", client=OllamaClient()), description="You write movie scripts.", response_model=MovieScript, structured_outputs=True,)
# Get the response in a variable# json_mode_response: RunResponse = json_mode_agent.run("New York")# pprint(json_mode_response.content)# structured_output_response: RunResponse = structured_ output_agent.run("New York")# pprint(structured_output_response.content)
json_mode_agent.print_response("New York")structured_output_agent.print_response("New York")
Expected Response
┏━ Message ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ ┃ ┃ New York ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┏━ Response (26.6s) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ ┃ ┃ { ┃ ┃ "setting": { ┃ ┃ "description": "Provide a nice setting for a blockbuster movie.", ┃ ┃ "type": "string" ┃ ┃ }, ┃ ┃ "ending": { ┃ ┃ "description": "Ending of the movie. If not available, provide a happy ending.", ┃ ┃ "type": "string" ┃ ┃ }, ┃ ┃ "genre": { ┃ ┃ "description": "Genre of the movie. If not available, select action, thriller or romantic comedy.", ┃ ┃ "type": "string" ┃ ┃ }, ┃ ┃ "name": { ┃ ┃ "description": "Give a name to this movie", ┃ ┃ "type": "string" ┃ ┃ }, ┃ ┃ "characters": { ┃ ┃ "description": "Name of characters for this movie.", ┃ ┃ "items": { ┃ ┃ "type": "string" ┃ ┃ }, ┃ ┃ "type": "array" ┃ ┃ }, ┃ ┃ "storyline": { ┃ ┃ "description": "3 sentence storyline for the movie. Make it exciting!", ┃ ┃ "type": "string" ┃ ┃ } ┃ ┃ } ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┏━ Message ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ ┃ ┃ New York ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┏━ Response (11.0s) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ ┃ ┃ { ┃ ┃ "setting": "A bustling New York City street, with towering skyscrapers and endless energy. The sound of car horns, chatter, and wailing sire ┃ ┃ "ending": "The sun sets over the Manhattan skyline, casting a golden glow over the city's concrete jungle as our protagonist walks alone int ┃ ┃ "genre": "Drama/Mystery", ┃ ┃ "name": "City of Secrets", ┃ ┃ "characters": [ ┃ ┃ "Jen", ┃ ┃ "Mike" ┃ ┃ ], ┃ ┃ "storyline": "A young journalist discovers a hidden world of corruption and deceit in New York City, leading her on a perilous journey to un ┃ ┃ } ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
....2 b continued....