{"record":{"id":"fde6e26222520095","repo":"assafelovic/gpt-researcher","slug":"brave-search-api-key-not-found-please-set-the-bra","errorCode":null,"errorMessage":"Brave Search API key not found. Please set the BRAVE_API_KEY environment variable.","messagePattern":"Brave Search API key not found\\. Please set the BRAVE_API_KEY environment variable\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"gpt_researcher/retrievers/brave/brave.py","lineNumber":35,"sourceCode":"        Initializes the BraveSearch object\n        Args:\n            query:\n        \"\"\"\n        self.query = query\n        self.query_domains = query_domains or None\n        self.api_key = self.get_api_key()\n        self.logger = logging.getLogger(__name__)\n\n    def get_api_key(self):\n        \"\"\"\n        Gets the Brave Search API key\n        Returns:\n\n        \"\"\"\n        try:\n            api_key = os.environ[\"BRAVE_API_KEY\"]\n        except Exception:\n            raise Exception(\n                \"Brave Search API key not found. Please set the BRAVE_API_KEY environment variable.\"\n            )\n        return api_key\n\n    def search(self, max_results=7) -> list[dict[str, str]]:\n        \"\"\"\n        Searches the query\n        Returns:\n\n        \"\"\"\n        print(\"Searching with query {0}...\".format(self.query))\n        \"\"\"Useful for general internet search queries using the Brave Search API.\"\"\"\n\n        url = \"https://api.search.brave.com/res/v1/web/search\"\n        headers = {\n            \"X-Subscription-Token\": self.api_key,\n            \"Accept\": \"application/json\",\n            \"Accept-Encoding\": \"gzip\",","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/assafelovic/gpt-researcher/blob/6f998577d547b1e54ec662dac63583aa11e3b84b/gpt_researcher/retrievers/brave/brave.py#L17-L53","documentation":"Exception raised by the Brave retriever when the BRAVE_API_KEY environment variable is not set; the os.environ lookup fails and the handler re-raises with this explanatory message. The Brave Search API requires this key for every request.","triggerScenarios":"Constructing the Brave retriever (its __init__ calls get_api_key()) without BRAVE_API_KEY present in the environment.","commonSituations":"Key not exported, .env not loaded in the runtime, or deploying to a server/container where the variable was never injected.","solutions":["export BRAVE_API_KEY=<key> (get one at api.search.brave.com).","Put BRAVE_API_KEY in .env and confirm it loads before creating the retriever.","Inject the variable in your deployment environment (Docker -e, k8s secret, CI variable)."],"exampleFix":"# before\nretriever = Brave(query=\"...\")\n\n# after\nimport os\nif not os.environ.get(\"BRAVE_API_KEY\"):\n    raise SystemExit(\"BRAVE_API_KEY missing\")\nretriever = Brave(query=\"...\")","handlingStrategy":"validation","validationCode":"import os\nif not os.environ.get(\"BRAVE_API_KEY\"):\n    raise SystemExit(\"BRAVE_API_KEY missing\")","typeGuard":"null","tryCatchPattern":"try:\n    retriever = Brave(query)\nexcept Exception as e:\n    if \"BRAVE_API_KEY\" in str(e):\n        logger.error(e); sys.exit(1)\n    raise","preventionTips":["Check env vars at startup.","Inject secrets via the platform's secret manager.","Keep a startup preflight that validates all configured retrievers' keys."],"tags":["brave","missing-env-var","api-key","retriever"],"backgroundTag":"missing-env-var","analyzedSha":"6f998577d547b1e54ec662dac63583aa11e3b84b","analyzedAt":"2026-08-28T17:50:07.383Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}