Tools API
A tool is a Python callable the model can invoke. FunctionTool wraps a
function, a Pydantic model, or an OpenAI-style specification into one shape,
and ToolRegistry is the set of tools offered for a given request.
See the custom tools guide for how to write one, and tool adapters for exposing tools over a CLI or MCP.
FunctionTool
FunctionTool
FunctionTool(
function_tool: Union[
BaseModel, Callable, Tuple[Dict[str, Any], Callable]
],
pre_processors: Union[
BaseProcessor, List[BaseProcessor], None
] = None,
post_processors: Union[
BaseProcessor, List[BaseProcessor], None
] = None,
debug_mode: bool = False,
require_confirmation: bool = False,
confirmation_description: Optional[str] = None,
confirmation_handler: Optional[
Callable[[ConfirmationRequest], None]
] = None,
with_execution_context: bool = False,
**additional_parameters: Any
)
Class representing a function tool for a LLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function_tool
|
Union[BaseModel, Callable, Tuple[Dict[str, Any], Callable]]
|
The function tool, can be either a pydantic model with a run method, a python function or a tuple of a OpenAI tool specification and a function as callback. |
required |
pre_processors
|
Union[BaseProcessor, List[BaseProcessor], None]
|
Single preprocessor or list of preprocessors to apply before function execution |
None
|
post_processors
|
Union[BaseProcessor, List[BaseProcessor], None]
|
Single postprocessor or list of postprocessors to apply after function execution |
None
|
debug_mode
|
bool
|
Enable debug mode to print parameters |
False
|
require_confirmation
|
bool
|
Whether to require user confirmation before executing the tool |
False
|
confirmation_description
|
Optional[str]
|
Optional description for the confirmation request |
None
|
confirmation_handler
|
Optional[Callable[[ConfirmationRequest], None]]
|
Optional callable that handles confirmation requests |
None
|
with_execution_context
|
bool
|
Only applicable for pydantic tools. If True, the run method will be called with an additional parameter containing the execution context. |
False
|
**additional_parameters
|
Any
|
Additional parameters to pass to the call if function is called. |
{}
|
from_pydantic_model_and_callable
staticmethod
from_pydantic_model_and_callable(
pydantic_model: BaseModel, tool_function: Callable
) -> FunctionTool
Converts an OpenAI tool schema and a callable function into a FunctionTool Args: pydantic_model(BaseModel): Pydantic Model representing the arguments to the tool. tool_function(Callable): Callable function that will be invoked when the agent uses it and will be passed the fields of the pydantic model.
Returns:
| Name | Type | Description |
|---|---|---|
FunctionTool |
FunctionTool
|
The FunctionTool instance. |
from_openai_tool
staticmethod
from_openai_tool(
openai_tool_schema: dict, tool_function: Callable
) -> FunctionTool
Converts an OpenAI tool schema and a callable function into a FunctionTool Args: openai_tool_schema(dict): OpenAI tool description dictionary. tool_function(Callable): Callable function that will be invoked when the agent uses it.
Returns:
| Name | Type | Description |
|---|---|---|
FunctionTool |
FunctionTool
|
The FunctionTool instance. |
from_llama_index_tool
staticmethod
from_llama_index_tool(llama_index_tool) -> FunctionTool
Converts a llama-index tool into a FunctionTool Args: llama_index_tool(["BaseTool"]): OpenAI tool description dictionary.
Returns:
| Name | Type | Description |
|---|---|---|
FunctionTool |
FunctionTool
|
The FunctionTool instance. |
set_confirmation_handler
set_confirmation_handler(
handler: Callable[[ConfirmationRequest], None],
)
Set the handler for confirmation requests.
enable_confirmation
Enable confirmation for this tool.
execute
Execute the function tool with the given parameters. If require_confirmation is True, this method will block until confirmation is received.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
Dict[str, Any]
|
Input parameters for the function |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Processed result from the function execution |
execute_async
async
Asynchronously execute the function tool with the given parameters. If require_confirmation is True, this method will await confirmation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
Dict[str, Any]
|
Input parameters for the function |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Processed result from the function execution |
add_pre_processor
add_pre_processor(
processor: Union[PreProcessor, Callable],
position: int = None,
) -> None
Add a new preprocessor to the function tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
processor
|
Union[PreProcessor, Callable]
|
Preprocessor to add (either PreProcessor instance or callable) |
required |
position
|
int
|
Optional position to insert the processor (None = append to end) |
None
|
add_post_processor
add_post_processor(
processor: Union[PostProcessor, Callable],
position: int = None,
) -> None
Add a new postprocessor to the function tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
processor
|
Union[PostProcessor, Callable]
|
Postprocessor to add (either PostProcessor instance or callable) |
required |
position
|
int
|
Optional position to insert the processor (None = append to end) |
None
|
AsyncFunctionTool
AsyncFunctionTool(
function_tool: Union[
BaseModel, Callable, Tuple[Dict[str, Any], Callable]
],
pre_processors: Union[
BaseProcessor, List[BaseProcessor], None
] = None,
post_processors: Union[
BaseProcessor, List[BaseProcessor], None
] = None,
debug_mode: bool = False,
require_confirmation: bool = False,
confirmation_description: Optional[str] = None,
confirmation_handler: Optional[
Callable[[ConfirmationRequest], None]
] = None,
**additional_parameters
)
Bases: FunctionTool
execute_async
async
Asynchronously execute the function tool with the given parameters. If require_confirmation is True, this method will await confirmation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
Dict[str, Any]
|
Input parameters for the function |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Processed result from the function execution |
ToolRegistry
Pre- and post-processors
Processors run either side of a tool call: use them to normalize arguments before execution or to reshape results before they return to the model.
BaseProcessor
PreProcessor
Bases: BaseProcessor
Abstract base class for preprocessing parameters before function execution.
process
abstractmethod
Process the input parameters before function execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
dict[str, Any]
|
Dictionary of input parameters |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Processed parameters dictionary |
PostProcessor
Bases: BaseProcessor
Abstract base class for postprocessing function results.
Execution context and confirmation
ToolExecutionContext
ConfirmationRequest
ConfirmationState
Bases: Enum
Represents the state of a confirmation request.