BerriAI/litellm · error · ValueError
api_base is required
Error message
api_base is required
What it means
GeminiFilesConfig.get_complete_url() builds the resumable-upload URL {api_base}/upload/v1beta/files. It resolves api_base via self.get_api_base() (explicit argument, then GEMINI_API_BASE env); if the result is falsy it raises 'api_base is required' before constructing the URL.
Source
Thrown at litellm/llms/gemini/files/transformation.py:82
self,
api_base: str | None,
api_key: str | None,
model: str,
optional_params: dict,
litellm_params: dict,
stream: bool | None = None,
) -> str:
"""
OPTIONAL
Get the complete url for the request
Some providers need `model` in `api_base`
"""
endpoint: Final = "upload/v1beta/files"
api_base = self.get_api_base(api_base)
if not api_base:
raise ValueError("api_base is required")
# Get API key from multiple sources
final_api_key: Final = api_key or litellm_params.get("api_key") or self.get_api_key()
if not final_api_key:
raise ValueError("api_key is required")
url: Final = f"{api_base}/{endpoint}"
return url
def get_supported_openai_params(self, model: str) -> list[OpenAICreateFileRequestOptionalParams]:
return []
def map_openai_params(
self,
non_default_params: dict,
optional_params: dict,
model: str,
drop_params: bool,View on GitHub (pinned to 6c2dcb801b)
Solutions
- Export GEMINI_API_BASE=https://generativelanguage.googleapis.com (the standard AI Studio base).
- Or pass api_base explicitly when invoking the file operation.
- Audit code that forwards api_base for accidental empty-string values, which also fail the check.
Example fix
# before
os.environ.pop("GEMINI_API_BASE", None)
litellm.create_file(model="gemini/", file=f) # api_base unresolvable
# after
os.environ["GEMINI_API_BASE"] = "https://generativelanguage.googleapis.com"
litellm.create_file(model="gemini/", file=f) Defensive patterns
Strategy: validation
Validate before calling
import os
def gemini_files_base() -> str:
base = os.getenv("GEMINI_API_BASE") or "https://generativelanguage.googleapis.com"
if not base:
raise RuntimeError("GEMINI_API_BASE must be a non-empty URL")
return base Type guard
def is_valid_api_base(base: object) -> bool:
return isinstance(base, str) and base.strip() != "" Try / catch
try:
litellm.create_file(model="gemini/", file=f, purpose="user_data")
except ValueError as e:
if "api_base is required" in str(e):
os.environ["GEMINI_API_BASE"] = "https://generativelanguage.googleapis.com"
litellm.create_file(model="gemini/", file=f, purpose="user_data")
else:
raise Prevention
- Default GEMINI_API_BASE explicitly in your config layer rather than relying on library defaults.
- Validate forwarded api_base values (non-empty, https) in the call-construction layer.
- Never pass api_base='' — normalize empty strings to None/absent before calling litellm.
When it happens
Trigger: File operations invoked with api_base=None/'' and no GEMINI_API_BASE environment variable set (e.g. an environment where the default base was cleared or a custom integration forgot to pass it).
Common situations: Custom orchestration code that forwards api_base=None explicitly; GEMINI_API_BASE set to an empty string in .env; a test harness that strips env vars and calls the transformation directly.
Related errors
- api_base is required
- GEMINI_API_KEY is required for Google AI Studio file operati
- api_key is required
- LLM Router not initialized. Ensure models added to proxy.
- API base is required for OpenAI image variations
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/6ec175094a1b2c12.
Report an issue: GitHub.