Tools API Reference
Tools enable agents to execute Python functions and integrate external capabilities.FunctionTool
A tool that wraps a Python function to make it callable by AI models.Constructor
str
required
The name of the function.
str
default:""
A description of the function.
Literal['always_require', 'never_require'] | None
default:"never_require"
Whether approval is required to run this tool.
str | None
Optional provider-agnostic tool classification (e.g., “shell”).
int | None
Maximum number of times this function can be invoked across the lifetime of this tool instance. If None, there is no limit.Note: This counter lives on the tool instance and is never automatically reset. For per-request limits, use
FunctionInvocationConfiguration["max_function_calls"] instead.int | None
Maximum number of exceptions allowed during invocations. If None, there is no limit.
dict[str, Any] | None
Additional properties to set on the function.
Callable[..., Any] | None
The function to wrap. When None, creates a declaration-only tool that has no implementation.
type[BaseModel] | Mapping[str, Any] | None
The Pydantic model that defines the input parameters, or a JSON schema dictionary. If not provided and func is not None, it will be inferred from the function signature.
Callable[[Any], str] | None
Optional callable to convert raw function return value to string. When provided, overrides the default
parse_result() logic.Any
Additional keyword arguments.
Properties
bool
Indicates whether the function is declaration only (has no implementation).
int
Current number of times this function has been invoked.
int
Current number of exceptions that occurred during invocations.
Methods
invoke()
Run the function with provided arguments.BaseModel | Mapping[str, Any] | None
A mapping or model instance containing the arguments for the function.
Any
Keyword arguments to pass to the function (used if arguments is not provided).
str
The parsed result as a string - either plain text or serialized JSON.
parameters()
Get the JSON schema of the parameters.dict[str, Any]
A dictionary containing the JSON schema for the function’s parameters. The result is cached after the first call.
to_json_schema_spec()
Convert the FunctionTool to JSON Schema function specification format.dict[str, Any]
A dictionary containing the function specification in JSON Schema format.
parse_result() (static)
Convert a raw function return value to a string representation.Any
required
The raw return value from the wrapped function.
str
A string representation of the result, either plain text or serialized JSON.
Example: Direct Instantiation
Example: Declaration-Only Tool
@tool Decorator
Decorate a function to turn it into a FunctionTool.Usage
Parameters
Callable[..., Any] | None
The function to decorate. Enables decorator to be used with or without parentheses.
str | None
The name of the function. If not provided, uses the function’s
__name__.str | None
A description of the function. If not provided, uses the function’s docstring.
type[BaseModel] | Mapping[str, Any] | None
Explicit input schema for the function. Can be a Pydantic BaseModel subclass or a JSON schema dictionary. When provided, used instead of inferring from signature.
Literal['always_require', 'never_require'] | None
default:"never_require"
Whether approval is required to run this tool.
str | None
Optional provider-agnostic tool classification.
int | None
Maximum number of times this function can be invoked across the lifetime of this tool instance.
int | None
Maximum number of exceptions allowed during invocations.
dict[str, Any] | None
Additional properties to set on the function.
Callable[[Any], str] | None
Optional callable to override default result parsing.
FunctionTool | Callable[[Callable], FunctionTool]
The decorated function as a FunctionTool, or a decorator if called without a function.
Examples
Basic Usage
With Pydantic Field Annotations
Async Functions
Custom Name and Description
With Explicit Schema
With Approval Required
Function Invocation Configuration
Configuration for automatic function invocation in chat clients.Configuration Dictionary
bool
default:"True"
Master switch for the function invocation loop.
int
default:"40"
Limits the number of LLM roundtrips (iterations). Each iteration may execute one or more function calls in parallel.
int | None
default:"None"
Limits the total number of individual function invocations across all iterations within a single request.This is a best-effort limit checked after each batch of parallel tool calls completes. If the model requests 20 parallel calls in a single iteration and the limit is 10, all 20 will execute before the loop stops.
int
default:"3"
How many consecutive errors before abandoning the tool loop for this request.
bool
default:"False"
Whether to raise an error when the model requests a function not in the tool map.
Sequence[FunctionTool]
default:"[]"
Extra tools available during execution but not advertised to the model in the tool list.
bool
default:"False"
Whether to include exception details in the function result returned to the model.
Example
normalize_tools()
Normalize tool inputs while preserving non-callable tool objects.ToolTypes | Callable | Sequence[ToolTypes | Callable] | None
required
A single tool or sequence of tools.
list[ToolTypes]
A normalized list where callable inputs are converted to FunctionTool using the @tool decorator, and existing tool objects are passed through unchanged.