Implementation:CrewAIInc CrewAI SerpApi Base Tool
| Knowledge Sources | |
|---|---|
| Domains | Tools, Web_Search |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
SerpApiBaseTool provides the abstract base class for all SerpApi-based search tools, handling client initialization, API key management, and response field filtering.
Description
SerpApiBaseTool extends BaseTool and declares serpapi as a package dependency with SERPAPI_API_KEY as a required environment variable. On initialization, it imports the serpapi.Client (with interactive installation via click.confirm if the package is missing), reads the API key from the environment, validates its presence (raising ValueError if missing with a helpful link to the SerpApi key management page), and creates a serpapi.Client instance. The _omit_fields() utility method recursively traverses dict/list data structures and removes keys matching any of the provided regex patterns (compiled via re.compile().match()). This is used by subclasses to strip metadata, pagination, and internal SerpApi fields from search results before returning them to the agent.
Usage
This is a base class and is not used directly. It is extended by SerpApiGoogleSearchTool and SerpApiGoogleShoppingTool to provide specific search engine functionality while sharing the client setup, dependency management, and response sanitization logic.
Code Reference
Source Location
- Repository: CrewAI
- File: lib/crewai-tools/src/crewai_tools/tools/serpapi_tool/serpapi_base_tool.py
- Lines: 1-61
Signature
class SerpApiBaseTool(BaseTool):
package_dependencies: list[str] # ["serpapi"]
env_vars: list[EnvVar] # SERPAPI_API_KEY required
client: Any | None = None
def __init__(self, **kwargs)
def _omit_fields(self, data: dict | list, omit_patterns: list[str]) -> None
Import
from crewai_tools.tools.serpapi_tool.serpapi_base_tool import SerpApiBaseTool
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| data | dict or list | Yes | Data structure to filter (used internally by _omit_fields) |
| omit_patterns | list[str] | Yes | List of regex patterns for field names to remove (used internally by _omit_fields) |
Outputs
| Name | Type | Description |
|---|---|---|
| _omit_fields() | None | Mutates the input data in-place, removing fields matching the regex patterns |
Usage Examples
Basic Usage
# SerpApiBaseTool is a base class; use subclasses instead:
from crewai_tools import SerpApiGoogleSearchTool, SerpApiGoogleShoppingTool
# Google web search
search_tool = SerpApiGoogleSearchTool()
# Google shopping search
shopping_tool = SerpApiGoogleShoppingTool()