{"record":{"id":"27a2a52bdcbfe7eb","repo":"BerriAI/litellm","slug":"google-api-key-is-required-set-google-api-key-or","errorCode":null,"errorMessage":"Google API key is required. Set GOOGLE_API_KEY or GEMINI_API_KEY, or pass api_key.","messagePattern":"Google API key is required\\. Set GOOGLE_API_KEY or GEMINI_API_KEY, or pass api_key\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"litellm/llms/gemini/agents/transformation.py","lineNumber":116,"sourceCode":"    ) -> dict[str, str]:\n        headers = dict(headers)\n        headers[\"Content-Type\"] = \"application/json\"\n        explicit_api_key: Final = litellm_params.get(\"api_key\")\n        # SECURITY: when the caller overrides ``api_base``, refuse to fall back\n        # to the process-wide GOOGLE_API_KEY / GEMINI_API_KEY env vars. Otherwise\n        # an authenticated proxy user could set ``api_base`` to an attacker-\n        # controlled host and have the proxy ship its shared Gemini key in the\n        # ``x-goog-api-key`` header.\n        if litellm_params.get(\"api_base\") and not explicit_api_key:\n            raise ValueError(\n                \"When overriding api_base for Gemini agents, you must also \"\n                \"supply an explicit api_key. Falling back to GOOGLE_API_KEY / \"\n                \"GEMINI_API_KEY env vars with a custom api_base is refused \"\n                \"to prevent leaking the shared provider key to arbitrary hosts.\"\n            )\n        api_key: Final = GeminiModelInfo.get_api_key(explicit_api_key)\n        if not api_key:\n            raise ValueError(\"Google API key is required. Set GOOGLE_API_KEY or GEMINI_API_KEY, or pass api_key.\")\n        headers[\"x-goog-api-key\"] = api_key\n        return headers\n\n    def _raise_for_status(self, raw_response: httpx.Response) -> None:\n        if not (200 <= raw_response.status_code < 300):\n            raise GeminiError(\n                message=raw_response.text,\n                status_code=raw_response.status_code,\n                headers=dict(raw_response.headers),\n            )\n\n    # ------------------------------------------------------------------ #\n    # CREATE                                                               #\n    # ------------------------------------------------------------------ #\n\n    def transform_create_request(\n        self,\n        name: str,","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/gemini/agents/transformation.py#L98-L134","documentation":"When building headers for a Gemini agents request, litellm resolves the key via GeminiModelInfo.get_api_key(): an explicit litellm_params api_key first, then GOOGLE_API_KEY, then GEMINI_API_KEY. If nothing resolves it raises this ValueError naming all three ways to supply the key; the request never leaves the process.","triggerScenarios":"Invoking Gemini agents (create/agent endpoints) with none of api_key param, GOOGLE_API_KEY, or GEMINI_API_KEY set — e.g. fresh environment, or the variables live only in a different shell/deployment.","commonSituations":"New Gemini integration before any key is configured; env var present in dev shell but missing in Docker/CI; key stored under a different name (GOOGLE_API_KEY vs GEMINI_API_KEY confusion, or a GOOGLE_APPLICATION_CREDENTIALS file which is not consulted here).","solutions":["Export GOOGLE_API_KEY (or GEMINI_API_KEY) with an AI Studio key (AIza...).","Or pass api_key explicitly on the call / in litellm_params for the agent request.","In containers, confirm the variable is baked into the image or passed with -e; check for empty-string values, which also fail the truthiness check."],"exampleFix":"# before\nos.environ.pop(\"GOOGLE_API_KEY\", None)\nlitellm.agent_create(...)  # or agents completion path -> ValueError\n\n# after\nos.environ[\"GOOGLE_API_KEY\"] = \"AIza...\"\nlitellm.agent_create(...)","handlingStrategy":"validation","validationCode":"import os\n\ndef resolve_gemini_key() -> str:\n    key = os.getenv(\"GOOGLE_API_KEY\") or os.getenv(\"GEMINI_API_KEY\")\n    if not key:\n        raise RuntimeError(\"Set GOOGLE_API_KEY or GEMINI_API_KEY for Gemini agents\")\n    return key","typeGuard":null,"tryCatchPattern":"try:\n    litellm.agent_create(...)\nexcept ValueError as e:\n    if \"Google API key is required\" in str(e):\n        raise RuntimeError(\"Gemini credentials missing in this environment\") from e\n    raise","preventionTips":["Check both GOOGLE_API_KEY and GEMINI_API_KEY in startup health checks for services using Gemini agents.","Pass api_key explicitly via litellm_params in multi-key setups.","Note that GOOGLE_APPLICATION_CREDENTIALS (ADC) does not satisfy this check — an AI Studio API key is required."],"tags":["gemini","authentication","configuration","environment-variables","agents"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}