Vertex AI Structured Output
Request JSON from Claude on Vertex AI and validate it locally as a MovieScript.
The current Agno Vertex Claude adapter uses the schema to guide JSON output and parses the response locally. It does not send a native output schema to Vertex AI. A valid response becomes MovieScript; invalid JSON or missing required fields can leave a string.
output_schema describes the expected type. If parsing or validation fails, result.content can remain a string. Before accessing schema fields in a run result, use isinstance(result.content, YourSchema), replacing YourSchema with the class you passed as output_schema.
The source uses claude-sonnet-4@20250514, which Anthropic lists as deprecated on Google Cloud. Google's Sonnet 4 card does not establish a completed retirement. For a new deployment, choose a current model and its supported settings, such as Sonnet 4.6. Provider availability and lifecycle dates can differ from the direct Anthropic API.
"""
Vertexai Structured Output
==========================
Cookbook example for `vertexai/claude/structured_output.py`.
"""
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
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
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@20250514"),
description="You help people write movie scripts.",
output_schema=MovieScript,
)
# Get the response in a variable
run: RunOutput = movie_agent.run("New York")
pprint(run.content)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
# --- Sync + Streaming ---
movie_agent.print_response("New York", stream=True)Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno "anthropic[vertex]"Set up Google Cloud access
Use a project with billing and aiplatform.googleapis.com enabled, enable the chosen Claude model in Model Garden, and give your calling identity permission to use it. Follow Google's Claude setup guide. Install the Google Cloud CLI for the local sign-in command below.
Set the project and a location supported by your model. us-east5 is a supported example for Sonnet 4 and Sonnet 4.6; check the model's current availability before switching models or locations.
export ANTHROPIC_VERTEX_PROJECT_ID="your-project-id"
export CLOUD_ML_REGION="us-east5"Authenticate locally with Application Default Credentials:
gcloud auth application-default loginADC supplies credentials; it does not enable models or grant IAM permissions. These examples authenticate to Google Cloud without an Anthropic API key.
Run the example
Save the code above as structured_output.py, then run:
python structured_output.pyFull source: cookbook/90_models/vertexai/claude/structured_output.py