{"record":{"id":"767aa9ebef842ac8","repo":"crewAIInc/crewAI","slug":"openai-api-key-environment-variable-is-required-fo-767aa9","errorCode":null,"errorMessage":"OPENAI_API_KEY environment variable is required for WeaviateVectorSearchTool and it is mandatory to use the tool.","messagePattern":"OPENAI_API_KEY environment variable is required for WeaviateVectorSearchTool and it is mandatory to use the tool\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/weaviate_tool/vector_search.py","lineNumber":91,"sourceCode":"                required=True,\n            ),\n        ]\n    )\n    weaviate_cluster_url: str = Field(\n        ...,\n        description=\"The URL of the Weaviate cluster\",\n    )\n    weaviate_api_key: str = Field(\n        ...,\n        description=\"The API key for the Weaviate cluster\",\n    )\n\n    def __init__(self, **kwargs: Any) -> None:\n        super().__init__(**kwargs)\n        if WEAVIATE_AVAILABLE:\n            openai_api_key = os.environ.get(\"OPENAI_API_KEY\")\n            if not openai_api_key:\n                raise ValueError(\n                    \"OPENAI_API_KEY environment variable is required for WeaviateVectorSearchTool and it is mandatory to use the tool.\"\n                )\n            self.headers = {\"X-OpenAI-Api-Key\": openai_api_key}\n        else:\n            if click.confirm(\n                \"You are missing the 'weaviate-client' package. Would you like to install it?\"\n            ):\n                subprocess.run([\"uv\", \"pip\", \"install\", \"weaviate-client\"], check=True)  # noqa: S607\n\n            else:\n                raise ImportError(\n                    \"You are missing the 'weaviate-client' package. Would you like to install it?\"\n                )\n\n    def _run(self, query: str) -> str:\n        if not WEAVIATE_AVAILABLE:\n            raise ImportError(\n                \"You are missing the 'weaviate-client' package. Would you like to install it?\"","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/weaviate_tool/vector_search.py#L73-L109","documentation":"WeaviateVectorSearchTool embeds queries with OpenAI via Weaviate's vectorizer, which requires an OpenAI API key passed as the X-OpenAI-Api-Key header. At construction time (only when weaviate-client is importable) it reads OPENAI_API_KEY from the environment and raises ValueError if it is unset or empty.","triggerScenarios":"Instantiating WeaviateVectorSearchTool(weaviate_cluster_url=..., weaviate_api_key=...) without OPENAI_API_KEY in os.environ, or with it set to an empty string.","commonSituations":"Only configuring Weaviate credentials and forgetting the OpenAI key; .env loaded after tool construction; CI/production secrets not injected; key named differently (e.g., OPEN_AI_API_KEY).","solutions":["Export OPENAI_API_KEY before creating the tool: export OPENAI_API_KEY=sk-... or set it in your environment/secrets manager.","Call load_dotenv() at the very top of the entrypoint, before any tool is built.","Add a startup assertion for all three required vars (OPENAI_API_KEY, WEAVIATE_URL, WEAVIATE_API_KEY) to fail fast with a clear message."],"exampleFix":"# before\ntool = WeaviateVectorSearchTool(\n    weaviate_cluster_url='https://x.weaviate.network',\n    weaviate_api_key='...',\n)  # ValueError: OPENAI_API_KEY required\n\n# after\nimport os\nfrom dotenv import load_dotenv\nload_dotenv()\nassert os.getenv('OPENAI_API_KEY'), 'set OPENAI_API_KEY'\ntool = WeaviateVectorSearchTool(\n    weaviate_cluster_url='https://x.weaviate.network',\n    weaviate_api_key='...',\n)","handlingStrategy":"validation","validationCode":"import os\n\nmissing = [v for v in ('OPENAI_API_KEY',) if not os.environ.get(v)]\nif missing:\n    raise SystemExit(f'missing env vars: {missing}')\n# WeaviateVectorSearchTool can now be constructed safely","typeGuard":null,"tryCatchPattern":"try:\n    tool = WeaviateVectorSearchTool(\n        weaviate_cluster_url=URL, weaviate_api_key=WKEY\n    )\nexcept ValueError as e:\n    if 'OPENAI_API_KEY' in str(e):\n        raise SystemExit('Set OPENAI_API_KEY - required for embeddings')\n    raise","preventionTips":["Load .env at the very top of the entrypoint, before tool imports run.","Assert all required env vars (OPENAI, WEAVIATE) at startup with clear messages.","Manage secrets via the platform's secret store, not ad-hoc exports."],"tags":["configuration","api-key","environment","weaviate","openai"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}