Mintplex-Labs/anything-llm · error · Error

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

Error message

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

What it means

Thrown by ApiPieLLM.getChatCompletion when `await this.isValidChatCompletionModel(this.model)` returns false, i.e. the configured model id is not in ApiPie's accepted chat-model list. It is a deterministic pre-flight check before any network call to apipie.ai, so no request is wasted on an unsupported model.

Source

Thrown at server/utils/AiProviders/apipie/index.js:192

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

  async getChatCompletion(messages = null, { temperature = 0.7 }) {
    if (!(await this.isValidChatCompletionModel(this.model)))
      throw new Error(
        `ApiPie 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. Set APIPIE_LLM_MODEL_PREF (or pass modelPreference) to a model id that isValidChatCompletionModel accepts — check the provider's current chat-capable model list.
  2. If using a custom model id in the UI, verify it against ApiPie's /v1/models endpoint before saving.
  3. Clear a stale saved preference and let it fall back to the default, then re-pick a supported model.
  4. Update the AnythingLLM provider file if ApiPie's valid-model list changed and isValidChatCompletionModel needs refreshing.

Example fix

// before
// .env
APIPIE_LLM_MODEL_PREF=some-embedding-model-001

// after
// .env
APIPIE_LLM_MODEL_PREF=openrouter/mistral-7b-instruct
Defensive patterns

Strategy: validation

Validate before calling

// Validate before calling getChatCompletion
const valid = await llm.isValidChatCompletionModel(llm.model);
if (!valid) {
  throw new Error(
    `Model '${llm.model}' is not a valid ApiPie chat model. Set APIPIE_LLM_MODEL_PREF to a supported id.`
  );
}
const res = await llm.getChatCompletion(messages, { temperature });

Type guard

/**
 * @param {ApiPieLLM} llm
 * @param {string} model
 * @returns {Promise<boolean>}
 */
async function isValidApiPieChatModel(llm, model) {
  return typeof model === "string" && model.length > 0 &&
    (await llm.isValidChatCompletionModel(model));
}

Try / catch

try {
  const res = await llm.getChatCompletion(messages, { temperature });
} catch (e) {
  if (/is not valid for chat completion/.test(e.message)) {
    return pickFallbackModelAndRetry();
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling getChatCompletion with this.model set to something isValidChatCompletionModel rejects — e.g. an embedding-only model id, a typo, a model id from a different provider namespace, or a model ApiPie delisted. this.model comes from modelPreference arg, else APIPIE_LLM_MODEL_PREF, else the hardcoded default.

Common situations: User typed a custom model id that isn't a chat model; ApiPie renamed/removed a model and the saved preference is stale; copied a model id from another provider's docs; default fallback changed but env still points at an old id.

Related errors


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