{"record":{"id":"bf0d1a04e9e45e4b","repo":"BerriAI/litellm","slug":"gemini-api-key-is-required-for-google-ai-studio-fi","errorCode":null,"errorMessage":"GEMINI_API_KEY is required for Google AI Studio file operations","messagePattern":"GEMINI_API_KEY is required for Google AI Studio file operations","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"litellm/llms/gemini/files/transformation.py","lineNumber":58,"sourceCode":"        return LlmProviders.GEMINI\n\n    def validate_environment(\n        self,\n        headers: dict[Any, Any],\n        model: str,\n        messages: list[AllMessageValues],\n        optional_params: dict[Any, Any],\n        litellm_params: dict[Any, Any],\n        api_key: str | None = None,\n        api_base: str | None = None,\n    ) -> dict[Any, Any]:\n        \"\"\"\n        Validate environment and add Gemini API key to headers.\n        Google AI Studio uses x-goog-api-key header for authentication.\n        \"\"\"\n        resolved_api_key: Final = self.get_api_key(api_key)\n        if not resolved_api_key:\n            raise ValueError(\"GEMINI_API_KEY is required for Google AI Studio file operations\")\n\n        headers[\"x-goog-api-key\"] = resolved_api_key\n        return headers\n\n    def get_complete_url(\n        self,\n        api_base: str | None,\n        api_key: str | None,\n        model: str,\n        optional_params: dict,\n        litellm_params: dict,\n        stream: bool | None = None,\n    ) -> str:\n        \"\"\"\n        OPTIONAL\n\n        Get the complete url for the request\n","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/gemini/files/transformation.py#L40-L76","documentation":"For Gemini Files API (Google AI Studio file upload), validate_environment() resolves the API key via self.get_api_key() (explicit argument then GOOGLE_API_KEY/GEMINI_API_KEY). If none is found it raises this ValueError — file upload/download calls are authenticated with x-goog-api-key and cannot proceed anonymously.","triggerScenarios":"Calling litellm.create_file(...) / file operations for gemini with no api_key argument and no GOOGLE_API_KEY or GEMINI_API_KEY in the environment.","commonSituations":"Adding multimodal file uploads to an app whose chat path used a different provider; key configured only for the completion client via Router but file calls run through module-level functions; empty-string env var.","solutions":["Export GOOGLE_API_KEY or GEMINI_API_KEY in the process performing file operations.","Or pass api_key explicitly to the file-creation call / litellm_params.","Verify the variable is non-empty in the exact runtime (container, worker, notebook) that executes the upload."],"exampleFix":"# before\nfile_obj = litellm.create_file(model=\"gemini/\", file=open(\"audio.mp3\", \"rb\"), purpose=\"user_data\")  # no key -> ValueError\n\n# after\nos.environ[\"GOOGLE_API_KEY\"] = \"AIza...\"\nfile_obj = litellm.create_file(model=\"gemini/\", file=open(\"audio.mp3\", \"rb\"), purpose=\"user_data\")","handlingStrategy":"validation","validationCode":"import os\n\ndef assert_gemini_files_ready() -> None:\n    if not (os.getenv(\"GOOGLE_API_KEY\") or os.getenv(\"GEMINI_API_KEY\")):\n        raise RuntimeError(\"Gemini file operations require GOOGLE_API_KEY/GEMINI_API_KEY\")","typeGuard":null,"tryCatchPattern":"try:\n    litellm.create_file(model=\"gemini/\", file=f, purpose=\"user_data\")\nexcept ValueError as e:\n    if \"GEMINI_API_KEY is required\" in str(e):\n        raise RuntimeError(\"Configure Gemini credentials for file uploads\") from e\n    raise","preventionTips":["Run one shared credentials bootstrap for all Gemini features (chat, files, agents).","Pass the key via litellm_params/api_key in server contexts where env vars are unreliable.","Add a tiny file-upload smoke test to CI for every provider you use files with."],"tags":["gemini","files","authentication","configuration"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}