BerriAI/litellm · error · ValueError

PERPLEXITYAI_API_KEY is not set. Set `PERPLEXITYAI_API_KEY`

Error message

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

What it means

Perplexity's search transformation resolves the credential via resolve_server_api_key (env var PERPLEXITYAI_API_KEY, caller key, or router config). If nothing resolves it raises this ValueError before any network call - the fix is purely configuration.

Source

Thrown at litellm/llms/perplexity/search/transformation.py:61

    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=("PERPLEXITYAI_API_KEY",),
            base_env_var="PERPLEXITY_API_BASE",
            default_api_base=self.PERPLEXITY_API_BASE,
        )
        if not api_key:
            raise ValueError("PERPLEXITYAI_API_KEY is not set. Set `PERPLEXITYAI_API_KEY` environment variable.")
        headers["Authorization"] = f"Bearer {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("PERPLEXITY_API_BASE") or self.PERPLEXITY_API_BASE

        # append "/search" to the api base if it's not already there
        if not api_base.endswith("/search"):

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. export PERPLEXITYAI_API_KEY=... in the environment that runs litellm
  2. Pass api_key explicitly on the call or in the litellm deployment config
  3. Add a startup assertion for the variable so failures surface at boot, not mid-request

Example fix

// before
res = litellm.perplexity_search(query="...")

// after
import os
res = litellm.perplexity_search(query="...", api_key=os.environ["PERPLEXITYAI_API_KEY"])
Defensive patterns

Strategy: validation

Validate before calling

import os

def perplexity_key_present() -> bool:
    return bool(os.getenv("PERPLEXITYAI_API_KEY"))

assert perplexity_key_present(), "Set PERPLEXITYAI_API_KEY"  # run at startup

Try / catch

try/except ValueError around the search call converts this deterministic configuration failure into a clear ops error; no retry will help.

Prevention

When it happens

Trigger: Invoking litellm's Perplexity search support without an api_key while PERPLEXITYAI_API_KEY is not set in the running process.

Common situations: Key present in a dev shell but absent in the deployed container/service; .env not loaded before litellm; variable name typo (e.g., PERPLEXITY_API_KEY).

Related errors


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