{"record":{"id":"ecad7729d8f6cd16","repo":"crewAIInc/crewAI","slug":"tavily-async-client-is-not-initialized-ensure-ta-ecad77","errorCode":null,"errorMessage":"Tavily async client is not initialized. Ensure 'tavily-python' is installed and API key is set.","messagePattern":"Tavily async 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_search_tool/tavily_search_tool.py","lineNumber":220,"sourceCode":"                        )\n\n        return json.dumps(raw_results, indent=2)\n\n    async def _arun(\n        self,\n        query: str,\n    ) -> str:\n        \"\"\"Asynchronously performs a search using the Tavily API.\n        Content of each result is truncated to `max_content_length_per_result`.\n\n        Args:\n            query: The search query string.\n\n        Returns:\n            A JSON string containing the search results with truncated content.\n        \"\"\"\n        if not self.async_client:\n            raise ValueError(\n                \"Tavily async client is not initialized. Ensure 'tavily-python' is installed and API key is set.\"\n            )\n\n        raw_results = await self.async_client.search(\n            query=query,\n            search_depth=self.search_depth,\n            topic=self.topic,\n            time_range=self.time_range,\n            days=self.days,\n            max_results=self.max_results,\n            include_domains=self.include_domains,\n            exclude_domains=self.exclude_domains,\n            include_answer=self.include_answer,\n            include_raw_content=self.include_raw_content,\n            include_images=self.include_images,\n            timeout=self.timeout,\n        )\n","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/tavily_search_tool/tavily_search_tool.py#L202-L238","documentation":"TavilySearchTool._run asynchronously (arun) guards on self.async_client. The AsyncTavilyClient is only constructed at init when tavily-python imported successfully and an API key (constructor arg or TAVILY_API_KEY) was present. If not, async_client is None and awaits fail with ValueError.","triggerScenarios":"Awaiting tool.arun(query) / tool._run(query) in async code when the tool was constructed without an API key or without tavily-python available, so the async client was never created.","commonSituations":"Async CrewAI crews where the .env was loaded after tool creation; deploying to an environment missing TAVILY_API_KEY; restarting after interactive install was skipped.","solutions":["Provide the key at construction (api_key=...) or export TAVILY_API_KEY before building the tool.","Confirm tavily-python is installed in the runtime environment and the app was restarted after installation.","Initialize environment config at process start (before any tool/crew construction)."],"exampleFix":"# before\nasync def main():\n    await tool.arun('query')  # async_client is None -> ValueError\n\n# after\nimport os\nos.environ.setdefault('TAVILY_API_KEY', 'tvly-...')  # or load_dotenv() first\ntool = TavilySearchTool()\nasync def main():\n    await tool.arun('query')","handlingStrategy":"validation","validationCode":"import os, importlib.util\nassert importlib.util.find_spec('tavily'), 'tavily-python not installed'\nassert os.getenv('TAVILY_API_KEY'), 'TAVILY_API_KEY not set'\n# async client will be constructed at init","typeGuard":"def tavily_async_ready(tool) -> bool:\n    return tool.async_client is not None","tryCatchPattern":"try:\n    result = await tool.arun(query)\nexcept ValueError as e:\n    if 'async client is not initialized' in str(e):\n        raise SystemExit(f'Fix TAVILY_API_KEY/install, then restart: {e}')\n    raise","preventionTips":["Initialize env/config once at process start, before async event loops create tools.","Assert tool.async_client is not None right after construction in async apps.","Include tavily-python in the deployment image, not installed at runtime."],"tags":["configuration","api-key","async","environment","tavily"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}