Skip to main content

Overview

The ClientTools class handles registration and execution of client-side tools that can be called by the conversational AI agent. It supports both synchronous and asynchronous tools and runs them in a dedicated event loop to ensure non-blocking operation.

Constructor

asyncio.AbstractEventLoop
Optional custom asyncio event loop to use for tool execution. If not provided, a new event loop will be created and run in a separate thread. Using a custom loop prevents “different event loop” runtime errors and allows for better context propagation and resource management.

Methods

start

Start the event loop in a separate thread for handling async operations. This is called automatically when creating a Conversation, but can be called manually if needed.

stop

Gracefully stop the event loop and clean up resources. This is called automatically when ending a Conversation.

register

Register a new tool that can be called by the AI agent.
str
required
Unique identifier for the tool. This name will be used by the agent to call the tool.
Union[Callable[[dict], Any], Callable[[dict], Awaitable[Any]]]
required
Function that implements the tool’s logic. Receives a dictionary of parameters and returns the tool result.
bool
Whether the handler is an async function. Defaults to False.
Raises:
  • ValueError if the handler is not callable
  • ValueError if a tool with the same name is already registered

handle

Execute a registered tool with the given parameters. This is an internal method typically called by the conversation handler.
str
required
The name of the tool to execute.
dict
required
Parameters to pass to the tool handler.
Returns: The result of the tool execution. Raises: ValueError if the tool is not registered.

execute_tool

Execute a tool and send its result via the provided callback. This method is non-blocking and handles both sync and async tools. This is an internal method used by the conversation handler.
str
required
The name of the tool to execute.
dict
required
Parameters to pass to the tool handler.
Callable[[dict], None]
required
Callback function to send the result to.
Raises: RuntimeError if the ClientTools event loop is not running.

Examples

Synchronous Tool

Asynchronous Tool

Multiple Tools

Using Custom Event Loop


Tool Handler Requirements

Parameter Format

Tool handlers receive parameters as a dictionary. The agent will call your tool with parameters based on the tool definition configured in your agent settings.

Return Values

Tools can return any JSON-serializable value:
  • Strings
  • Numbers
  • Dictionaries
  • Lists
  • Booleans
  • None
The return value will be sent back to the agent for processing.

Error Handling

If a tool raises an exception, it will be caught and the error message will be sent back to the agent:
The agent will receive an error response with the exception message.