{"record":{"id":"e828a041bb7b379c","repo":"BerriAI/litellm","slug":"api-key-is-required-e828a0","errorCode":null,"errorMessage":"api_key is required","messagePattern":"api_key is required","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"litellm/llms/gemini/files/transformation.py","lineNumber":87,"sourceCode":"        litellm_params: dict,\n        stream: bool | None = None,\n    ) -> str:\n        \"\"\"\n        OPTIONAL\n\n        Get the complete url for the request\n\n        Some providers need `model` in `api_base`\n        \"\"\"\n        endpoint: Final = \"upload/v1beta/files\"\n        api_base = self.get_api_base(api_base)\n        if not api_base:\n            raise ValueError(\"api_base is required\")\n\n        # Get API key from multiple sources\n        final_api_key: Final = api_key or litellm_params.get(\"api_key\") or self.get_api_key()\n        if not final_api_key:\n            raise ValueError(\"api_key is required\")\n\n        url: Final = f\"{api_base}/{endpoint}\"\n        return url\n\n    def get_supported_openai_params(self, model: str) -> list[OpenAICreateFileRequestOptionalParams]:\n        return []\n\n    def map_openai_params(\n        self,\n        non_default_params: dict,\n        optional_params: dict,\n        model: str,\n        drop_params: bool,\n    ) -> dict:\n        return optional_params\n\n    def transform_create_file_request(\n        self,","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/gemini/files/transformation.py#L69-L105","documentation":"In get_complete_url(), after api_base resolves, the Gemini Files config resolves the key from three sources in order: the explicit api_key argument, litellm_params['api_key'], then self.get_api_key() (GOOGLE_API_KEY/GEMINI_API_KEY env). If all three are empty it raises 'api_key is required' — the URL for the upload would otherwise be built with no way to authenticate it.","triggerScenarios":"Gemini file upload/download where neither the call nor the environment supplies a key: no api_key argument, no api_key in litellm_params, and GOOGLE_API_KEY/GEMINI_API_KEY unset.","commonSituations":"Same deployment gaps as other key errors: credentials loaded after import, missing in containers, or the file-upload feature added to a service that only configured a Vertex service account (not an AI Studio API key).","solutions":["Set GOOGLE_API_KEY (or GEMINI_API_KEY) before the file call.","Or pass api_key in litellm_params on the request so the third fallback succeeds.","Distinguish from Vertex: gemini/ file operations want an AI Studio API key, not a service-account JSON."],"exampleFix":"# before\nlitellm.create_file(model=\"gemini/\", file=f)  # no key anywhere -> ValueError\n\n# after\nlitellm.create_file(model=\"gemini/\", file=f, litellm_params={\"api_key\": \"AIza...\"})","handlingStrategy":"validation","validationCode":"import os\n\ndef gemini_files_key(litellm_params: dict | None = None) -> str:\n    key = (\n        (litellm_params or {}).get(\"api_key\")\n        or os.getenv(\"GOOGLE_API_KEY\")\n        or os.getenv(\"GEMINI_API_KEY\")\n    )\n    if not key:\n        raise RuntimeError(\"Gemini file operations need an API key\")\n    return key","typeGuard":null,"tryCatchPattern":"try:\n    litellm.create_file(model=\"gemini/\", file=f, purpose=\"user_data\")\nexcept ValueError as e:\n    if \"api_key is required\" in str(e):\n        raise RuntimeError(\"Missing Gemini key for file upload\") from e\n    raise","preventionTips":["Thread api_key through litellm_params in server apps instead of assuming env inheritance.","Distinguish key sources per feature in config: AI Studio API key for gemini/ files, service account for vertex_ai/.","Validate credentials once at startup for every provider feature you use (chat AND files AND rerank)."],"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"}