
How to Add Tools to a PydanticAI Agent in 10 Min
Your AI agent can answer questions, but it can't do anything. It can't check the weather, look up a user, or query a database. Without tools, it's just an expensive autocomplete. PydanticAI fixes this with one decorator. Here's how to give your agent two working tools in under 10 minutes. The Code import httpx from dataclasses import dataclass from pydantic import BaseModel from pydantic_ai import Agent , RunContext @dataclass class Deps : http_client : httpx . Client class CityWeather ( BaseModel ): city : str temperature_f : float summary : str recommendation : str agent = Agent ( " openai:gpt-4o-mini " , deps_type = Deps , output_type = CityWeather , instructions = " You help users check weather conditions. Use the tools provided to fetch real data before answering. " , ) @agent.tool def get_coordinates ( ctx : RunContext [ Deps ], city : str ) -> str : """ Get latitude and longitude for a city. """ response = ctx . deps . http_client . get ( " https://geocoding-api.open-meteo.com/v
Continue reading on Dev.to Tutorial
Opens in a new tab



