{"record":{"id":"ace19a1ad2b7e728","repo":"Mintplex-Labs/anything-llm","slug":"no-perplexity-api-key-was-set","errorCode":null,"errorMessage":"No Perplexity API key was set.","messagePattern":"No Perplexity API key was set\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/perplexity/index.js","lineNumber":20,"sourceCode":"const { NativeEmbedder } = require(\"../../EmbeddingEngines/native\");\nconst { isAbortError } = require(\"../../helpers/abortSignals\");\nconst {\n  writeResponseChunk,\n  clientAbortedHandler,\n} = require(\"../../helpers/chat/responses\");\nconst {\n  LLMPerformanceMonitor,\n} = require(\"../../helpers/chat/LLMPerformanceMonitor\");\n\nfunction perplexityModels() {\n  const { MODELS } = require(\"./models.js\");\n  return MODELS || {};\n}\n\nclass PerplexityLLM {\n  constructor(embedder = null, modelPreference = null) {\n    if (!process.env.PERPLEXITY_API_KEY)\n      throw new Error(\"No Perplexity API key was set.\");\n\n    this.className = \"PerplexityLLM\";\n    const { OpenAI: OpenAIApi } = require(\"openai\");\n    this.openai = new OpenAIApi({\n      baseURL: \"https://api.perplexity.ai\",\n      apiKey: process.env.PERPLEXITY_API_KEY ?? null,\n    });\n    this.model =\n      modelPreference ||\n      process.env.PERPLEXITY_MODEL_PREF ||\n      \"llama-3-sonar-large-32k-online\"; // Give at least a unique model to the provider as last fallback.\n    this.limits = {\n      history: this.promptWindowLimit() * 0.15,\n      system: this.promptWindowLimit() * 0.15,\n      user: this.promptWindowLimit() * 0.7,\n    };\n\n    this.embedder = embedder ?? new NativeEmbedder();","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/perplexity/index.js#L2-L38","documentation":"Thrown by the PerplexityLLM constructor when process.env.PERPLEXITY_API_KEY is falsy at instantiation time. AnythingLLM refuses to build the provider client without credentials because every downstream call would fail with a 401 anyway, so it fails fast. The provider is an OpenAI-compatible client pointed at https://api.perplexity.ai.","triggerScenarios":"Instantiating `new PerplexityLLM(embedder, modelPreference)` while PERPLEXITY_API_KEY is unset, empty string, or whitespace-trimmed to nothing. This includes selecting Perplexity as the workspace LLM in the UI before the key is configured, or running the server with a .env that omits the variable.","commonSituations":"Fresh clone of AnythingLLM where the developer copied .env.example but never filled in PERPLEXITY_API_KEY; a deployment where the env var is set in one process (e.g. shell) but not exported into the Node process; a typo'd key name (PERPLEXITY_KEY vs PERPLEXITY_API_KEY); rotating a revoked key and forgetting to update the running container.","solutions":["Set PERPLEXITY_API_KEY in server/.env (or your process env) to a valid key from https://www.perplexity.ai/settings/api and restart the server.","Verify the variable is actually loaded by the Node process: `node -e \"console.log(Boolean(process.env.PERPLEXITY_API_KEY))\"` run with the same env as the server.","If running under Docker/compose, confirm the variable is passed through (env_file or environment: block) and the container was recreated after the change.","Check for leading/trailing whitespace or quote characters copied from the portal."],"exampleFix":"// before\n// PERPLEXITY_API_KEY is unset -> constructor throws\nconst llm = new PerplexityLLM(embedder, \"llama-3-sonar-large-32k-online\");\n\n// after\n// server/.env\n// PERPLEXITY_API_KEY=pplx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\nprocess.env.PERPLEXITY_API_KEY ??= require(\"fs\").readFileSync(\"/run/secrets/perplexity_key\", \"utf8\").trim();\nconst llm = new PerplexityLLM(embedder, \"llama-3-sonar-large-32k-online\");","handlingStrategy":"validation","validationCode":"function assertPerplexityKey() {\n  if (!process.env.PERPLEXITY_API_KEY || !process.env.PERPLEXITY_API_KEY.trim()) {\n    throw new Error(\"PERPLEXITY_API_KEY is missing — get one at https://www.perplexity.ai/settings/api\");\n  }\n}\n// run before constructing PerplexityLLM\nassertPerplexityKey();\nconst llm = new PerplexityLLM(embedder, modelPref);","typeGuard":"function hasPerplexityKey(env = process.env) {\n  return typeof env.PERPLEXITY_API_KEY === \"string\" && env.PERPLEXITY_API_KEY.trim().length > 0;\n}","tryCatchPattern":"let llm;\ntry {\n  llm = new PerplexityLLM(embedder, modelPref);\n} catch (e) {\n  if (/No Perplexity API key/i.test(e.message)) {\n    return { ok: false, reason: \"missing-api-key\", action: \"configure PERPLEXITY_API_KEY\" };\n  }\n  throw e;\n}","preventionTips":["Centralise env validation at server boot and fail with a single, actionable message listing every missing provider key.","Keep a .env.example with the exact variable names (PERPLEXITY_API_KEY) commented out so typos are caught early.","After rotating keys, always recreate/restart the process so the new env is loaded.","Use a secret manager rather than editing .env by hand to avoid whitespace/quote artifacts."],"tags":["configuration","api-key","env","llm-provider","perplexity","constructor"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}