{"record":{"id":"4d7b3a5ba177747c","repo":"assafelovic/gpt-researcher","slug":"serper-api-key-not-found-please-set-the-serper-ap","errorCode":null,"errorMessage":"Serper API key not found. Please set the SERPER_API_KEY environment variable. You can get a key at https://serper.dev/","messagePattern":"Serper API key not found\\. Please set the SERPER_API_KEY environment variable\\. You can get a key at https://serper\\.dev/","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"gpt_researcher/retrievers/serper/serper.py","lineNumber":53,"sourceCode":"        Returns:\n            list: List of sites to exclude\n        \"\"\"\n        exclude_sites_env = os.getenv(\"SERPER_EXCLUDE_SITES\", \"\")\n        if exclude_sites_env:\n            # Split by comma and strip whitespace\n            return [site.strip() for site in exclude_sites_env.split(\",\") if site.strip()]\n        return []\n\n    def get_api_key(self):\n        \"\"\"\n        Gets the Serper API key\n        Returns:\n\n        \"\"\"\n        try:\n            api_key = os.environ[\"SERPER_API_KEY\"]\n        except Exception:\n            raise Exception(\"Serper API key not found. Please set the SERPER_API_KEY environment variable. \"\n                            \"You can get a key at https://serper.dev/\")\n        return api_key\n\n    def search(self, max_results=7):\n        \"\"\"\n        Searches the query with optional country, language, and time filtering\n        Returns:\n            list: List of search results with title, href, and body\n        \"\"\"\n        print(\"Searching with query {0}...\".format(self.query))\n        \"\"\"Useful for general internet search queries using the Serper API.\"\"\"\n\n        # Search the query (see https://serper.dev/playground for the format)\n        url = \"https://google.serper.dev/search\"\n\n        headers = {\n            'X-API-KEY': self.api_key,\n            'Content-Type': 'application/json'","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/assafelovic/gpt-researcher/blob/6f998577d547b1e54ec662dac63583aa11e3b84b/gpt_researcher/retrievers/serper/serper.py#L35-L71","documentation":"The Serper retriever needs an API key to call serper.dev's search API. In __init__, get_api_key() looks up os.environ['SERPER_API_KEY'] and raises a generic Exception if the lookup fails. The error is purely environmental — no request is attempted without the key.","triggerScenarios":"Constructing the Serper retriever (e.g., selecting 'serper' as the retriever/search provider in GPT Researcher config) when SERPER_API_KEY is not present in the process environment; the bare except also swallows and converts any exception raised during the os.environ lookup.","commonSituations":"Key stored in .env but dotenv not loaded; CI/CD pipeline secrets not injected; shell session started before the variable was exported; confusing SERPER_API_KEY with SERPAPI_API_KEY when switching search providers.","solutions":["Export the variable: export SERPER_API_KEY='your-key' or add SERPER_API_KEY=... to your .env and load it before startup.","In Docker/CI, inject it as a secret/environment (-e SERPER_API_KEY=...).","Create a key at https://serper.dev if none exists.","Sanity-check with python -c \"import os; print(bool(os.environ.get('SERPER_API_KEY')))\"."],"exampleFix":"# before\nfrom gpt_researcher.retrievers.serper.serper import SerperSearch\ns = SerperSearch(query='news')  # raises: Serper API key not found\n\n# after\nimport os\nos.environ['SERPER_API_KEY'] = 'your-key'\ns = SerperSearch(query='news')","handlingStrategy":"validation","validationCode":"import os\n\nif not os.environ.get('SERPER_API_KEY'):\n    raise SystemExit('SERPER_API_KEY is not set — get a key at https://serper.dev')","typeGuard":null,"tryCatchPattern":"try:\n    searcher = SerperSearch(query='...')\nexcept Exception as e:\n    if 'API key not found' in str(e):\n        raise SystemExit(f'Fix configuration: {e}')\n    raise","preventionTips":["Validate all provider keys in a startup config check before research runs begin.","Keep provider-specific keys in .env and load it explicitly (dotenv.load_dotenv()).","Don't confuse SERPER_API_KEY with SERPAPI_API_KEY when switching providers.","Inject keys via CI/Docker secrets so every environment gets them."],"tags":["serper","api-key","environment-variable","configuration","retriever"],"backgroundTag":"missing-env-var","analyzedSha":"6f998577d547b1e54ec662dac63583aa11e3b84b","analyzedAt":"2026-08-28T17:50:07.383Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}