BerriAI/litellm · error · ValueError

SERPER_API_KEY is not set. Set `SERPER_API_KEY` environment

Error message

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

What it means

Validation guard in validate_environment: resolve_server_api_key found no caller api_key, SERPER_API_KEY env var, or server-side configured key, so the Serper search request cannot be authenticated and is rejected before any HTTP call.

Source

Thrown at litellm/llms/serper/search/transformation.py:66

    def validate_environment(
        self,
        headers: dict,
        api_key: str | None = None,
        api_base: str | None = None,
        **kwargs,
    ) -> dict:
        """
        Validate environment and return headers.
        """
        api_key = self.resolve_server_api_key(
            caller_api_key=api_key,
            caller_api_base=api_base,
            key_env_vars=("SERPER_API_KEY",),
            base_env_var="SERPER_API_BASE",
            default_api_base=self.SERPER_API_BASE,
        )
        if not api_key:
            raise ValueError("SERPER_API_KEY is not set. Set `SERPER_API_KEY` environment variable.")
        headers["X-API-KEY"] = api_key
        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:
        """
        Get complete URL for Search endpoint.
        """
        api_base = api_base or get_secret_str("SERPER_API_BASE") or self.SERPER_API_BASE
        api_base = api_base.rstrip("/")

        if not api_base.endswith("/search"):

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Set the SERPER_API_KEY environment variable.

Example fix

os.environ["SERPER_API_KEY"] = "<key>"
Defensive patterns

Strategy: validation

When it happens

Trigger: Triggered when calling Serper search without SERPER_API_KEY set.

Common situations: See trigger scenarios.


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/7903cfd99472801d. Report an issue: GitHub.