Implementation:CrewAIInc CrewAI BaseTool Schema
Overview
Concrete abstract base class defining the tool interface with name, description, and Pydantic argument schema provided by the CrewAI framework.
Source Reference
- Repository: crewAIInc/crewAI
- File:
lib/crewai/src/crewai/tools/base_tool.py - Lines: L55-96
Signature
class BaseTool(BaseModel, ABC):
name: str
description: str
args_schema: type[BaseModel] = _ArgsSchemaPlaceholder
result_as_answer: bool = False
max_usage_count: int | None = None
cache_function: Callable[..., bool]
@abstractmethod
def _run(self, **kwargs) -> str: ...
Fields
| Field | Type | Default | Description |
|---|---|---|---|
name |
str |
(required) | Unique identifier for the tool, used by the LLM to select it |
description |
str |
(required) | Natural language description telling the LLM when and how to use the tool |
args_schema |
type[BaseModel] |
_ArgsSchemaPlaceholder |
Pydantic model defining accepted arguments with types and descriptions |
result_as_answer |
bool |
False |
If True, the tool's output is returned directly as the agent's final answer
|
max_usage_count |
None | None |
Maximum number of times this tool can be invoked per task execution; None means unlimited
|
cache_function |
Callable[..., bool] |
lambda returning True | Function that determines whether a tool result should be cached |
Import
from crewai.tools import BaseTool
How It Works
BaseTool inherits from both Pydantic's BaseModel and Python's ABC (Abstract Base Class). This combination provides:
- Schema validation via Pydantic -- all fields are type-checked and validated at instantiation time.
- Abstract interface via ABC -- subclasses must implement the
_run()method, which contains the actual tool logic.
The args_schema field defaults to an internal _ArgsSchemaPlaceholder class. When a subclass does not explicitly set args_schema, the framework introspects the _run() method's signature and generates a schema automatically. When a custom schema is provided, it takes precedence.
The framework serializes the tool specification (name, description, and args schema) into the format expected by the LLM's function-calling interface, enabling the LLM to select and invoke tools with correctly typed arguments.
Example
from pydantic import BaseModel, Field
from crewai.tools import BaseTool
class SearchArgs(BaseModel):
"""Arguments for the web search tool."""
query: str = Field(description="The search query string")
max_results: int = Field(default=5, description="Maximum number of results to return")
class WebSearchTool(BaseTool):
name: str = "search_web"
description: str = (
"Search the web for current information about a topic. "
"Use this when the agent needs up-to-date information "
"that may not be in its training data."
)
args_schema: type[BaseModel] = SearchArgs
def _run(self, query: str, max_results: int = 5) -> str:
# Implementation of the actual search logic
results = perform_search(query, max_results)
return "\n".join(results)
# Instantiate and use the tool
search_tool = WebSearchTool()
In this example:
SearchArgsdefines the typed argument schema with field descriptions.WebSearchToolsubclassesBaseTooland providesname,description, andargs_schema.- The
_run()method contains the actual tool logic. - The LLM sees the name, description, and schema when selecting tools.
Principle Link
Principle:CrewAIInc_CrewAI_Tool_Design
See Also
- Implementation:CrewAIInc_CrewAI_Tool_Decorator_And_Classes -- Alternative ways to create tools (decorator, factory)
- Implementation:CrewAIInc_CrewAI_Tool_Assignment_Config -- How tools are assigned to agents and tasks
- Environment:CrewAIInc_CrewAI_Python_Runtime_Environment