{"record":{"id":"2c4b3258b0e9e229","repo":"crewAIInc/crewAI","slug":"brave-api-key-environment-variable-is-required-for","errorCode":null,"errorMessage":"BRAVE_API_KEY environment variable is required for BraveSearchTool","messagePattern":"BRAVE_API_KEY environment variable is required for BraveSearchTool","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"lib/crewai-tools/src/crewai_tools/tools/brave_search_tool/brave_search_tool.py","lineNumber":56,"sourceCode":"    n_results: int = 10\n    save_file: bool = False\n    env_vars: list[EnvVar] = Field(\n        default_factory=lambda: [\n            EnvVar(\n                name=\"BRAVE_API_KEY\",\n                description=\"API key for Brave Search\",\n                required=True,\n            ),\n        ]\n    )\n    # Rate limiting parameters\n    _last_request_time: ClassVar[float] = 0\n    _min_request_interval: ClassVar[float] = 1.0  # seconds\n\n    def __init__(self, *args: Any, **kwargs: Any) -> None:\n        super().__init__(*args, **kwargs)\n        if \"BRAVE_API_KEY\" not in os.environ:\n            raise ValueError(\n                \"BRAVE_API_KEY environment variable is required for BraveSearchTool\"\n            )\n\n    def _run(\n        self,\n        **kwargs: Any,\n    ) -> Any:\n        current_time = time.time()\n        if (current_time - self._last_request_time) < self._min_request_interval:\n            time.sleep(\n                self._min_request_interval - (current_time - self._last_request_time)\n            )\n        BraveSearchTool._last_request_time = time.time()\n\n        try:\n            # Fallback to \"query\" or \"search_query\" for backwards compatibility\n            query = kwargs.get(\"q\") or kwargs.get(\"query\") or kwargs.get(\"search_query\")\n            if not query:","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/brave_search_tool/brave_search_tool.py#L38-L74","documentation":"BraveSearchTool.__init__ raises ValueError immediately at construction time if the BRAVE_API_KEY environment variable is not set. The key cannot be passed as a constructor argument (it is only read from the environment), so instantiation without prior env setup always fails.","triggerScenarios":"Instantiating BraveSearchTool() in a shell/process where BRAVE_API_KEY was never exported; setting the variable after the tool was constructed; running in CI, Docker, or a notebook where the .env file was not loaded before import/instantiation.","commonSituations":"Missing export in shell profile, .env not loaded (python-dotenv load_dotenv() called too late or not at all), secret configured only in a different environment (local vs deploy), case-sensitivity mistakes like BRAVE_APIKEY.","solutions":["export BRAVE_API_KEY=<your key> before starting the process (get one at bravesearch api portal).","In Python, call load_dotenv() before constructing the tool: from dotenv import load_dotenv; load_dotenv().","For Docker/CI, inject the variable via -e BRAVE_API_KEY=... or the platform's secret manager.","Verify with 'echo $BRAVE_API_KEY' (or os.environ check) in the same process that builds the tool."],"exampleFix":"# before\ntool = BraveSearchTool()  # ValueError: BRAVE_API_KEY environment variable is required\n\n# after\nimport os\nfrom dotenv import load_dotenv\nfrom crewai_tools.tools.brave_search_tool import BraveSearchTool\n\nload_dotenv()\nassert os.getenv('BRAVE_API_KEY'), 'set BRAVE_API_KEY first'\ntool = BraveSearchTool()","handlingStrategy":"validation","validationCode":"import os\n\nif not os.getenv(\"BRAVE_API_KEY\"):\n    raise SystemExit(\"Set BRAVE_API_KEY before creating BraveSearchTool\")","typeGuard":null,"tryCatchPattern":"try:\n    tool = BraveSearchTool()\nexcept ValueError as e:\n    if \"BRAVE_API_KEY\" in str(e):\n        load_dotenv()\n        tool = BraveSearchTool()  # retry once after loading .env\n    else:\n        raise","preventionTips":["Call load_dotenv() once at process startup, before any tool construction.","Add a startup assertion for required env vars so failures surface at boot, not mid-task.","Keep a bootstrap checklist of required env vars per tool in your project README."],"tags":["brave-search","environment-variables","api-key","configuration"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}