{"record":{"id":"29e05bc072936a42","repo":"Mintplex-Labs/anything-llm","slug":"key-is-required","errorCode":null,"errorMessage":"Key is required","messagePattern":"Key is required","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"server/models/systemPromptVariables.js","lineNumber":357,"sourceCode":"        } else {\n          result = result.replace(match, variable.value || match);\n        }\n      }\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 };","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/models/systemPromptVariables.js#L339-L375","documentation":"Thrown by the internal _checkVariableKey helper when the key argument is falsy (null, undefined, or empty string). This is the first of several sequential validations on a variable key and runs on both create and update paths (update passes checkExisting=false). It enforces that a key is always supplied before format/length checks.","triggerScenarios":"Creating or updating a system prompt variable without a key field, or with key set to '' / null / undefined. The validator's default (key = null) means omitting the argument entirely also trips it.","commonSituations":"Request body omitted the key field; the UI submitted the form before the key input was filled; a refactor changed the field name so key became undefined; programmatic seed script forgot to set the key.","solutions":["Always supply a non-empty key when creating or updating a variable.","Validate at the API boundary: reject requests with a missing/empty key with a 400.","Check downstream validators too: key must match ^[a-zA-Z0-9_]+$, be 3–255 chars, and not start with 'user.' or 'system.'."],"exampleFix":"// before\nawait SystemPromptVariables.create({ value: 'x' });\n// after\nawait SystemPromptVariables.create({ key: 'my_var', value: 'x' });","handlingStrategy":"validation","validationCode":"if (typeof key !== 'string' || key.trim().length === 0) {\n  return res.status(400).json({ error: 'key is required and must be a non-empty string' });\n}","typeGuard":"function isValidVariableKey(key) {\n  return typeof key === 'string' && /^[a-zA-Z0-9_]{3,255}$/.test(key)\n    && !key.startsWith('user.') && !key.startsWith('system.');\n}","tryCatchPattern":null,"preventionTips":["Validate key presence and format at the API boundary.","Remember the full rule set: 3–255 chars, [A-Za-z0-9_], no 'user.'/'system.' prefix.","Make the key field required in the UI form."],"tags":["system-prompt-variables","validation","input-validation","required-field"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}