Mintplex-Labs/anything-llm · warning · Error

System prompt variable not found

Error message

System prompt variable not found

What it means

Thrown by SystemPromptVariables.update(id, data) when prisma.system_prompt_variables.findFirst({ where: { id } }) returns null — the row being updated does not exist. Note the function returns null silently (no throw) if id, key, or value are missing, so reaching this throw means those were present but the id has no matching row.

Source

Thrown at server/models/systemPromptVariables.js:203

        description: description ? String(description) : null,
        type: type ? String(type) : "static",
        userId: userId ? Number(userId) : null,
      },
    });
  },

  /**
   * Updates a system prompt variable by its unique database ID
   * @param {number} id
   * @param {{ key: string, value: string, description: string }} data
   * @returns {Promise<SystemPromptVariable>}
   */
  update: async function (id, { key, value, description = null }) {
    if (!id || !key || !value) return null;
    const existingRecord = await prisma.system_prompt_variables.findFirst({
      where: { id: Number(id) },
    });
    if (!existingRecord) throw new Error("System prompt variable not found");
    await this._checkVariableKey(key, false);

    return await prisma.system_prompt_variables.update({
      where: { id: existingRecord.id },
      data: {
        key: String(key),
        value: String(value),
        description: description ? String(description) : null,
      },
    });
  },

  /**
   * Deletes a system prompt variable by its unique database ID
   * @param {number} id
   * @returns {Promise<boolean>}
   */
  delete: async function (id = null) {

View on GitHub (pinned to 526360e320)

Solutions

  1. Re-fetch the variable list to confirm the id still exists before editing.
  2. Verify id is a valid integer and matches an existing row.
  3. Handle the not-found case in the UI with a clear 'variable no longer exists' message.
  4. Use upsert or check existence then update to avoid race-driven 404s.

Example fix

// before
await SystemPromptVariables.update(req.params.id, req.body);
// after
const existing = await SystemPromptVariables.getById(req.params.id);
if (!existing) return res.status(404).json({ error: 'Variable was deleted' });
await SystemPromptVariables.update(req.params.id, req.body);
Defensive patterns

Strategy: validation

Validate before calling

const existing = await prisma.system_prompt_variables.findFirst({ where: { id: Number(id) } });
if (!existing) return { status: 404, body: { error: 'Variable not found' } };

Try / catch

try {
  await SystemPromptVariables.update(id, data);
} catch (e) {
  if (e.message === 'System prompt variable not found') return res.status(404).json({ error: e.message });
  throw e;
}

Prevention

When it happens

Trigger: Updating a variable that was deleted, using an id from a stale list, or passing an id that failed Number() coercion to a non-existent row. Concurrent deletion between a list-and-edit UI flow.

Common situations: Admin UI held a stale variable list open while another admin/session deleted the row; the id was extracted from a URL and truncated; a test fixture was cleaned up mid-run.

Related errors


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