PydanticAI Agent With Custom Tools
Introduction
In this post, I’ll walk you through how I built a simple Weather Assistant Agent using Python and the PydanticAI framework. This agent can check the temperature for a given city and provide friendly, practical clothing recommendations based on the weather. It’s a great example of how to combine AI with custom tools to deliver helpful, context-aware advice.
We’ll break down the implementation step by step, explain the code, and discuss how you can use and extend this agent for your own projects.
Project Overview
The goal was to create a command-line tool that:
- Accepts a city name as input
- Returns the current (mocked) temperature for that city
- Provides a clothing recommendation based on the temperature
- Uses OpenAI’s GPT-4 model for natural language interaction
To achieve this, I used the PydanticAI agent framework and defined two custom tools: one for fetching the temperature and another for generating clothing advice.
Code Walkthrough
1. Setting Up the Environment
First, I initialized a new Python environment and installed the required dependencies:
uv init
uv add pydantic-ai openai
2. Importing Dependencies
Here’s the list of imports used in the script:
from typing import Literal
from pydantic_ai import Agent, RunContext
Literal
is used for type hintsAgent
andRunContext
are core components from the PydanticAI framework
3. Creating the Weather Assistant Agent
The core of the project is the agent configuration:
agent = Agent(
'openai:gpt-4',
system_prompt=(
"You are a helpful weather assistant. You can check the temperature "
"and provide clothing recommendations based on the weather. "
"Always be friendly and concise in your responses."
),
)
- The agent uses OpenAI’s GPT-4 model
- The system prompt guides the agent to be helpful, friendly, and concise
4. Defining the Temperature Tool
@agent.tool_plain
def get_temperature(city: str) -> int:
mock_temperatures = {
"london": 15,
"paris": 20,
"berlin": 18,
"tokyo": 25,
}
return mock_temperatures.get(city.lower(), 20)
- This function simulates fetching the temperature for a given city
- It returns a mock temperature from a predefined dictionary, defaulting to 20°C if the city isn’t listed
5. Defining the Clothing Recommendation Tool
@agent.tool
def get_clothing_recommendation(ctx: RunContext[int], temperature: int) -> str:
if temperature < 10:
return "It's cold! Wear a heavy coat, scarf, and gloves."
elif temperature < 15:
return "It's cool. A light jacket or sweater would be good."
elif temperature < 20:
return "It's mild. A light sweater or long sleeves should be fine."
else:
return "It's warm! Short sleeves and light clothing are recommended."
- This function provides clothing advice based on the temperature
- It uses simple conditional logic to cover cold, cool, mild, and warm scenarios
6. Running the Agent
def main():
result = agent.run_sync("What should I wear in Paris today?")
print(result.output)
if __name__ == "__main__":
main()
- The
main
function runs the agent with a sample query and prints the response - This pattern ensures the script can be run directly from the command line
Practical Use Cases
- Personal weather assistant: Quickly get clothing advice for any city
- Learning project: Understand how to build and extend AI agents with custom tools
- Template for more complex assistants: Add real API calls, more cities, or additional advice types
Possible Extensions
- Integrate with a real weather API for live data
- Add support for more cities and languages
- Provide more detailed or personalized clothing recommendations
- Build a web or mobile interface for easier access
Conclusion
This Weather Assistant Agent shows how you can combine AI with custom logic to deliver practical, user-friendly advice. The PydanticAI framework makes it easy to define tools and guide the agent’s behavior. With a few tweaks, you can adapt this pattern for many other helpful assistants or automation tasks.