Mintplex-Labs/anything-llm · error · Error

System prompt variable with this key already exists

Error message

System prompt variable with this key already exists

What it means

Thrown by SystemPromptVariables._checkVariableKey when checkExisting is true and this.get(key) returns a non-null record. Only the create path passes checkExisting=true (line 180), so this is a uniqueness guard preventing two static variables with the same key. update passes checkExisting=false (line 204), so updates do not trip it.

Source

Thrown at server/models/systemPromptVariables.js:369

   * Internal function to check if a variable key is valid
   * @param {string} key
   * @param {boolean} checkExisting
   * @returns {Promise<boolean>}
   */
  _checkVariableKey: async function (key = null, checkExisting = true) {
    if (!key) throw new Error("Key is required");
    if (typeof key !== "string") throw new Error("Key must be a string");
    if (!/^[a-zA-Z0-9_]+$/.test(key))
      throw new Error("Key must contain only letters, numbers and underscores");
    if (key.length > 255)
      throw new Error("Key must be less than 255 characters");
    if (key.length < 3) throw new Error("Key must be at least 3 characters");
    if (key.startsWith("user."))
      throw new Error("Key cannot start with 'user.'");
    if (key.startsWith("system."))
      throw new Error("Key cannot start with 'system.'");
    if (checkExisting && (await this.get(key)) !== null)
      throw new Error("System prompt variable with this key already exists");

    return true;
  },
};

module.exports = { SystemPromptVariables };

View on GitHub (pinned to 526360e320)

Solutions

  1. If you want a new value for an existing key, use PUT /system/prompt-variables/:id instead of POST.
  2. Look up the variable by key first and update it if it already exists.
  3. Make the create flow idempotent by checking existence before submitting.

Example fix

// before
await SystemPromptVariables.create({ key: existingKey, value });
// after
const existing = await SystemPromptVariables.get(existingKey);
if (existing) {
  await SystemPromptVariables.update(existing.id, { key: existingKey, value });
} else {
  await SystemPromptVariables.create({ key: existingKey, value });
}
Defensive patterns

Strategy: try-catch

Validate before calling

const existing = await SystemPromptVariables.get(key);
if (existing) {
  // update instead of create
}

Try / catch

try {
  await SystemPromptVariables.create({ key, value });
} catch (e) {
  if (/already exists/.test(e.message)) {
    const existing = await SystemPromptVariables.get(key);
    await SystemPromptVariables.update(existing.id, { key, value });
  } else throw e;
}

Prevention

When it happens

Trigger: POST /system/prompt-variables with a key that already exists in the system_prompt_variables table.

Common situations: Admin clicks 'create' twice or retries after a network blip. Two admins defining the same variable concurrently. Migration script re-running without dedup.

Related errors


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