Base¶
base
¶
Pydantic-based Tool class with validation and schema generation.
This module provides the core Tool class and related base classes for defining tools with input/output validation and auto-generated JSON schemas for LLM function calling.
ToolInput
¶
Bases: BaseModel
Base class for tool input schemas.
Subclass this to define strict input validation for tools. The extra="forbid" config means unknown fields will be rejected.
Example
class MyToolInput(ToolInput): query: str = Field(..., description="Search query") limit: int = Field(default=10, description="Max results")
ToolOutput
¶
Bases: BaseModel
Base class for tool output schemas.
Provides a standardized output format with success status and optional error.
Example
class SearchOutput(ToolOutput): results: List[str] = Field(default_factory=list)
Tool
¶
Bases: BaseModel
Pydantic-based tool with validation and schema generation.
Provides: - Input validation via Pydantic schemas - Auto-generated JSON schemas for LLM function calling - Consistent error handling - Backward compatibility with existing @tool decorator - Support for both sync and async functions
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Unique tool identifier |
description |
str
|
What the tool does (used in LLM prompts) |
function |
Optional[Callable]
|
The callable to execute (excluded from serialization) |
input_schema |
Optional[Type[ToolInput]]
|
Optional Pydantic model for input validation |
output_schema |
Optional[Type[ToolOutput]]
|
Optional Pydantic model for output wrapping |
category |
str
|
Tool category for organization |
requires_confirmation |
bool
|
Whether to prompt user before execution |
cacheable |
bool
|
Whether results can be cached |
cache_ttl |
int
|
Cache time-to-live in seconds |
Example
From a decorated function¶
@tool(description="Search the web") async def web_search(query: str) -> str: return f"Results for {query}"
tool_instance = Tool(function=web_search)
With explicit schema¶
class SearchInput(ToolInput): query: str
tool_instance = Tool( name="web_search", description="Search the web", function=web_search, input_schema=SearchInput )
Initialize a Tool instance.
Can be initialized either with just a function (backward compatible) or with explicit parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Optional[Callable]
|
The function to wrap as a tool. If provided as positional arg, enables backward compatibility with old Tool(function) pattern. |
None
|
**data
|
Additional tool configuration (name, description, etc.) |
{}
|
Source code in reactive_agents/core/tools/base.py
tool_definition
property
¶
Generate OpenAI-compatible function schema for LLM tool calling.
Returns a schema in the format expected by OpenAI's function calling API: { "type": "function", "function": { "name": "tool_name", "description": "What the tool does", "parameters": { ... JSON Schema ... } } }
use
async
¶
Execute tool with input validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Dict[str, Any]
|
Parameters to pass to the tool function |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Tool execution result, or ToolOutput with error on failure |
Source code in reactive_agents/core/tools/base.py
from_function
classmethod
¶
from_function(function: Callable, name: Optional[str] = None, description: Optional[str] = None, input_schema: Optional[Type[ToolInput]] = None, output_schema: Optional[Type[ToolOutput]] = None, **kwargs) -> 'Tool'
Create a Tool from a function.
Factory method that extracts name and description from function metadata if not provided explicitly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable
|
The callable to wrap |
required |
name
|
Optional[str]
|
Override the tool name (defaults to function.name) |
None
|
description
|
Optional[str]
|
Override the description (defaults to docstring) |
None
|
input_schema
|
Optional[Type[ToolInput]]
|
Optional Pydantic model for input validation |
None
|
output_schema
|
Optional[Type[ToolOutput]]
|
Optional Pydantic model for output wrapping |
None
|
**kwargs
|
Additional tool configuration |
{}
|
Returns:
| Type | Description |
|---|---|
'Tool'
|
A configured Tool instance |
Example
async def my_tool(query: str) -> str: '''Search for something.''' return f"Results for {query}"
tool = Tool.from_function(my_tool)