Typed Tools
~2 min read
Concept & How It Works
- Key points are in the visual diagram above.
Learn these elsewhere (not covered in depth here)
- →Dependencies
- →Structured Results
Why Does It Exist?
Stringly-typed tool calling is how SQL injection and silent None bugs land in agent stacks.
Real-World Analogy
A FastAPI route: you declare types, the framework parses, you never json.loads by hand.
Visual Workflows
What is Typed Tools?
Example
Scenario
get_order(order_id) returns Order. The model cannot pass {id: 1} if the field is order_id: str.
Solution
In PydanticAI, apply Typed Tools to this scenario: get_order(order_id) returns Order. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.
Practice Task
Do this before moving to the next module — reading alone is not enough.
Open the Code Walkthrough below and run it locally. Change one parameter related to Typed Tools (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.
Code Walkthrough
Highlighted lines show where Typed Tools happens in the code.
1from pydantic_ai import Agent, RunContext # import dependencies2from pydantic import BaseModel # import dependencies3
4class Order(BaseModel): # define a data structure or component5 id: str6 total: float7
8agent = Agent("openai:gpt-4o-mini")9
10@agent.tool11async def get_order(ctx: RunContext[None], order_id: str) -> Order:12 """Look up an order by id."""13 return db.get(order_id) # return the resultCommands to Remember
Commands to Remember
@agent.tool + typesDocstring = tool descriptionReturn a model if you canNo raw JSON in the handler
Common Mistakes
- Untyped **kwargs tools
- Doing business logic in the prompt
Cheat Sheet
Quick recap — the most important points from this module.
Cheat Sheet
quick ref- •Types at the boundary
- •RunContext for deps
- •Small tools
- •Validate then execute
