{"record":{"id":"b5f0064ac438d2c5","repo":"Mintplex-Labs/anything-llm","slug":"key-must-contain-only-letters-numbers-and-undersc","errorCode":null,"errorMessage":"Key must contain only letters, numbers and underscores","messagePattern":"Key must contain only letters, numbers and underscores","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/models/systemPromptVariables.js","lineNumber":360,"sourceCode":"      }\n      return result;\n    } catch (error) {\n      console.error(\"Error in expandSystemPromptVariables:\", error);\n      return str;\n    }\n  },\n\n  /**\n   * 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":342,"sourceCodeEnd":376,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/models/systemPromptVariables.js#L342-L376","documentation":"Thrown by SystemPromptVariables._checkVariableKey when a variable key fails the regex /^[a-zA-Z0-9_]+$/. Keys are used as template placeholders in system prompts, so they must be valid identifiers. The check runs on both create (line 180) and update (line 204) paths, reached via the admin-only POST/PUT /system/prompt-variables endpoints.","triggerScenarios":"POST /system/prompt-variables or PUT /system/prompt-variables/:id with a key containing spaces, hyphens, dots, or any non-[A-Za-z0-9_] character (e.g. 'my-var', 'user name', 'key.1', 'café'). Any value passed through that fails the single-line character class test.","commonSituations":"Admins authoring a prompt variable naturally type hyphenated or human-readable names. Importing/migrating variables from external configs that permit dashes. Frontend form that submits before sanitizing the key field.","solutions":["Replace every non-alphanumeric character in the key with an underscore (e.g. 'my-var' -> 'my_var').","Validate the key client-side with /^[a-zA-Z0-9_]+$/ before submitting to the API.","If you need word separators, use camelCase or snake_case only."],"exampleFix":"// before\nSystemPromptVariables.create({ key: 'my-var', value: '...' });\n// after\nSystemPromptVariables.create({ key: 'my_var', value: '...' });","handlingStrategy":"validation","validationCode":"function isValidVariableKey(key) {\n  return typeof key === 'string'\n    && /^[a-zA-Z0-9_]+$/.test(key)\n    && key.length >= 3 && key.length <= 255\n    && !key.startsWith('user.') && !key.startsWith('system.');\n}","typeGuard":"function isVariableKey(v) {\n  return typeof v === 'string' && /^[a-zA-Z0-9_]+$/.test(v);\n}","tryCatchPattern":"try {\n  await SystemPromptVariables.create({ key, value });\n} catch (e) {\n  if (/letters, numbers and underscores/.test(e.message)) {\n    // surface a field-level error to the user\n  }\n}","preventionTips":["Run the same /^[a-zA-Z0-9_]+$/ regex client-side before submit.","Disable the submit button until the key passes validation.","Auto-sanitize pasted keys by replacing disallowed characters with underscores."],"tags":["validation","system-prompt-variables","user-input"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}