BerriAI/litellm · error · ValueError

GOOGLE_PSE_API_KEY is not set. Set `GOOGLE_PSE_API_KEY` envi

Error message

GOOGLE_PSE_API_KEY is not set. Set `GOOGLE_PSE_API_KEY` environment variable.

What it means

Raised by the Google PSE (Programmable Search Engine) search provider during header setup when resolve_server_api_key cannot find a key: no caller api_key was passed, no GOOGLE_PSE_API_KEY env var is set (or it's empty), and no key source matched. Note the provider resolves the key host-aware against GOOGLE_PSE_API_BASE to avoid leaking a server-managed key to a caller-supplied host, so a mismatched api_base can also suppress key resolution.

Source

Thrown at litellm/llms/google_pse/search/transformation.py:92

        api_key: str | None = None,
        api_base: str | None = None,
        **kwargs,
    ) -> dict:
        """
        Validate environment and return headers.

        Google PSE uses API key as a query parameter, not in headers.
        This method is called but headers are not used for authentication.
        """
        api_key = self.resolve_server_api_key(
            caller_api_key=api_key,
            caller_api_base=api_base,
            key_env_vars=("GOOGLE_PSE_API_KEY",),
            base_env_var="GOOGLE_PSE_API_BASE",
            default_api_base=self.GOOGLE_PSE_API_BASE,
        )
        if not api_key:
            raise ValueError("GOOGLE_PSE_API_KEY is not set. Set `GOOGLE_PSE_API_KEY` environment variable.")

        # Also check for search engine ID
        search_engine_id: Final = kwargs.get("search_engine_id") or get_secret_str("GOOGLE_PSE_ENGINE_ID")
        if not search_engine_id:
            raise ValueError(
                "GOOGLE_PSE_ENGINE_ID is not set. Set `GOOGLE_PSE_ENGINE_ID` environment variable or pass `search_engine_id` parameter."
            )

        headers["Content-Type"] = "application/json"
        return headers

    def get_complete_url(
        self,
        api_base: str | None,
        optional_params: dict,
        data: dict | list[dict] | None = None,
        **kwargs,
    ) -> str:

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Export GOOGLE_PSE_API_KEY with a valid Google API key (created in Google Cloud Console -> Credentials, with Custom Search API enabled).
  2. Or pass api_key explicitly on the search call if per-request keys are preferred.
  3. If GOOGLE_PSE_API_BASE is set, ensure it is either the default (https://www.googleapis.com) or a host the server key may be sent to; remove a stray GOOGLE_PSE_API_BASE otherwise.
  4. Verify the key is non-empty: many secret managers inject empty strings that resolve as missing.

Example fix

# before: no key configured
os.environ.pop("GOOGLE_PSE_API_KEY", None)
results = litellm.web_search(provider="google_pse", query="hello")  # ValueError

# after: provision and export the key
os.environ["GOOGLE_PSE_API_KEY"] = "AIza..."
os.environ["GOOGLE_PSE_ENGINE_ID"] = "a1b2c3d4e5f6g7h8j"
results = litellm.web_search(provider="google_pse", query="hello")
Defensive patterns

Strategy: validation

Validate before calling

import os

missing = [v for v in ("GOOGLE_PSE_API_KEY", "GOOGLE_PSE_ENGINE_ID") if not os.getenv(v)]
if missing:
    raise RuntimeError(f"google_pse misconfigured; missing env vars: {', '.join(missing)}")

Type guard

def google_pse_configured() -> bool:
    """True when both the API key and engine ID resolve for google_pse search."""
    import os
    return bool(os.getenv("GOOGLE_PSE_API_KEY")) and bool(os.getenv("GOOGLE_PSE_ENGINE_ID"))

Try / catch

try:
    results = litellm.web_search(provider="google_pse", query=q)
except ValueError as e:
    if "GOOGLE_PSE_API_KEY is not set" in str(e):
        raise RuntimeError("Provision a Google Cloud API key (Custom Search API enabled) as GOOGLE_PSE_API_KEY") from e
    raise

Prevention

When it happens

Trigger: Invoking litellm's web search with provider google_pse without passing api_key and without GOOGLE_PSE_API_KEY exported — e.g. calling the search transformation's set_headers path (or a higher-level search API that reaches it) in a fresh shell, CI runner, or container where only other provider keys are configured. Also fires when GOOGLE_PSE_API_BASE is set to a host that is not authorized for the server key.

Common situations: .env files loaded for the LLM provider but not for search settings; deploying search features to environments where the Google Cloud key was never provisioned; passing api_base without a matching api_key so the host-aware resolver declines to reuse the server key.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/c5322c91827bb357. Report an issue: GitHub.