The AI SDK provides a lightweight tool calling system that allows you to define functions that can be called by language models. Tools couple a JSON schema (name, description, parameters) with a Python handler function, enabling the model to execute custom logic.
from ai_sdk import generate_text, openaimodel = openai("gpt-4o-mini")result = generate_text( model=model, prompt="What is 15 + 27?", tools=[add_numbers])print(result.text) # "The result is 42."
class WeatherParams(BaseModel): city: str = Field(description="The city to get weather for") units: str = Field(default="celsius", description="Temperature units (celsius/fahrenheit)")
class AddNumbersParams(BaseModel): a: float = Field(description="First number") b: float = Field(description="Second number")@tool( name="add_numbers", description="Add two numbers", parameters=AddNumbersParams)def add_numbers(a: float, b: float) -> float: return a + b
Tools provide a powerful way to extend AI model capabilities with custom logic. Use Pydantic
models for the best developer experience and type safety.
All tools are automatically validated and converted to the appropriate format for each provider.
The SDK handles the complexity of provider-specific implementations.
While JSON schema is still supported for backward compatibility, Pydantic models are recommended
for new development due to their superior type safety and validation capabilities.