Mintplex-Labs/anything-llm · error · Error

e.message

Error message

e.message

What it means

Not a distinct error type — the literal message is whatever e.message the underlying OpenAI SDK (Foundry's OpenAI-compatible endpoint) threw. The `.catch((e) => { throw new Error(e.message); })` wrapper strips the original error class and stack, re-throwing only the human-readable text. So 'e.message' covers every failure the SDK can raise during a chat completion: rate limits, auth, model-not-found, network/premature-close, malformed request, etc.

Source

Thrown at server/utils/AiProviders/foundry/index.js:324

    if (!this.model)
      throw new Error(
        `Foundry chat: ${this.model} is not valid or defined model for chat completion!`
      );

    // max_completion_tokens is required by Foundry (it caps output at 1024
    // otherwise), so the window has to be resolved before the request is built.
    await this.assertModelContextLimits();
    await this.assertModelLoaded();
    const result = await LLMPerformanceMonitor.measureAsyncFunction(
      this.openai.chat.completions
        .create({
          model: this.model,
          messages,
          temperature,
          max_completion_tokens: this.promptWindowLimit(),
        })
        .catch((e) => {
          throw new Error(e.message);
        })
    );

    if (
      !result.output.hasOwnProperty("choices") ||
      result.output.choices.length === 0
    )
      return null;

    return {
      textResponse: result.output.choices[0].message.content,
      metrics: {
        prompt_tokens: result.output.usage.prompt_tokens || 0,
        completion_tokens: result.output.usage.completion_tokens || 0,
        total_tokens: result.output.usage.total_tokens || 0,
        outputTps: result.output.usage.completion_tokens / result.duration,
        duration: result.duration,
        model: this.model,

View on GitHub (pinned to 526360e320)

Solutions

  1. Read the captured e.message text — it is the real cause (e.g. 'Premature close', 'context_length_exceeded', 401)
  2. If it says the model is gone, the file's recovery path is to clear it from #loadedModels so the next call reloads it
  3. Lower max_completion_tokens / the resolved context window if the message mentions context length
  4. Check Foundry Local process health and VRAM; restart Foundry if it OOMed

Example fix

// before (loses stack + class)
.catch((e) => { throw new Error(e.message); })

// after (preserve original error)
.catch((e) => { throw e; })
Defensive patterns

Strategy: try-catch

Try / catch

try {
  return await llm.getChatCompletion(messages, { temperature });
} catch (e) {
  // e.message is the OpenAI SDK's text; match on it since the class is lost
  if (/Premature close/i.test(e.message)) {
    FoundryLLM.forgetModel(this.model); // clear cache so next call reloads
    return retryOnce();
  }
  if (/context_length_exceeded/i.test(e.message)) {
    throw new ContextTooLongError(e.message);
  }
  throw e;
}

Prevention

When it happens

Trigger: this.openai.chat.completions.create({ model, messages, temperature, max_completion_tokens }) rejecting for any reason — Foundry Local returned a non-2xx, the socket dropped mid-response (the 'Premature close' case the file comments on), max_completion_tokens exceeded the window, or the model was evicted after loading.

Common situations: Model evicted by Foundry's idle timeout right after the load check (the documented 'Premature close'); max_completion_tokens larger than the resolved promptWindowLimit; Foundry Local crashed/OOM during generation; transient network blip to localhost endpoint.

Related errors


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