{"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":null,"httpStatus":500,"severity":"warning","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/3aec848f2885144aa8f1e53b9731a04310d5d558/server/models/systemPromptVariables.js#L342-L376","documentation":"Validation rule in SystemPromptVariables._checkVariableKey: the proposed variable key failed the ^[a-zA-Z0-9_]+$ regex, meaning it contained characters other than letters, digits, or underscores — such as spaces, dashes, or punctuation — which cannot be used as a substitution key.","triggerScenarios":"Using kebab-case ('my-var') instead of snake_case ('my_var'); keys containing spaces or dots ('company.name'); localized or accented characters ('cafe_zeit'); copy-pasted keys with invisible whitespace.","commonSituations":"Developers habitually using URL-style slugs as identifiers; keys derived from natural-language labels; keys imported from systems that permit richer alphabets.","solutions":["Convert separators to underscores: my-key -> my_var becomes my_var","Strip or replace disallowed characters before submitting","Enforce the same pattern in front-end validation so users get immediate feedback"],"exampleFix":"// before\nSystemPromptVariables.create({ key: 'company-name', value: 'Acme' }); // throws\n\n// after\nSystemPromptVariables.create({ key: 'company_name', value: 'Acme' });","handlingStrategy":"validation","validationCode":"const KEY_RE = /^[a-zA-Z0-9_]+$/;\n\nfunction isValidKeyFormat(key) {\n  return typeof key === 'string' && KEY_RE.test(key);\n}\n\nif (!isValidKeyFormat(key)) {\n  return res.status(400).json({ error: 'Key may only contain letters, numbers, and underscores' });\n}","typeGuard":"const isPlainIdentifier = (v) => typeof v === 'string' && /^[a-zA-Z0-9_]+$/.test(v);","tryCatchPattern":"try {\n  await SystemPromptVariables.create({ key, value });\n} catch (err) {\n  if (/letters, numbers and underscores/.test(err.message)) {\n    return res.status(400).json({ error: 'Use snake_case for keys' });\n  }\n  throw err;\n}","preventionTips":["Standardize on snake_case keys; convert hyphens to underscores at the edge","Mirror the ^[a-zA-Z0-9_]+$ pattern in front-end validation for instant feedback","Beware copy-pasted keys carrying invisible unicode or whitespace - normalize before submit"],"tags":["system-prompt","validation","format"],"backgroundTag":"invalid-characters-in-key","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"}