Mintplex-Labs/anything-llm · error

LocalAI chat: is not valid for chat completion!

Error message

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

What it means

Defensive guard at the top of LocalAiLLM.getChatCompletion(): it awaits isValidChatCompletionModel(this.model) and throws if the check fails. In the current implementation isValidChatCompletionModel always returns true, so this exact error is effectively unreachable unless the method is overridden by a subclass or a modified/older version of the provider is deployed.

Solutions

  1. Confirm the model id matches one served by the instance: curl $LOCAL_AI_BASE_PATH/v1/models
  2. Re-select the model in the AnythingLLM UI so the stored id matches what LocalAI currently serves
  3. If you subclass or fork LocalAiLLM, review your isValidChatCompletionModel override against the live /v1/models response
  4. Check for local modifications: git diff server/utils/AiProviders/localAi/index.js

Example fix

# before
LOCAL_AI_MODEL_PREF=my-old-model
# after (use an id from `curl http://localhost:1234/v1/models`)
LOCAL_AI_MODEL_PREF=luna-ai-llama2-7b
Defensive patterns

Strategy: try-catch

Validate before calling

const valid = await llm.isValidChatCompletionModel(llm.model);
if (!valid)
  throw new Error(`Model ${llm.model} is not served by the LocalAI instance`);

Try / catch

try {
  await llm.getChatCompletion(messages);
} catch (e) {
  if (/not valid for chat completion/.test(e.message)) {
    // re-fetch $LOCAL_AI_BASE_PATH/v1/models and reselect the model
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling getChatCompletion when isValidChatCompletionModel has been overridden (custom fork or plugin) and returns false for this.model; or running a variant of localAi/index.js whose validator actually queries the LocalAI /v1/models endpoint while LOCAL_AI_MODEL_PREF names a model the instance does not serve.

Common situations: Custom provider subclasses that add real model validation; pointing LOCAL_AI_BASE_PATH at a rebuilt LocalAI container that no longer hosts the configured model; local modifications to server/utils/AiProviders/localAi/index.js.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@20f6d3546c (2026-08-18). Data as JSON: /api/errors/797d8580f5907102. Report an issue: GitHub.

Appendix: source

Thrown at server/utils/AiProviders/localAi/index.js:210

    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(
        `LocalAI chat: ${this.model} is not valid for chat completion!`
      );

    const result = await LLMPerformanceMonitor.measureAsyncFunction(
      this.openai.chat.completions.create({
        model: this.model,
        messages,
        temperature,
      })
    );

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

    const promptTokens = LLMPerformanceMonitor.countTokens(messages);

View on GitHub (pinned to 20f6d3546c)