{"record":{"id":"5b388d58ba51a964","repo":"Mintplex-Labs/anything-llm","slug":"lmstudio-must-have-a-valid-model-set","errorCode":null,"errorMessage":"LMStudio must have a valid model set.","messagePattern":"LMStudio must have a valid model set\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/lmStudio/index.js","lineNumber":34,"sourceCode":"  constructor(embedder = null, modelPreference = null) {\n    if (!process.env.LMSTUDIO_BASE_PATH)\n      throw new Error(\"No LMStudio API Base Path was set.\");\n\n    this.className = \"LMStudioLLM\";\n    const apiKey = process.env.LMSTUDIO_AUTH_TOKEN ?? null;\n    this.lmstudio = new OpenAIApi({\n      baseURL: parseLMStudioBasePath(process.env.LMSTUDIO_BASE_PATH), // here is the URL to your LMStudio instance\n      apiKey,\n    });\n\n    // Prior to LMStudio 0.2.17 the `model` param was not required and you could pass anything\n    // into that field and it would work. On 0.2.17 LMStudio introduced multi-model chat\n    // which now has a bug that reports the server model id as \"Loaded from Chat UI\"\n    // and any other value will crash inferencing. So until this is patched we will\n    // try to fetch the `/models` and have the user set it, or just fallback to \"Loaded from Chat UI\"\n    // which will not impact users with <v0.2.17 and should work as well once the bug is fixed.\n    this.model = modelPreference || process.env.LMSTUDIO_MODEL_PREF;\n    if (!this.model) throw new Error(\"LMStudio must have a valid model set.\");\n\n    this.embedder = embedder ?? new NativeEmbedder();\n    this.defaultTemp = 0.7;\n\n    // Lazy load the limits to avoid blocking the main thread on cacheContextWindows\n    this.limits = null;\n\n    LMStudioLLM.cacheContextWindows(true);\n    this.#log(`initialized with model: ${this.model}`);\n  }\n\n  #log(text, ...args) {\n    console.log(`\\x1b[32m[LMStudio]\\x1b[0m ${text}`, ...args);\n  }\n\n  static #slog(text, ...args) {\n    console.log(`\\x1b[32m[LMStudio]\\x1b[0m ${text}`, ...args);\n  }","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/f92433b4ea0598492a1e6645ea22addbb4dd1287/server/utils/AiProviders/lmStudio/index.js#L16-L52","documentation":"LMStudioLLM throws when neither a modelPreference argument nor LMSTUDIO_MODEL_PREF yields a model. The in-code comment explains why: LM Studio 0.2.17 introduced multi-model chat and a bug that reports the server's model id as 'Loaded from Chat UI', so the client cannot safely auto-detect; the user must pin an explicit model id that matches what LM Studio actually loaded.","triggerScenarios":"Constructing LMStudioLLM with no/empty modelPreference while LMSTUDIO_MODEL_PREF is unset — i.e., LMSTUDIO_BASE_PATH configured but the model line missing. The || chain means an empty-string preference falls through to env; both must be absent to throw.","commonSituations":"Upgrading LM Studio past 0.2.17 where the old 'pass any model string' behavior broke, then hitting this after removing the pref; using the model's display name instead of its internal id; the workspace settings model field cleared; copy-pasting Ollama model names that differ from LM Studio's loaded model id.","solutions":["Set LMSTUDIO_MODEL_PREF to the model identifier LM Studio reports (check the loaded model in the app, or curl $LMSTUDIO_BASE_PATH/v1/models).","Match the id exactly — 'Loaded from Chat UI' era quirks aside, the id must equal the loaded model's identifier.","Ensure a model is actually loaded in LM Studio before starting the server; an unloaded server reports no usable id.","Restart AnythingLLM after env changes."],"exampleFix":"# before (.env)\nLMSTUDIO_BASE_PATH=http://127.0.0.1:1234\n\n# after (.env)\nLMSTUDIO_BASE_PATH=http://127.0.0.1:1234\nLMSTUDIO_MODEL_PREF=meta-llama-3.1-8b-instruct","handlingStrategy":"validation","validationCode":"const lmModel = modelPreference || process.env.LMSTUDIO_MODEL_PREF;\nif (!lmModel) throw new Error(\"LMSTUDIO_MODEL_PREF required (LM Studio 0.2.17+ multi-model bug prevents auto-detect)\");\nconst llm = new LMStudioLLM(embedder, lmModel);","typeGuard":"function hasLmStudioModel(arg) {\n  const m = arg || process.env.LMSTUDIO_MODEL_PREF;\n  return typeof m === \"string\" && m.trim().length > 0;\n}","tryCatchPattern":"try {\n  new LMStudioLLM(embedder, pref);\n} catch (e) {\n  if (/valid model set/i.test(e.message)) throw new Error(\"Load a model in LM Studio and set LMSTUDIO_MODEL_PREF to its id\");\n  throw e;\n}","preventionTips":["On LM Studio 0.2.17+, never rely on auto-detection — always pin the model id.","Copy the id from curl $LMSTUDIO_BASE_PATH/v1/models rather than the UI display name.","Keep a model loaded in LM Studio before starting the AnythingLLM server."],"tags":["lm-studio","anythingllm","model-selection","env-var","version-compatibility","configuration"],"backgroundTag":"model-not-configured","analyzedSha":"f92433b4ea0598492a1e6645ea22addbb4dd1287","analyzedAt":"2026-08-18T10:02:21.017Z","contentChangedAt":"2026-08-18T10:02:21.017Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}