Mintplex-Labs/anything-llm · error · Error

${e.message}

Error message

${e.message}

What it means

Re-thrown from the .catch on this.openai.chat.completions.create inside ZAiLLM.getChatCompletion. Z.AI does not pre-validate the model in getChatCompletion (no isValidChatCompletionModel guard here), so the request is sent directly to https://api.z.ai/api/paas/v4 and any rejection is flattened to a new Error carrying only e.message, discarding status/type/stack.

Source

Thrown at server/utils/AiProviders/zai/index.js:121

      prompt,
      ...chatHistory,
      {
        role: "user",
        content: this.#generateContent({ userPrompt, attachments }),
      },
    ];
  }

  async getChatCompletion(messages = null, { temperature = 0.7 }) {
    const result = await LLMPerformanceMonitor.measureAsyncFunction(
      this.openai.chat.completions
        .create({
          model: this.model,
          messages,
          temperature,
        })
        .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. Inspect e.message to classify: auth -> rotate ZAI_API_KEY; model_not_found -> fix ZAI_MODEL_PREF; rate/context -> reduce input or back off.
  2. Wrap the call in try/catch and branch on message substrings since the structured error is lost.
  3. For richer handling, call this.openai.chat.completions.create directly and inspect e.status/e.error before rethrowing.
  4. Retry idempotent reads on transient 5xx/429 with exponential backoff.

Example fix

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

// after
.catch((e) => {
  const err = new Error(e.message);
  err.status = e.status;
  err.upstream = "zai";
  err.cause = e;
  throw err;
})
Defensive patterns

Strategy: try-catch

Validate before calling

// Z.AI does not validate the model in getChatCompletion — pre-flight auth/endpoint:
try {
  await llm.openai.models.list();
} catch (e) {
  throw new Error(`Z.AI preflight failed: ${e.message}`);
}

Try / catch

try {
  const res = await llm.getChatCompletion(messages, { temperature });
} catch (e) {
  const msg = e.message;
  if (/401|unauthorized|api.key/i.test(msg)) handleAuth();
  else if (/model_not_found|404/i.test(msg)) handleBadModel();
  else if (/rate.limit|429|quota/i.test(msg)) backoff();
  else throw e;
}

Prevention

When it happens

Trigger: 401 invalid ZAI_API_KEY; 404 unknown ZAI_MODEL_PREF; 429 rate limit; 400 from unsupported message format or context exceeding the model window; 5xx from the Z.AI PaaS; network errors.

Common situations: Setting ZAI_MODEL_PREF to a model not enabled for the account; exceeding GLM token limits; revoked key after rotation; intermittent upstream outages; sending tool/function payloads the model rejects.

Related errors


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