Using custom tools with Python functions
In this section, let’s explore how to define and configure custom tools using Python functions within OpenAI Agents SDK. As a reminder, tools are a core part of the agent’s capabilities as they enable the agent to go beyond its internal knowledge and perform tasks such as fetching data, processing user requests, or triggering actions. We’ll begin with simple tool definitions using the @function_tool
decorator and then progress to more advanced capabilities, such as overriding tool parameters and using Pydantic
models for complex input validation.
Defining a new tool
As we discussed in the previous chapter, OpenAI Agents SDK provides a @function_tool
decorator that turns any Python function into a tool that can be called by an agent. For example, we will bring back the code that we wrote in the previous chapter that creates a simple tool and calls an agent with that tool:
# Required imports
import os
from dotenv...