Mintplex-Labs/anything-llm · warning

System prompt variable with this key already exists

Error message

System prompt variable with this key already exists

What it means

On create, _checkVariableKey runs with checkExisting=true; if SystemPromptVariables.get(key) finds an existing record, the key is rejected as a duplicate. Keys are unique across the installation, so a second variable with the same key cannot be created.

Solutions

  1. If the variable should change, update the existing record by its id instead of creating a new one
  2. Otherwise pick a different, more specific key
  3. Check for existence before creating: `const existing = await SystemPromptVariables.get(key)`
  4. Make seeding scripts idempotent (get-then-update-or-create)

Example fix

// before
await SystemPromptVariables.create({ key: 'company_name', value: 'Acme' }); // throws if key exists

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

Strategy: validation

Validate before calling

const { SystemPromptVariables } = require('../models/systemPromptVariables');

async function upsertVariable(key, value, description) {
  const existing = await SystemPromptVariables.get(key);
  if (existing) {
    return SystemPromptVariables.update(existing.id, { key, value, description });
  }
  return SystemPromptVariables.create({ key, value, description });
}

Try / catch

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

Prevention

When it happens

Trigger: Creating a second variable with an already-registered key; retrying a POST that actually succeeded (network retry, double click); two admins defining the same key concurrently.

Common situations: Seeding scripts run twice; idempotency-unaware API clients; teams independently defining keys like 'company_name'.

Related errors


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

Appendix: 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 3aec848f28)