Mintplex-Labs/anything-llm · warning
Key is required
Error message
Key is required
What it means
_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.
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
Example fix
// before
await SystemPromptVariables.create({ value: 'abc', description: 'd' }); // throws: Key is required
// after
await SystemPromptVariables.create({ key: 'my_key', value: 'abc', description: 'd' }); Defensive patterns
Strategy: validation
Validate before calling
function hasKey(payload) {
return typeof payload?.key === 'string' && payload.key.length > 0;
}
if (!hasKey(req.body)) {
return res.status(400).json({ error: 'key is required' });
} Type guard
const hasValidKeyShape = (p) => p !== null && typeof p === 'object' && typeof p.key === 'string' && p.key.length > 0;
Try / catch
try {
await SystemPromptVariables.create(payload);
} catch (err) {
if (err.message === 'Key is required') {
return res.status(400).json({ error: 'Provide a key field' });
}
throw err;
} Prevention
- 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
When it happens
Trigger: 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 ''.
Common situations: 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.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Device OS and name are required
- Key cannot start with 'system.'
- Key cannot start with 'user.'
- Key must be a string
- Key must be at least 3 characters
AI-assisted analysis of Mintplex-Labs/anything-llm@3aec848f28 (2026-08-18).
Data as JSON: /api/errors/29e05bc072936a42.
Report an issue: GitHub.
Appendix: source
Thrown at server/models/systemPromptVariables.js:357
} else {
result = result.replace(match, variable.value || match);
}
}
return result;
} catch (error) {
console.error("Error in expandSystemPromptVariables:", error);
return str;
}
},
/**
* Internal function to check if a variable key is valid
* @param {string} key
* @param {boolean} checkExisting
* @returns {Promise<boolean>}
*/
_checkVariableKey: async function (key = null, checkExisting = true) {
if (!key) throw new Error("Key is required");
if (typeof key !== "string") throw new Error("Key must be a string");
if (!/^[a-zA-Z0-9_]+$/.test(key))
throw new Error("Key must contain only letters, numbers and underscores");
if (key.length > 255)
throw new Error("Key must be less than 255 characters");
if (key.length < 3) throw new Error("Key must be at least 3 characters");
if (key.startsWith("user."))
throw new Error("Key cannot start with 'user.'");
if (key.startsWith("system."))
throw new Error("Key cannot start with 'system.'");
if (checkExisting && (await this.get(key)) !== null)
throw new Error("System prompt variable with this key already exists");
return true;
},
};
module.exports = { SystemPromptVariables };View on GitHub (pinned to 3aec848f28)