Agent with Structured Outputs
Return a validated Pydantic object from a Vertex AI Claude agent.
Set output_schema to supply schema instructions and parse the response into a Pydantic object. This adapter does not request provider-enforced JSON Schema; check the result type before using it.
Code
from typing import List
from agno.agent import Agent, RunOutput # noqa
from agno.models.vertexai.claude import Claude
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!"
)
movie_agent = Agent(
model=Claude(id="claude-sonnet-4-6"),
description="You help people write movie scripts.",
output_schema=MovieScript,
)
# Get the response in a variable
run: RunOutput = movie_agent.run("New York")
if not isinstance(run.content, MovieScript):
raise ValueError("The model did not return a valid MovieScript")
pprint(run.content)Usage
Complete the Vertex project, model enablement, permissions, and CLI prerequisites first. Use a region that serves Sonnet 4.6.
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateSet your environment variables
export CLOUD_ML_REGION="us-east5"
export GOOGLE_CLOUD_PROJECT=xxxAuthenticate your CLI session
gcloud auth application-default loginYou don't need to authenticate your CLI every time.
Install dependencies
uv pip install -U 'anthropic[vertex]' agnoRun Agent
Save the code above as structured_output.py, then run:
python structured_output.py