Mintplex-Labs/anything-llm · error · Error

Perplexity chat: ${this.model} is not valid for chat complet

Error message

Perplexity chat: ${this.model} is not valid for chat completion!

What it means

Thrown by PerplexityLLM.getChatCompletion when this.model is not a key returned by allModelInformation() (i.e. it is not present in the bundled ./models.js MODELS map). isValidChatCompletionModel does a strict hasOwnProperty check, so any model id the provider does not recognise is rejected before the network call.

Source

Thrown at server/utils/AiProviders/perplexity/index.js:92

    return availableModels.hasOwnProperty(model);
  }

  constructPrompt({
    systemPrompt = "",
    contextTexts = [],
    chatHistory = [],
    userPrompt = "",
  }) {
    const prompt = {
      role: "system",
      content: `${systemPrompt}${this.#appendContext(contextTexts)}`,
    };
    return [prompt, ...chatHistory, { role: "user", content: userPrompt }];
  }

  async getChatCompletion(messages = null, { temperature = 0.7 }) {
    if (!(await this.isValidChatCompletionModel(this.model)))
      throw new Error(
        `Perplexity chat: ${this.model} is not valid for chat completion!`
      );

    const result = await LLMPerformanceMonitor.measureAsyncFunction(
      this.openai.chat.completions
        .create({
          model: this.model,
          messages,
          temperature,
        })
        .catch((e) => {
          throw new Error(e.message);
        })
    );

    if (
      !result.output.hasOwnProperty("choices") ||
      result.output.choices.length === 0

View on GitHub (pinned to 526360e320)

Solutions

  1. Log this.model right before the call and confirm it matches an id in server/utils/AiProviders/perplexity/models.js (e.g. llama-3-sonar-large-32k-online).
  2. If the bundled list is stale, update models.js to include the current Perplexity model ids, or pass a known-good modelPreference explicitly.
  3. Clear PERPLEXITY_MODEL_PREF so the constructor falls back to the hardcoded default (llama-3-sonar-large-32k-online).
  4. Re-select the model in the AnythingLLM UI so a valid id is persisted on the workspace.

Example fix

// before
const llm = new PerplexityLLM(embedder, "sonar-medium-chat"); // not in MODELS
await llm.getChatCompletion(messages, { temperature: 0.7 });

// after
const { MODELS } = require("./models.js");
const validId = Object.prototype.hasOwnProperty.call(MODELS, "sonar-medium-chat")
  ? "sonar-medium-chat"
  : "llama-3-sonar-large-32k-online";
const llm = new PerplexityLLM(embedder, validId);
await llm.getChatCompletion(messages, { temperature: 0.7 });
Defensive patterns

Strategy: validation

Validate before calling

const { MODELS } = require("./models.js");
function pickPerplexityModel(pref) {
  if (pref && Object.prototype.hasOwnProperty.call(MODELS, pref)) return pref;
  return "llama-3-sonar-large-32k-online"; // safe default
}
const model = pickPerplexityModel(process.env.PERPLEXITY_MODEL_PREF);
const llm = new PerplexityLLM(embedder, model);
if (!(await llm.isValidChatCompletionModel(llm.model))) throw new Error(`bad model ${llm.model}`);

Type guard

function isKnownPerplexityModel(id, MODELS) {
  return typeof id === "string" && Object.prototype.hasOwnProperty.call(MODELS, id);
}

Try / catch

try {
  await llm.getChatCompletion(messages, { temperature: 0.7 });
} catch (e) {
  if (/not valid for chat completion/i.test(e.message)) {
    llm.model = "llama-3-sonar-large-32k-online";
    return llm.getChatCompletion(messages, { temperature: 0.7 });
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling getChatCompletion with a this.model set to something not in the MODELS table — e.g. an offline-only model id, a typo, a model Perplexity has deprecated/renamed, or a custom value passed as modelPreference that the bundled models.js does not list.

Common situations: The user passed `modelPreference = "sonar-medium"` (old name) after Perplexity renamed to `llama-3-sonar-large-32k-online`; PERPLEXITY_MODEL_PREF in .env points at a model the installed AnythingLLM version predates; copy-pasting an OpenAI model id ("gpt-4") into a Perplexity workspace; the bundled models.js was hand-edited and lost entries.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/ec1b3b44935268b9. Report an issue: GitHub.