Mintplex-Labs/anything-llm · critical · Error

LMStudio must have a valid model set.

Error message

LMStudio must have a valid model set.

What it means

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.

Source

Thrown at server/utils/AiProviders/lmStudio/index.js:34

  constructor(embedder = null, modelPreference = null) {
    if (!process.env.LMSTUDIO_BASE_PATH)
      throw new Error("No LMStudio API Base Path was set.");

    this.className = "LMStudioLLM";
    const apiKey = process.env.LMSTUDIO_AUTH_TOKEN ?? null;
    this.lmstudio = new OpenAIApi({
      baseURL: parseLMStudioBasePath(process.env.LMSTUDIO_BASE_PATH), // here is the URL to your LMStudio instance
      apiKey,
    });

    // Prior to LMStudio 0.2.17 the `model` param was not required and you could pass anything
    // into that field and it would work. On 0.2.17 LMStudio introduced multi-model chat
    // which now has a bug that reports the server model id as "Loaded from Chat UI"
    // and any other value will crash inferencing. So until this is patched we will
    // try to fetch the `/models` and have the user set it, or just fallback to "Loaded from Chat UI"
    // which will not impact users with <v0.2.17 and should work as well once the bug is fixed.
    this.model = modelPreference || process.env.LMSTUDIO_MODEL_PREF;
    if (!this.model) throw new Error("LMStudio must have a valid model set.");

    this.embedder = embedder ?? new NativeEmbedder();
    this.defaultTemp = 0.7;

    // Lazy load the limits to avoid blocking the main thread on cacheContextWindows
    this.limits = null;

    LMStudioLLM.cacheContextWindows(true);
    this.#log(`initialized with model: ${this.model}`);
  }

  #log(text, ...args) {
    console.log(`\x1b[32m[LMStudio]\x1b[0m ${text}`, ...args);
  }

  static #slog(text, ...args) {
    console.log(`\x1b[32m[LMStudio]\x1b[0m ${text}`, ...args);
  }

View on GitHub (pinned to 526360e320)

Solutions

  1. 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).
  2. Select a model in the workspace LLM settings UI.
  3. In LMStudio, ensure a model is loaded before starting the local server, then use the model list endpoint to get the exact identifier.
  4. For LMStudio 0.2.17+, check the /v1/models endpoint for the correct model ID since the 'Loaded from Chat UI' bug may apply.

Example fix

// before: .env — no model pref
LMSTUDIO_BASE_PATH='http://localhost:1234/v1'

// after
LMSTUDIO_BASE_PATH='http://localhost:1234/v1'
LMSTUDIO_MODEL_PREF='Loaded from Chat UI'
Defensive patterns

Strategy: validation

Validate before calling

function validateLMStudioModel(modelPreference) {
  const model = modelPreference || process.env.LMSTUDIO_MODEL_PREF;
  if (!model) {
    throw new Error(
      'No LMStudio model set. Set LMSTUDIO_MODEL_PREF in .env or select a model in workspace settings.'
    );
  }
  return model;
}

const model = validateLMStudioModel(workspaceModel);

Type guard

/** @returns {model is string} */
function isValidModelName(model) {
  return typeof model === 'string' && model.trim().length > 0;
}

Try / catch

try {
  const llm = new LMStudioLLM(embedder, model);
} catch (e) {
  if (e.message.includes('valid model set')) {
    console.error('Set LMSTUDIO_MODEL_PREF in .env or load a model in LMStudio before starting the server.');
    return null;
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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