Agent with Structured Outputs
Return a validated Pydantic object from an Azure AI Foundry agent using output_schema.
Classic adapter: its SDK is retired. See the migration example.
Code
This classic adapter uses schema instructions and parses the response after generation. strict_output=True and False do not enable different provider enforcement modes in this Agent path. The returned content must be checked before use.
from typing import List
from agno.agent import Agent, RunOutput # noqa
from agno.models.azure import AzureAIFoundry
from pydantic import BaseModel, Field
from rich.pretty import pprint # noqa
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!"
)
# Classic adapter: schema instructions and post-response parsing.
agent = Agent(
model=AzureAIFoundry(id="gpt-4o"),
description="You write movie scripts.",
output_schema=MovieScript,
)
response = agent.run("New York")
if not isinstance(response.content, MovieScript):
raise ValueError("The model did not return a valid MovieScript")
pprint(response.content)
Usage
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateSet your API key
export AZURE_API_KEY=xxx
export AZURE_ENDPOINT=xxxInstall dependencies
uv pip install -U azure-ai-inference aiohttp agnoRun Agent
Save the code above as structured_output.py, then run:
python structured_output.py