Mintplex-Labs/anything-llm · error · Error

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

Error message

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

What it means

Thrown by LocalAiLLM.getChatCompletion when `isValidChatCompletionModel(this.model)` returns false. However, the class defines `async isValidChatCompletionModel(_ = '') { return true; }` — it unconditionally returns true. This means the `if (!(await this.isValidChatCompletionModel(this.model)))` guard is always false, making this throw statement dead code that is unreachable in normal operation.

Source

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

    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 526360e320)

Solutions

  1. This error should not occur in normal usage — if you see it, check whether LocalAiLLM was subclassed with custom isValidChatCompletionModel logic.
  2. If you need real model validation, override isValidChatCompletionModel to check against LocalAI's /v1/models endpoint.
  3. Verify the model name matches a model available on the LocalAI server by querying its models endpoint.
Defensive patterns

Strategy: validation

Validate before calling

// This error is currently unreachable because isValidChatCompletionModel always returns true.
// If you add real validation, check the model before calling getChatCompletion:
async function validateLocalAIModel(provider) {
  // Query the LocalAI server's model list
  const res = await fetch(`${process.env.LOCAL_AI_BASE_PATH}/models`);
  const data = await res.json();
  const available = (data.data || []).map((m) => m.id);
  if (!available.includes(provider.model)) {
    throw new Error(`Model "${provider.model}" not available on LocalAI server.`);
  }
  return true;
}

Try / catch

// Currently unnecessary since the guard is dead code, but for future-proofing:
try {
  const result = await localaiProvider.getChatCompletion(messages, { temperature: 0.7 });
} catch (e) {
  if (e.message.includes('not valid for chat completion')) {
    console.error('LocalAI model validation failed for:', localaiProvider.model);
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: In the current implementation, this error cannot be triggered because isValidChatCompletionModel always returns true. It would only fire if a subclass overrode isValidChatCompletionModel with real validation logic that returned false for the configured model.

Common situations: Practically unreachable. If a developer subclasses LocalAiLLM and adds real model validation that rejects the configured model, this error would surface. The guard exists as a hook point for future model validation.

Related errors


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