Skip to content

API Reference

AsyncToolbox

Bases: BaseToolbox[Awaitable[Union[str, dict[str, Any]]]]

execute_tool_calls(tool_calls) async

Execute multiple tool calls asynchronously and returns a result that can be used to respond to the OpenAI API.

Parameters:

Name Type Description Default
tool_calls list[ChatCompletionMessageToolCall]

List of tool calls from the OpenAI API to execute

required

Returns:

Type Description
list[ChatCompletionToolMessageParam]

List of tool messages containing the results of executing each tool call

Warning

If any individual tool call raises an uncaught exception, other pending tool calls will continue to run but may be left in an indeterminate state. To prevent this, catch exceptions within the tool handler functions themselves and return error messages as part of the normal return value.

get_func_arg_models()

Get Pydantic models for validating arguments of all functions in the toolbox.

Returns:

Type Description
dict[str, type[BaseModel]]

dict[str, type[BaseModel]]: Mapping of function names to their corresponding

dict[str, type[BaseModel]]

Pydantic models. Each model validates the arguments for that function.

get_schema()

Get OpenAI function schemas for all functions in the toolbox.

Returns:

Type Description
Sequence[ChatCompletionToolParam]

Sequence[ChatCompletionToolParam]: List of function schemas compatible with OpenAI's API.

Sequence[ChatCompletionToolParam]

Each schema describes the name, description and parameters of a function.

parse_invocations(tool_calls)

Parse a list of tool calls into a list of Invocation objects. Invocations can be executed by calling the execute method on them, giving you more fine-grained control over execution than the execute_tool_calls method.

Parameters:

Name Type Description Default
tool_calls list[ChatCompletionMessageToolCall]

List of tool calls from the OpenAI API to execute

required

Returns:

Type Description
list[Invocation[T]]

List of Invocation objects, one for each tool call

Toolbox

Bases: BaseToolbox[Union[str, dict[str, Any]]]

execute_tool_calls(tool_calls)

Execute tool calls and returns a result that can be used to respond to the OpenAI API. Note that this method is synchronous and will block until all tool calls are executed. For asynchronous execution, use the AsyncToolbox class.

Parameters:

Name Type Description Default
tool_calls list[ChatCompletionMessageToolCall]

List of tool calls from the OpenAI API to execute

required

Returns:

Type Description
list[ChatCompletionToolMessageParam]

List of tool messages containing the results of executing each tool call

Warning

Tool calls are executed in the order they are given. If a tool call raises an uncaught exception, the remaining tool calls will not be executed. To prevent this, catch exceptions within the tool handler functions themselves and return error messages as part of the normal return value.

get_func_arg_models()

Get Pydantic models for validating arguments of all functions in the toolbox.

Returns:

Type Description
dict[str, type[BaseModel]]

dict[str, type[BaseModel]]: Mapping of function names to their corresponding

dict[str, type[BaseModel]]

Pydantic models. Each model validates the arguments for that function.

get_schema()

Get OpenAI function schemas for all functions in the toolbox.

Returns:

Type Description
Sequence[ChatCompletionToolParam]

Sequence[ChatCompletionToolParam]: List of function schemas compatible with OpenAI's API.

Sequence[ChatCompletionToolParam]

Each schema describes the name, description and parameters of a function.

parse_invocations(tool_calls)

Parse a list of tool calls into a list of Invocation objects. Invocations can be executed by calling the execute method on them, giving you more fine-grained control over execution than the execute_tool_calls method.

Parameters:

Name Type Description Default
tool_calls list[ChatCompletionMessageToolCall]

List of tool calls from the OpenAI API to execute

required

Returns:

Type Description
list[Invocation[T]]

List of Invocation objects, one for each tool call

func_to_pydantic(func)

Convert a function's arguments to a Pydantic model. Used for input validation.

func_to_schema(fn)

Wraps a Python function to be compatible with OpenAI's function calling API.

Parameters:

Name Type Description Default
fn Callable[..., Any]

The Python function to wrap

required

Returns:

Name Type Description
dict ChatCompletionToolParam

Function schema compatible with OpenAI's API