{"record":{"id":"5a1fb5e0e0d2aa95","repo":"Mintplex-Labs/anything-llm","slug":"system-prompt-variable-with-this-key-already-exist","errorCode":null,"errorMessage":"System prompt variable with this key already exists","messagePattern":"System prompt variable with this key already exists","errorType":"validation","errorClass":null,"httpStatus":500,"severity":"warning","filePath":"server/models/systemPromptVariables.js","lineNumber":369,"sourceCode":"   * Internal function to check if a variable key is valid\n   * @param {string} key\n   * @param {boolean} checkExisting\n   * @returns {Promise<boolean>}\n   */\n  _checkVariableKey: async function (key = null, checkExisting = true) {\n    if (!key) throw new Error(\"Key is required\");\n    if (typeof key !== \"string\") throw new Error(\"Key must be a string\");\n    if (!/^[a-zA-Z0-9_]+$/.test(key))\n      throw new Error(\"Key must contain only letters, numbers and underscores\");\n    if (key.length > 255)\n      throw new Error(\"Key must be less than 255 characters\");\n    if (key.length < 3) throw new Error(\"Key must be at least 3 characters\");\n    if (key.startsWith(\"user.\"))\n      throw new Error(\"Key cannot start with 'user.'\");\n    if (key.startsWith(\"system.\"))\n      throw new Error(\"Key cannot start with 'system.'\");\n    if (checkExisting && (await this.get(key)) !== null)\n      throw new Error(\"System prompt variable with this key already exists\");\n\n    return true;\n  },\n};\n\nmodule.exports = { SystemPromptVariables };\n","sourceCodeStart":351,"sourceCodeEnd":376,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/3aec848f2885144aa8f1e53b9731a04310d5d558/server/models/systemPromptVariables.js#L351-L376","documentation":"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.","triggerScenarios":"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.","commonSituations":"Seeding scripts run twice; idempotency-unaware API clients; teams independently defining keys like 'company_name'.","solutions":["If the variable should change, update the existing record by its id instead of creating a new one","Otherwise pick a different, more specific key","Check for existence before creating: `const existing = await SystemPromptVariables.get(key)`","Make seeding scripts idempotent (get-then-update-or-create)"],"exampleFix":"// before\nawait SystemPromptVariables.create({ key: 'company_name', value: 'Acme' }); // throws if key exists\n\n// after\nconst existing = await SystemPromptVariables.get('company_name');\nif (existing) {\n  await SystemPromptVariables.update(existing.id, { key: 'company_name', value: 'Acme' });\n} else {\n  await SystemPromptVariables.create({ key: 'company_name', value: 'Acme' });\n}","handlingStrategy":"validation","validationCode":"const { SystemPromptVariables } = require('../models/systemPromptVariables');\n\nasync function upsertVariable(key, value, description) {\n  const existing = await SystemPromptVariables.get(key);\n  if (existing) {\n    return SystemPromptVariables.update(existing.id, { key, value, description });\n  }\n  return SystemPromptVariables.create({ key, value, description });\n}","typeGuard":null,"tryCatchPattern":"try {\n  await SystemPromptVariables.create({ key, value });\n} catch (err) {\n  if (/already exists/.test(err.message)) {\n    const existing = await SystemPromptVariables.get(key);\n    await SystemPromptVariables.update(existing.id, { key, value });\n  } else throw err;\n}","preventionTips":["Make create flows get-then-update-or-create so retries and double-submits are idempotent","Disable the submit button while a create is in flight to prevent double clicks","Coordinate key names across the team to avoid independent duplicate definitions"],"tags":["system-prompt","validation","duplicate"],"backgroundTag":"duplicate-key-conflict","analyzedSha":"3aec848f2885144aa8f1e53b9731a04310d5d558","analyzedAt":"2026-08-18T10:02:21.017Z","contentChangedAt":"2026-08-18T10:02:21.017Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}