Tools#

Tools are the components that allow agents to perform actions. Any Python function can be converted into a tool using the @tool decorator.

Tool Class#

class brain.agents.tool.Tool#

Base class for a tool.

Inherit from this class to create a tool. Each tool must implement the execute and async_execute methods. Depending on the default_execute attribute in ToolConfig, the tool will be executed synchronously or asynchronously.

Synchronous tools may be executed in the asynchronous context, but not vice versa.

Show-inheritance:

classmethod format_traceback(e: Exception) str#

Format a traceback of an exception.

Tool Configuration#

class brain.agents.tool.ToolConfig(*, name: str, description: str, input_schema: Type[BaseModel] | Type[str] | str, stateful: bool = False, default_execute: Literal['sync', 'async'] = 'sync', strict: bool = True, requires_approval: bool = False)#

Configuration for a tool

Show-inheritance:

model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

name: str#

An identifier for the tool. It is advised to use a unique Python identifier

Example: `python ToolConfig(..., name="my_tool") `

description: str#

A description of the tool supplied to a LLM to help it understand the tool.

Example: `python ToolConfig(..., description="A tool to get the weather in a given city.") `

input_schema: Type[BaseModel] | Type[str] | str#

The schema of the input of the tool. If a string is provided, it is assumed to be a qualified name of a Pydantic model.

Example: ```python ToolConfig(…, input_schema=”my_package.my_module.MyInputSchema”)

If not provided, the input schema will be inferred from the parameters of the execute method or a function decorated with @tool. If cannot be inferred, assumed to be a string.

stateful: bool#

Whether the tool is stateful.

If true, the tool will be able to maintain state between calls, load and save state to a database. Stateful tools should implement load_state and save_state methods and declare state_schema.

Example: `python ToolConfig(..., stateful=True) `

default_execute: Literal['sync', 'async']#

The default execution mode of the tool.

If “sync”, the tool will be executed synchronously with execute. If “async”, the tool will be executed asynchronously with async_execute.

strict: bool#

Whether the tool should be strict about the input schema.

requires_approval: bool#

Whether the tool requires approval to be executed.

Example: `python ToolConfig(..., requires_approval=True) `

Function Tool#

class brain.agents.tool.FunctionTool(fn: Callable[[...], Any], output_transform: Callable[[Any], Any], bound: type[Any] | Any | None = None)#

A class-wrapper around a function that can be used as a tool.

Warning

The class-wrapper must be created with the tool decorator, not instantiated directly. Example: .. code-block:: python

@tool def my_tool(a: int, b: int) -> int:

return a + b

Warning

The function must be a top-level function, not a method of a class.

Show-inheritance:

Tool Decorator#

brain.agents.tool.tool(name: str | None = None, description: str | None = None, returns_context: bool = False, returns_content: bool = False, requires_approval: bool = False, strict: bool = True) Callable[[Callable[[...], Any]], FunctionTool[Any, Any, Any]]#

Convert a function into a tool.

The function may take any JSON-serializable arguments. They automatically will be converted to a Pydantic model with fields matching the function parameters.

The function’s return value will be converted to a Pydantic model with a single field content that contains the return value unless the return value is a Pydantic model.

Note

The function may be synchronous or asynchronous. Running asynchronous tools in the synchronous agent is not yet supported.

Parameters:
  • config – A ToolConfig object to configure the tool.

  • **kwargs – Additional keyword arguments to pass to the tool.

Returns:

A class-wrapper instance that can be used as a tool.

Example: .. code-block:: python

@tool def my_tool(a: int, b: int) -> int:

return a + b

agent = Agent(tools=[my_tool])

Output Model#

class brain.agents.tool.Output(*, data: Any | None = None, context: list[Any] = [], content: list[Any] = [])#
Show-inheritance:

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

State Context#