{"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/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/foundry/index.js#L289-L325","documentation":"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.","triggerScenarios":"Invoking getChatCompletion on a FoundryLLM instance where neither a modelPreference argument nor FOUNDRY_MODEL_PREF was supplied at construction time.","commonSituations":"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.","solutions":["Set FOUNDRY_MODEL_PREF in .env to a valid Foundry model id","Pass an explicit modelPreference when constructing FoundryLLM from code","Re-select the model in the AnythingLLM provider settings so it is persisted","Restart the server after updating .env so process.env reflects the change"],"exampleFix":"// before\nconst llm = new FoundryLLM(embedder); // no modelPreference, FOUNDRY_MODEL_PREF unset\n\n// after\n// .env: FOUNDRY_MODEL_PREF=Phi-3-mini-4k-instruct-cuda\nconst llm = new FoundryLLM(embedder);","handlingStrategy":"validation","validationCode":"function hasFoundryModel() {\n  return Boolean(process.env.FOUNDRY_MODEL_PREF);\n}\n// guard before constructing or before sending\nif (!hasFoundryModel()) {\n  throw new ConfigError('Set FOUNDRY_MODEL_PREF to a valid Foundry model id.');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Require a model id in the provider-settings UI before saving","Default FOUNDRY_MODEL_PREF in your .env template so it is never blank","Validate model config at startup, not at first inference"],"tags":["foundry","configuration","model-selection","chat-completion"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}