{"record":{"id":"a0c363a51f6769e1","repo":"Mintplex-Labs/anything-llm","slug":"foundry-chat-this-model-is-not-valid-or-define","errorCode":null,"errorMessage":"Foundry chat: ${this.model} is not valid or defined model for chat completion!","messagePattern":"Foundry chat: (.+?) is not valid or defined model for chat completion!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/foundry/index.js","lineNumber":307,"sourceCode":"    attachments = [],\n  }) {\n    const prompt = {\n      role: \"system\",\n      content: `${systemPrompt}${this.#appendContext(contextTexts)}`,\n    };\n    return [\n      prompt,\n      ...formatChatHistory(chatHistory, this.#generateContent),\n      {\n        role: \"user\",\n        content: this.#generateContent({ userPrompt, attachments }),\n      },\n    ];\n  }\n\n  async getChatCompletion(messages = null, { temperature = 0.7 }) {\n    if (!this.model)\n      throw new Error(\n        `Foundry chat: ${this.model} is not valid or defined model for chat completion!`\n      );\n\n    // max_completion_tokens is required by Foundry (it caps output at 1024\n    // otherwise), so the window has to be resolved before the request is built.\n    await this.assertModelContextLimits();\n    await this.assertModelLoaded();\n    const result = await LLMPerformanceMonitor.measureAsyncFunction(\n      this.openai.chat.completions\n        .create({\n          model: this.model,\n          messages,\n          temperature,\n          max_completion_tokens: this.promptWindowLimit(),\n        })\n        .catch((e) => {\n          throw new Error(e.message);\n        })","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/3aec848f2885144aa8f1e53b9731a04310d5d558/server/utils/AiProviders/foundry/index.js#L289-L325","documentation":"FoundryLLM.getChatCompletion throws when this.model is falsy before it can resolve the context window (max_completion_tokens is mandatory for Foundry) and load the model. The constructor sets this.model from modelPreference || FOUNDRY_MODEL_PREF, both of which are optional, so unlike Docker Model Runner there is no constructor-time guard — an empty model survives construction and fails here.","triggerScenarios":"Any non-streaming chat request when neither a workspace modelPreference nor FOUNDRY_MODEL_PREF was provided. Constructing the provider succeeds (env path present, model blank), then the first getChatCompletion trips this guard.","commonSituations":"FOUNDRY_BASE_PATH configured but FOUNDRY_MODEL_PREF never set and the workspace model selection left empty; the UI passed an empty string as the preference; env var name typo like FOUNDRY_MODEL.","solutions":["Set FOUNDRY_MODEL_PREF to a valid model id (e.g. ai/qwen2.5-0.5b-instruct) in .env and restart","Or select a specific Foundry model in the workspace's model picker so modelPreference is non-empty","If constructing programmatically, pass a non-empty modelPreference second argument"],"exampleFix":"# before (.env)\nFOUNDRY_BASE_PATH=http://localhost:5273/v1\n# no model pref -> throws at chat time\n\n# after (.env)\nFOUNDRY_BASE_PATH=http://localhost:5273/v1\nFOUNDRY_MODEL_PREF=ai/qwen2.5-0.5b-instruct","handlingStrategy":"validation","validationCode":"const model = modelPreference || process.env.FOUNDRY_MODEL_PREF;\nif (!model) {\n  return respond(\"Select a Foundry Local model before sending a message.\");\n}\nconst llm = new FoundryLLM(embedder, model);","typeGuard":null,"tryCatchPattern":"try {\n  await llm.getChatCompletion(messages);\n} catch (err) {\n  if (/not valid or defined model/.test(err.message)) {\n    return respond(\"No Foundry model configured — set FOUNDRY_MODEL_PREF or pick one.\");\n  }\n  throw err;\n}","preventionTips":["Set FOUNDRY_MODEL_PREF even if the UI usually supplies the preference, as a safety net","Disallow saving a workspace with the Foundry provider and an empty model field","Log the resolved model id at construction time for quick diagnosis"],"tags":["foundry-local","missing-model-configuration","pre-flight-check"],"backgroundTag":"missing-model-configuration","analyzedSha":"3aec848f2885144aa8f1e53b9731a04310d5d558","analyzedAt":"2026-08-18T10:02:21.017Z","contentChangedAt":"2026-08-18T10:02:21.017Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}