BerriAI/litellm · error · ValueError
When overriding api_base for Gemini agents, you must also…
Error message
When overriding api_base for Gemini agents, you must also supply an explicit api_key. Falling back to GOOGLE_API_KEY / GEMINI_API_KEY env vars with a custom api_base is refused to prevent leaking the shared provider key to arbitrary hosts.
What it means
Error "When overriding api_base for Gemini agents, you must also supply an explicit api_key. Falling back to GOOGLE_API_KEY / GEMINI_API_KEY env vars with a custom api_base is refused to prevent leaking the shared provider key to arbitrary hosts." thrown in BerriAI/litellm.
Solutions
- When overriding api_base for Gemini agents, also pass an explicit api_key; env fallback is refused to avoid leaking the shared key to arbitrary hosts.
When it happens
Trigger: Thrown at litellm/llms/gemini/agents/transformation.py:108 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/3bd86563583e1d24.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/llms/gemini/agents/transformation.py:108
litellm_params: dict[str, Any],
) -> str:
return f"{self._base_url(api_base)}/agents"
def validate_environment(
self,
headers: dict[str, str],
litellm_params: dict[str, Any],
) -> dict[str, str]:
headers = dict(headers)
headers["Content-Type"] = "application/json"
explicit_api_key: Final = litellm_params.get("api_key")
# SECURITY: when the caller overrides ``api_base``, refuse to fall back
# to the process-wide GOOGLE_API_KEY / GEMINI_API_KEY env vars. Otherwise
# an authenticated proxy user could set ``api_base`` to an attacker-
# controlled host and have the proxy ship its shared Gemini key in the
# ``x-goog-api-key`` header.
if litellm_params.get("api_base") and not explicit_api_key:
raise ValueError(
"When overriding api_base for Gemini agents, you must also "
"supply an explicit api_key. Falling back to GOOGLE_API_KEY / "
"GEMINI_API_KEY env vars with a custom api_base is refused "
"to prevent leaking the shared provider key to arbitrary hosts."
)
api_key: Final = GeminiModelInfo.get_api_key(explicit_api_key)
if not api_key:
raise ValueError("Google API key is required. Set GOOGLE_API_KEY or GEMINI_API_KEY, or pass api_key.")
headers["x-goog-api-key"] = api_key
return headers
def _raise_for_status(self, raw_response: httpx.Response) -> None:
if not (200 <= raw_response.status_code < 300):
raise GeminiError(
message=raw_response.text,
status_code=raw_response.status_code,
headers=dict(raw_response.headers),
)View on GitHub (pinned to 77b7c6c40c)