{"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":null,"httpStatus":500,"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/3aec848f2885144aa8f1e53b9731a04310d5d558/server/models/systemPromptVariables.js#L339-L375","documentation":"_checkVariableKey is the shared validator for system-prompt variable keys used by create and update. It throws 'Key is required' when the key argument is falsy - undefined, null, or empty string. In practice the request payload or function call omitted the key field entirely.","triggerScenarios":"POST/PUT to system-prompt-variables without a `key` field in the body; calling SystemPromptVariables.create({ value, description }) with key missing; key set to empty string ''.","commonSituations":"Form submissions that only mark optional fields; API clients built from a partial example; destructuring payload with a renamed property so key ends up undefined.","solutions":["Include a non-empty `key` in the request payload or function call","Validate payload shape at the API boundary before invoking the model","If the key comes from user input, require it in form/UI validation before submit"],"exampleFix":"// before\nawait SystemPromptVariables.create({ value: 'abc', description: 'd' }); // throws: Key is required\n\n// after\nawait SystemPromptVariables.create({ key: 'my_key', value: 'abc', description: 'd' });","handlingStrategy":"validation","validationCode":"function hasKey(payload) {\n  return typeof payload?.key === 'string' && payload.key.length > 0;\n}\n\nif (!hasKey(req.body)) {\n  return res.status(400).json({ error: 'key is required' });\n}","typeGuard":"const hasValidKeyShape = (p) =>\n  p !== null && typeof p === 'object' && typeof p.key === 'string' && p.key.length > 0;","tryCatchPattern":"try {\n  await SystemPromptVariables.create(payload);\n} catch (err) {\n  if (err.message === 'Key is required') {\n    return res.status(400).json({ error: 'Provide a key field' });\n  }\n  throw err;\n}","preventionTips":["Require the key field in form and API-contract validation before the model layer","Destructure payloads explicitly so a renamed field fails loudly at the boundary","Keep one canonical name for the field ('key') across client and server"],"tags":["system-prompt","validation","required-field"],"backgroundTag":"missing-required-argument","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"}