🔧 You are viewing: API Specification - Complete technical reference for the @tool decoratorSee also: Conceptual Guide
- Component: Tool Decorator
- Module:
gaia.agents.base.tools - Import:
from gaia.agents.base.tools import tool - Source:
src/gaia/agents/base/tools.py
Overview
The@tool decorator is a simple, powerful mechanism for registering Python functions as agent tools. It automatically extracts function signatures, type annotations, and docstrings to create tool definitions that LLMs can understand and use.
What it does:
- Registers functions in the global tool registry
- Extracts function signature and parameters
- Infers parameter types from type annotations
- Captures docstrings for tool descriptions
- Generates JSON schema for LLM function calling
- Supports both
@tooland@tool()syntax
- Minimal boilerplate (single decorator)
- Automatic type inference from Python annotations
- Clear separation between tool definition and implementation
- Compatible with standard Python typing
Purpose and Use Cases
When to Use
-
Registering Agent Tools
- Every function you want the agent to call
- Tools that perform actions (database queries, file operations, API calls)
- Tools that retrieve information (search, list, get status)
-
Simple Tool Definition
- Quick tool prototyping
- Small to medium agents with 3-20 tools
- Standard Python function signatures
-
Type-Safe Tools
- Tools with clear parameter types
- Tools requiring type validation
- Tools used by multiple agents
When NOT to Use
- Functions not meant to be called by the agent
- Internal helper methods
- Methods that require agent instance state (use instance methods in
_register_tools()instead)
API Specification
Decorator Definition
Tool Registry
Type Mapping
Code Examples
Example 1: Basic Tool
Example 2: Tool with Optional Parameters
Example 3: Tool with Complex Types
Example 4: Tool in Agent Context
Example 5: Both Decorator Syntaxes
Implementation Details
Type Inference
Docstring Parsing
Registry Management
Testing Requirements
Unit Tests
File:tests/agents/base/test_tools.py
Dependencies
Required Packages
No External Dependencies
The tool decorator uses only Python standard library.Acceptance Criteria
-
@tooldecorator implemented - Type inference working for all Python basic types
- Required vs optional parameter detection working
- Docstring extraction working
- Both
@tooland@tool()syntax working - Decorated functions remain callable
- Registry properly populated
- All unit tests pass
- Works with instance methods in
_register_tools() - Documented in SDK.md
Best Practices
Good Tool Docstrings
Clear Parameter Names
Consistent Return Types
Limitations
No Advanced Type Hints
No Parameter Validation
Future Enhancements
- Support for Union types
- Support for Optional types
- Support for List[Type], Dict[Key, Value]
- Pydantic model integration
- Parameter validation decorators
- Tool categories/tags
- Tool versioning
- Automatic example generation from docstring
@tool Decorator Technical Specification