{"record":{"id":"be2cce007603359f","repo":"huggingface/smolagents","slug":"missing-api-key-make-sure-you-have-api-key-env","errorCode":null,"errorMessage":"Missing API key. Make sure you have '{api_key_env_name}' in your env variables.","messagePattern":"Missing API key\\. Make sure you have '(.+?)' in your env variables\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/smolagents/default_tools.py","lineNumber":188,"sourceCode":"            \"nullable\": True,\n        },\n    }\n    output_type = \"string\"\n\n    def __init__(self, provider: str = \"serpapi\"):\n        super().__init__()\n        import os\n\n        self.provider = provider\n        if provider == \"serpapi\":\n            self.organic_key = \"organic_results\"\n            api_key_env_name = \"SERPAPI_API_KEY\"\n        else:\n            self.organic_key = \"organic\"\n            api_key_env_name = \"SERPER_API_KEY\"\n        self.api_key = os.getenv(api_key_env_name)\n        if self.api_key is None:\n            raise ValueError(f\"Missing API key. Make sure you have '{api_key_env_name}' in your env variables.\")\n\n    def forward(self, query: str, filter_year: int | None = None) -> str:\n        import requests\n\n        if self.provider == \"serpapi\":\n            params = {\n                \"q\": query,\n                \"api_key\": self.api_key,\n                \"engine\": \"google\",\n                \"google_domain\": \"google.com\",\n            }\n            base_url = \"https://serpapi.com/search.json\"\n        else:\n            params = {\n                \"q\": query,\n                \"api_key\": self.api_key,\n            }\n            base_url = \"https://google.serper.dev/search\"","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/default_tools.py#L170-L206","documentation":"Raised in SearchEngineTool/__init__ (GoogleSearchTool-style provider tool) when the required API key environment variable (SERPAPI_API_KEY or SERPER_API_KEY, depending on provider) is not set. The tool reads the key at construction time and refuses to build without it.","triggerScenarios":"Instantiating the search tool with provider='serpapi' without SERPAPI_API_KEY in env, or provider='serper' without SERPER_API_KEY. Also when the env var is set in a shell but not in the process (e.g. different venv, docker, or cron).","commonSituations":"Missing .env loading, deploying to containers/cron where env vars aren't propagated, or using the wrong provider's key name.","solutions":["Set the correct env var for your provider: export SERPAPI_API_KEY=... or export SERPER_API_KEY=....","If using python-dotenv, ensure load_dotenv() runs before constructing the tool.","Verify with `env | grep -i serp` that the variable is actually in the running process."],"exampleFix":"# before\ntool = GoogleSearchTool(provider='serper')  # ValueError\n# after\nimport os; os.environ['SERPER_API_KEY'] = '...'\ntool = GoogleSearchTool(provider='serper')","handlingStrategy":"validation","validationCode":"import os\nneeded = {'serpapi':'SERPAPI_API_KEY','serper':'SERPER_API_KEY'}[provider]\nassert os.getenv(needed), f'set {needed}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Load .env before building tools.","Centralize required-env checks in a startup validation function."],"tags":["missing-env-var","api-key","search-tool","smolagents"],"backgroundTag":"missing-env-var","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}