Mintplex-Labs/anything-llm · warning · Error
Key is required
Error message
Key is required
What it means
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.
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 526360e320)
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.'.
Example fix
// before
await SystemPromptVariables.create({ value: 'x' });
// after
await SystemPromptVariables.create({ key: 'my_var', value: 'x' }); Defensive patterns
Strategy: validation
Validate before calling
if (typeof key !== 'string' || key.trim().length === 0) {
return res.status(400).json({ error: 'key is required and must be a non-empty string' });
} Type guard
function isValidVariableKey(key) {
return typeof key === 'string' && /^[a-zA-Z0-9_]{3,255}$/.test(key)
&& !key.startsWith('user.') && !key.startsWith('system.');
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid video id!
- Impossible to retrieve Youtube video ID.
- Filename is required!
- Invalid scope: ${JSON.stringify(v)}
- Content must be a non-empty string
AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13).
Data as JSON: /api/errors/29e05bc072936a42.
Report an issue: GitHub.