Agent with Structured Outputs and Strict Tools

Validate both Claude tool-call inputs and the agent's final Pydantic response.

Set strict=True on a function and output_schema on the agent to constrain the tool call and final response separately.

Code

structured_output_strict_tools.py
from agno.agent import Agent
from agno.models.anthropic import Claude
from agno.tools import Function
from pydantic import BaseModel


class WeatherInfo(BaseModel):
    location: str
    temperature: float
    unit: str
    condition: str


def get_weather(location: str, unit: str) -> str:
    temp = 72 if unit == "fahrenheit" else 22
    symbol = "F" if unit == "fahrenheit" else "C"
    return f"Weather in {location}: {temp} degrees {symbol}, sunny"


weather_tool = Function(
    name="get_weather",
    description="Get current weather information for a location",
    parameters={
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA",
            },
            "unit": {
                "type": "string",
                "enum": ["celsius", "fahrenheit"],
                "description": "Temperature unit",
            },
        },
        "required": ["location", "unit"],
        "additionalProperties": False,
    },
    strict=True,
    entrypoint=get_weather,
)

agent = Agent(
    model=Claude(id="claude-sonnet-4-6"),
    tools=[weather_tool],
    output_schema=WeatherInfo,
    description="You help users get weather information.",
)

if __name__ == "__main__":
    agent.print_response("What's the weather like in San Francisco?")
ContractAgno settingAnthropic request shape
Tool name and argumentsFunction(strict=True)Top-level strict: true beside name, description, and input_schema
Final responseAgent(output_schema=WeatherInfo)JSON output schema

Both schemas must use Anthropic's supported JSON Schema subset. Agno adds additionalProperties: false and makes every strict function parameter required when it formats the tool schema. In Agno, defaulted and Optional function parameters remain required when strict=True because Function rewrites the schema's required list. This example therefore requires unit in both the function signature and the explicit schema.

Usage

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U "agno[anthropic]"

Export your Anthropic API key

export ANTHROPIC_API_KEY="your_anthropic_api_key_here"

Run the agent

Save the code above as structured_output_strict_tools.py, then run:

python structured_output_strict_tools.py

Developer Resources