Decorators¶
decorators
¶
Tool decorator for converting Python functions into LLM-callable tools.
This module provides the @tool decorator which adds metadata to functions that allows them to be used as tools in LLM function calling.
tool
¶
tool(description: Optional[str] = None, parameters: Optional[Dict[str, Any]] = None, category: str = 'general', requires_confirmation: bool = False, cacheable: bool = False, cache_ttl: int = 300)
A decorator to convert a Python function into a tool with LLM-callable metadata.
The decorated function will have a tool_definition attribute containing
an OpenAI-compatible function schema that can be used for LLM function calling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
description
|
Optional[str]
|
Optional. Description of the function's purpose. If not provided, extracted from the function's docstring. |
None
|
parameters
|
Optional[Dict[str, Any]]
|
Optional. JSON schema for function parameters. If not provided, inferred from the function's signature and type hints. |
None
|
category
|
str
|
Tool category for organization (default: "general") |
'general'
|
requires_confirmation
|
bool
|
Whether the tool requires user confirmation (default: False) |
False
|
cacheable
|
bool
|
Whether results can be cached (default: False) |
False
|
cache_ttl
|
int
|
Cache time-to-live in seconds (default: 300) |
300
|
Returns:
| Type | Description |
|---|---|
|
A decorator that adds tool metadata to the function. |
Example
@tool(description="Search the web for information") async def web_search(query: str, limit: int = 10) -> str: ''' Search the web.
Args:
query: The search query
limit: Maximum results to return
'''
return f"Results for {query}"
The function now has tool_definition attribute:¶
web_search.tool_definition = {¶
"type": "function",¶
"function": {¶
"name": "web_search",¶
"description": "Search the web for information",¶
"parameters": {¶
"type": "object",¶
"properties": {¶
"query": {"type": "string", "description": "The search query"},¶
"limit":¶
},¶
"required": ["query"]¶
}¶
}¶
}¶
Source code in reactive_agents/core/tools/decorators.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
create_tool_from_function
¶
create_tool_from_function(func: Callable, description: Optional[str] = None, parameters: Optional[Dict[str, Any]] = None, **kwargs) -> Tool
Create a Tool instance directly from a function.
This is a convenience function that combines the @tool decorator with Tool instantiation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
The function to convert to a tool |
required |
description
|
Optional[str]
|
Optional description override |
None
|
parameters
|
Optional[Dict[str, Any]]
|
Optional parameters schema override |
None
|
**kwargs
|
Additional Tool configuration (category, requires_confirmation, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
Tool
|
A configured Tool instance |
Example
async def my_search(query: str) -> str: return f"Results for {query}"
tool = create_tool_from_function( my_search, description="Search for things", category="search" )