{"record":{"id":"03cb045c838f726a","repo":"crewAIInc/crewAI","slug":"tavily-client-is-not-initialized-ensure-tavily-p-03cb04","errorCode":null,"errorMessage":"Tavily client is not initialized. Ensure 'tavily-python' is installed and API key is set.","messagePattern":"Tavily client is not initialized\\. Ensure 'tavily-python' is installed and API key is set\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/tavily_get_research_tool/tavily_get_research_tool.py","lineNumber":105,"sourceCode":"                        f\"Attempted to install 'tavily-python' but failed: {e}. \"\n                        \"Please install it manually to use the TavilyGetResearchTool.\"\n                    ) from e\n            else:\n                raise ImportError(\n                    \"The 'tavily-python' package is required to use the \"\n                    \"TavilyGetResearchTool. Please install it with: uv add tavily-python\"\n                )\n\n    @staticmethod\n    def _stringify_response(response: Any) -> str:\n        if isinstance(response, str):\n            return response\n        return json.dumps(response, indent=2)\n\n    def _run(self, request_id: str) -> str:\n        \"\"\"Synchronously retrieves Tavily research task status and results.\"\"\"\n        if not self._client:\n            raise ValueError(\n                \"Tavily client is not initialized. Ensure 'tavily-python' is \"\n                \"installed and API key is set.\"\n            )\n\n        return self._stringify_response(self._client.get_research(request_id))\n\n    async def _arun(self, request_id: str) -> str:\n        \"\"\"Asynchronously retrieves Tavily research task status and results.\"\"\"\n        if not self._async_client:\n            raise ValueError(\n                \"Tavily async client is not initialized. Ensure 'tavily-python' is \"\n                \"installed and API key is set.\"\n            )\n\n        return self._stringify_response(\n            await self._async_client.get_research(request_id)\n        )\n","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/tavily_get_research_tool/tavily_get_research_tool.py#L87-L123","documentation":"TavilyGetResearchTool._run raises this ValueError when self._client is None while trying to fetch a research task's status/results via client.get_research(request_id). _client is only built in __init__ when tavily-python is importable, so None means the package (or its client construction) was skipped.","triggerScenarios":"Calling tool.run(request_id)/_run on an instance created while tavily-python was missing, or where os.getenv(\"TAVILY_API_KEY\") paths never ran; the guard fires before any Tavily API call.","commonSituations":"Reusing a tool instance created before the dependency was installed; half-initialized tools after a swallowed __init__ error.","solutions":["Install tavily-python and instantiate a fresh TavilyGetResearchTool.","Verify TAVILY_API_KEY is set in the environment at construction time.","Check tool._client before calling run(); rebuild the tool if it is None."],"exampleFix":"# before\nstatus = tool._run(req_id)  # -> ValueError: Tavily client is not initialized\n\n# after\nif not tool._client:\n    tool = TavilyGetResearchTool()\nstatus = tool._run(req_id)","handlingStrategy":"type-guard","validationCode":"if not getattr(tool, \"_client\", None):\n    raise RuntimeError(\"TavilyGetResearchTool uninitialized; rebuild after installing tavily-python\")","typeGuard":"def get_research_ready(tool) -> bool:\n    \"\"\"True when the sync Tavily research client exists.\"\"\"\n    return getattr(tool, \"_client\", None) is not None","tryCatchPattern":"try:\n    out = tool._run(request_id)\nexcept ValueError as e:\n    if \"Tavily client is not initialized\" in str(e):\n        tool = TavilyGetResearchTool()\n        out = tool._run(request_id)\n    else:\n        raise","preventionTips":["Construct Tavily tools only after deps and TAVILY_API_KEY are verified.","Check _client/_async_client truthiness when wiring tools into agents.","Rebuild tool instances after dependency installation instead of reusing them."],"tags":["tavily","not-initialized","lifecycle","crewai-tools"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}