Mintplex-Labs/anything-llm · error · Error

Foundry chat: ${this.model} is not valid or defined model fo

Error message

Foundry chat: ${this.model} is not valid or defined model for chat completion!

What it means

Thrown at the top of getChatCompletion when this.model is falsy. The constructor sets this.model from modelPreference || process.env.FOUNDRY_MODEL_PREF; if both are unset/empty the field stays undefined/null/'' and the method bails before doing any work. It is a guard against sending an empty `model` field to the Foundry OpenAI endpoint.

Source

Thrown at server/utils/AiProviders/foundry/index.js:307

    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 (!this.model)
      throw new Error(
        `Foundry chat: ${this.model} is not valid or defined model for chat completion!`
      );

    // max_completion_tokens is required by Foundry (it caps output at 1024
    // otherwise), so the window has to be resolved before the request is built.
    await this.assertModelContextLimits();
    await this.assertModelLoaded();
    const result = await LLMPerformanceMonitor.measureAsyncFunction(
      this.openai.chat.completions
        .create({
          model: this.model,
          messages,
          temperature,
          max_completion_tokens: this.promptWindowLimit(),
        })
        .catch((e) => {
          throw new Error(e.message);
        })

View on GitHub (pinned to 526360e320)

Solutions

  1. Set FOUNDRY_MODEL_PREF in .env to a valid Foundry model id
  2. Pass an explicit modelPreference when constructing FoundryLLM from code
  3. Re-select the model in the AnythingLLM provider settings so it is persisted
  4. Restart the server after updating .env so process.env reflects the change

Example fix

// before
const llm = new FoundryLLM(embedder); // no modelPreference, FOUNDRY_MODEL_PREF unset

// after
// .env: FOUNDRY_MODEL_PREF=Phi-3-mini-4k-instruct-cuda
const llm = new FoundryLLM(embedder);
Defensive patterns

Strategy: validation

Validate before calling

function hasFoundryModel() {
  return Boolean(process.env.FOUNDRY_MODEL_PREF);
}
// guard before constructing or before sending
if (!hasFoundryModel()) {
  throw new ConfigError('Set FOUNDRY_MODEL_PREF to a valid Foundry model id.');
}

Prevention

When it happens

Trigger: Invoking getChatCompletion on a FoundryLLM instance where neither a modelPreference argument nor FOUNDRY_MODEL_PREF was supplied at construction time.

Common situations: FOUNDRY_MODEL_PREF left blank in .env and the caller passed no modelPreference; the workspace/provider configuration was migrated and the model pref got wiped; selecting Foundry without picking a default model in the UI.

Related errors


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