{"record":{"id":"cc768b14bb39b469","repo":"crewAIInc/crewAI","slug":"serpapi-package-not-found-please-install-with","errorCode":null,"errorMessage":"`serpapi` package not found, please install with `uv add serpapi`","messagePattern":"`serpapi` package not found, please install with `uv add serpapi`","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/serpapi_tool/serpapi_base_tool.py","lineNumber":41,"sourceCode":"    client: Any | None = None\n\n    def __init__(self, **kwargs: Any) -> None:\n        super().__init__(**kwargs)\n\n        try:\n            from serpapi import Client\n        except ImportError:\n            import click\n\n            if click.confirm(\n                \"You are missing the 'serpapi' package. Would you like to install it?\"\n            ):\n                import subprocess\n\n                subprocess.run([\"uv\", \"add\", \"serpapi\"], check=True)  # noqa: S607\n                from serpapi import Client  # type: ignore[import-untyped]\n            else:\n                raise ImportError(\n                    \"`serpapi` package not found, please install with `uv add serpapi`\"\n                ) from None\n        api_key = os.getenv(\"SERPAPI_API_KEY\")\n        if not api_key:\n            raise ValueError(\n                \"Missing API key, you can get the key from https://serpapi.com/manage-api-key\"\n            )\n        self.client = Client(api_key=api_key)\n\n    def _omit_fields(\n        self, data: dict[str, Any] | list[Any], omit_patterns: list[str]\n    ) -> None:\n        if isinstance(data, dict):\n            for field in list(data.keys()):\n                if any(re.compile(p).match(field) for p in omit_patterns):\n                    data.pop(field, None)\n                else:\n                    if isinstance(data[field], (dict, list)):","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/serpapi_tool/serpapi_base_tool.py#L23-L59","documentation":"ImportError raised by the SerpApi base tool when the serpapi package cannot be imported and the interactive click.confirm prompt to install it (`uv add serpapi`) is declined or unavailable. The import is attempted lazily during tool initialization; on decline, the error includes the exact install command. It fires at construction time, before any search is executed.","triggerScenarios":"Instantiating a SerpApi-based tool (e.g. SerpApiGoogleSearchTool / SerpApiGoogleScholarTool) in an environment where `from serpapi import Client` fails and the install prompt is answered 'no' (or auto-declined in non-interactive CI/Docker).","commonSituations":"CI pipelines and containers missing the optional dependency; fresh environments where crewai-tools was installed without serpapi; headless runs where click.confirm has no TTY and cannot prompt.","solutions":["Install it beforehand: `uv add serpapi` (or pip install serpapi).","Bake the dependency into Docker/CI images so the prompt never appears.","Set SERPAPI_API_KEY too — the very next check raises if the key is missing."],"exampleFix":"# before (CI, no TTY)\ntool = SerpApiGoogleSearchTool()  # ImportError\n\n# after\n# CI step: uv add serpapi\n# env: SERPAPI_API_KEY=...\ntool = SerpApiGoogleSearchTool()","handlingStrategy":"validation","validationCode":"def serpapi_available() -> bool:\n    try:\n        from serpapi import Client  # noqa: F401\n        return True\n    except ImportError:\n        return False\n\nif not serpapi_available():\n    subprocess.run([\"uv\", \"add\", \"serpapi\"], check=True)","typeGuard":null,"tryCatchPattern":"try:\n    tool = SerpApiGoogleSearchTool()\nexcept ImportError as e:\n    if \"serpapi\" in str(e):\n        raise SystemExit(\"Run `uv add serpapi` before using SerpApi tools\") from e\n    raise","preventionTips":["Declare serpapi as a project dependency when using SerpApi tools.","Pre-check imports in CI so optional dependencies never surface as runtime prompts.","Configure SERPAPI_API_KEY together with the install step."],"tags":["dependency","import-error","serpapi","installation"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}