Structured Output With Tool Use
Search the web with a Mistral Medium agent and request a Person response.
Code
from agno.agent import Agent
from agno.models.mistral import MistralChat
from agno.tools.websearch import WebSearchTools
from pydantic import BaseModel
class Person(BaseModel):
name: str
description: str
model = MistralChat(
id="mistral-medium-latest",
temperature=0.0,
)
researcher = Agent(
name="Researcher",
model=model,
role="You find people with a specific role at a provided company.",
instructions=[
"- Search the web for the person described"
"- Find out if they have public contact details"
"- Return the information in a structured format"
],
tools=[WebSearchTools()],
output_schema=Person,
add_datetime_to_context=True,
)
researcher.print_response("Find information about Elon Musk")
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.
Usage
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateSet your API key
export MISTRAL_API_KEY=xxxInstall dependencies
uv pip install -U mistralai ddgs agnoRun Agent
Save the code above as structured_output_with_tool_use.py, then run:
python structured_output_with_tool_use.py