{"record":{"id":"5a8230f072849439","repo":"BerriAI/litellm","slug":"perplexityai-api-key-is-not-set-set-perplexityai","errorCode":null,"errorMessage":"PERPLEXITYAI_API_KEY is not set. Set `PERPLEXITYAI_API_KEY` environment variable.","messagePattern":"PERPLEXITYAI_API_KEY is not set\\. Set `PERPLEXITYAI_API_KEY` environment variable\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/perplexity/search/transformation.py","lineNumber":61,"sourceCode":"    def validate_environment(\n        self,\n        headers: dict,\n        api_key: str | None = None,\n        api_base: str | None = None,\n        **kwargs,\n    ) -> dict:\n        \"\"\"\n        Validate environment and return headers.\n        \"\"\"\n        api_key = self.resolve_server_api_key(\n            caller_api_key=api_key,\n            caller_api_base=api_base,\n            key_env_vars=(\"PERPLEXITYAI_API_KEY\",),\n            base_env_var=\"PERPLEXITY_API_BASE\",\n            default_api_base=self.PERPLEXITY_API_BASE,\n        )\n        if not api_key:\n            raise ValueError(\"PERPLEXITYAI_API_KEY is not set. Set `PERPLEXITYAI_API_KEY` environment variable.\")\n        headers[\"Authorization\"] = f\"Bearer {api_key}\"\n        headers[\"Content-Type\"] = \"application/json\"\n        return headers\n\n    def get_complete_url(\n        self,\n        api_base: str | None,\n        optional_params: dict,\n        data: dict | list[dict] | None = None,\n        **kwargs,\n    ) -> str:\n        \"\"\"\n        Get complete URL for Search endpoint.\n        \"\"\"\n        api_base = api_base or get_secret_str(\"PERPLEXITY_API_BASE\") or self.PERPLEXITY_API_BASE\n\n        # append \"/search\" to the api base if it's not already there\n        if not api_base.endswith(\"/search\"):","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/perplexity/search/transformation.py#L43-L79","documentation":"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.","triggerScenarios":"Invoking litellm's Perplexity search support without an api_key while PERPLEXITYAI_API_KEY is not set in the running process.","commonSituations":"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).","solutions":["export PERPLEXITYAI_API_KEY=... in the environment that runs litellm","Pass api_key explicitly on the call or in the litellm deployment config","Add a startup assertion for the variable so failures surface at boot, not mid-request"],"exampleFix":"// before\nres = litellm.perplexity_search(query=\"...\")\n\n// after\nimport os\nres = litellm.perplexity_search(query=\"...\", api_key=os.environ[\"PERPLEXITYAI_API_KEY\"])","handlingStrategy":"validation","validationCode":"import os\n\ndef perplexity_key_present() -> bool:\n    return bool(os.getenv(\"PERPLEXITYAI_API_KEY\"))\n\nassert perplexity_key_present(), \"Set PERPLEXITYAI_API_KEY\"  # run at startup","typeGuard":null,"tryCatchPattern":"try/except ValueError around the search call converts this deterministic configuration failure into a clear ops error; no retry will help.","preventionTips":["Assert PERPLEXITYAI_API_KEY resolves at process start","Watch for the PERPLEXITY_API_KEY vs PERPLEXITYAI_API_KEY name confusion","Pass api_key explicitly in server deployments instead of relying on shell env"],"tags":["perplexity","search","authentication","env-var","litellm"],"backgroundTag":"missing-api-key","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}