Tools
Tool
module-attribute
Tool = Union[
FunctionTool,
FileSearchTool,
WebSearchTool,
ComputerTool,
]
A tool that can be used in an agent.
FunctionTool
dataclass
A tool that wraps a function. In most cases, you should use the function_tool
helpers to
create a FunctionTool, as they let you easily wrap a Python function.
Source code in src/agents/tool.py
name
instance-attribute
The name of the tool, as shown to the LLM. Generally the name of the function.
params_json_schema
instance-attribute
The JSON schema for the tool's parameters.
on_invoke_tool
instance-attribute
on_invoke_tool: Callable[
[RunContextWrapper[Any], str], Awaitable[str]
]
A function that invokes the tool with the given context and parameters. The params passed are: 1. The tool run context. 2. The arguments from the LLM, as a JSON string.
You must return a string representation of the tool output. In case of errors, you can either raise an Exception (which will cause the run to fail) or return a string error message (which will be sent back to the LLM).
FileSearchTool
dataclass
A hosted tool that lets the LLM search through a vector store. Currently only supported with OpenAI models, using the Responses API.
Source code in src/agents/tool.py
vector_store_ids
instance-attribute
The IDs of the vector stores to search.
max_num_results
class-attribute
instance-attribute
The maximum number of results to return.
include_search_results
class-attribute
instance-attribute
Whether to include the search results in the output produced by the LLM.
ranking_options
class-attribute
instance-attribute
Ranking options for search.
WebSearchTool
dataclass
A hosted tool that lets the LLM search the web. Currently only supported with OpenAI models, using the Responses API.
Source code in src/agents/tool.py
user_location
class-attribute
instance-attribute
Optional location for the search. Lets you customize results to be relevant to a location.
ComputerTool
dataclass
A hosted tool that lets the LLM control a computer.
Source code in src/agents/tool.py
default_tool_error_function
default_tool_error_function(
ctx: RunContextWrapper[Any], error: Exception
) -> str
The default tool error function, which just returns a generic error message.
Source code in src/agents/tool.py
function_tool
function_tool(
func: ToolFunction[...],
*,
name_override: str | None = None,
description_override: str | None = None,
docstring_style: DocstringStyle | None = None,
use_docstring_info: bool = True,
failure_error_function: ToolErrorFunction | None = None,
) -> FunctionTool
function_tool(
*,
name_override: str | None = None,
description_override: str | None = None,
docstring_style: DocstringStyle | None = None,
use_docstring_info: bool = True,
failure_error_function: ToolErrorFunction | None = None,
) -> Callable[[ToolFunction[...]], FunctionTool]
function_tool(
func: ToolFunction[...] | None = None,
*,
name_override: str | None = None,
description_override: str | None = None,
docstring_style: DocstringStyle | None = None,
use_docstring_info: bool = True,
failure_error_function: ToolErrorFunction
| None = default_tool_error_function,
) -> (
FunctionTool
| Callable[[ToolFunction[...]], FunctionTool]
)
Decorator to create a FunctionTool from a function. By default, we will: 1. Parse the function signature to create a JSON schema for the tool's parameters. 2. Use the function's docstring to populate the tool's description. 3. Use the function's docstring to populate argument descriptions. The docstring style is detected automatically, but you can override it.
If the function takes a RunContextWrapper
as the first argument, it must match the
context type of the agent that uses the tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
ToolFunction[...] | None
|
The function to wrap. |
None
|
name_override
|
str | None
|
If provided, use this name for the tool instead of the function's name. |
None
|
description_override
|
str | None
|
If provided, use this description for the tool instead of the function's docstring. |
None
|
docstring_style
|
DocstringStyle | None
|
If provided, use this style for the tool's docstring. If not provided, we will attempt to auto-detect the style. |
None
|
use_docstring_info
|
bool
|
If True, use the function's docstring to populate the tool's description and argument descriptions. |
True
|
failure_error_function
|
ToolErrorFunction | None
|
If provided, use this function to generate an error message when the tool call fails. The error message is sent to the LLM. If you pass None, then no error message will be sent and instead an Exception will be raised. |
default_tool_error_function
|
Source code in src/agents/tool.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|