{"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":"Error","httpStatus":null,"severity":"critical","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/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/lmStudio/index.js#L16-L52","documentation":"Thrown by the LMStudioLLM constructor when no model can be resolved. The constructor uses `modelPreference || process.env.LMSTUDIO_MODEL_PREF` (note: `||` not `??`, so empty-string param also falls through). If both are falsy, the error fires. The model is needed because LMStudio 0.2.17+ requires an explicit model identifier due to a multi-model chat bug.","triggerScenarios":"Instantiating `new LMStudioLLM(embedder, model)` where model is falsy (null, undefined, empty string, 0) AND process.env.LMSTUDIO_MODEL_PREF is also unset or empty. The `||` chain means empty string falls through to the next option.","commonSituations":"LMStudio server is running but no model was loaded/selected in LMStudio before starting the server. The workspace LLM settings don't specify a model. LMSTUDIO_MODEL_PREF was removed from .env. The model identifier returned by LMStudio's /v1/models changed after an update.","solutions":["Set LMSTUDIO_MODEL_PREF in .env to a model loaded in LMStudio (e.g. 'Loaded from Chat UI' for pre-0.2.17, or the specific model ID for newer versions).","Select a model in the workspace LLM settings UI.","In LMStudio, ensure a model is loaded before starting the local server, then use the model list endpoint to get the exact identifier.","For LMStudio 0.2.17+, check the /v1/models endpoint for the correct model ID since the 'Loaded from Chat UI' bug may apply."],"exampleFix":"// before: .env — no model pref\nLMSTUDIO_BASE_PATH='http://localhost:1234/v1'\n\n// after\nLMSTUDIO_BASE_PATH='http://localhost:1234/v1'\nLMSTUDIO_MODEL_PREF='Loaded from Chat UI'","handlingStrategy":"validation","validationCode":"function validateLMStudioModel(modelPreference) {\n  const model = modelPreference || process.env.LMSTUDIO_MODEL_PREF;\n  if (!model) {\n    throw new Error(\n      'No LMStudio model set. Set LMSTUDIO_MODEL_PREF in .env or select a model in workspace settings.'\n    );\n  }\n  return model;\n}\n\nconst model = validateLMStudioModel(workspaceModel);","typeGuard":"/** @returns {model is string} */\nfunction isValidModelName(model) {\n  return typeof model === 'string' && model.trim().length > 0;\n}","tryCatchPattern":"try {\n  const llm = new LMStudioLLM(embedder, model);\n} catch (e) {\n  if (e.message.includes('valid model set')) {\n    console.error('Set LMSTUDIO_MODEL_PREF in .env or load a model in LMStudio before starting the server.');\n    return null;\n  }\n  throw e;\n}","preventionTips":["Load a model in LMStudio before starting its local server.","Query the /v1/models endpoint to get the exact model identifier.","For LMStudio 0.2.17+, be aware of the 'Loaded from Chat UI' model ID quirk."],"tags":["lmstudio","configuration","env-var","constructor","model-selection"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}