Mintplex-Labs/anything-llm · warning

Key must be less than 255 characters

Error message

Key must be less than 255 characters

What it means

Validation guard in _checkVariableKey: the supplied system-prompt variable key exceeds the 255-character limit enforced to keep keys safe for storage and template rendering. Fires when caller passes an overly long key string.

Solutions

  1. Cap generated keys with a length limit (e.g. key.slice(0, 255)) and ensure uniqueness within it
  2. Move long text into the variable's `value` field, keeping the key a short identifier
  3. Hash or slugify long sources before using them as keys

Example fix

// before
const key = `section_${JSON.stringify(section)}`; // easily exceeds 255 chars

// after
const key = `section_${slugify(section.name).slice(0, 40)}`;
Defensive patterns

Strategy: validation

Validate before calling

const MAX_KEY_LEN = 255;

if (typeof key === 'string' && key.length > MAX_KEY_LEN) {
  return res.status(400).json({ error: `Key must be at most ${MAX_KEY_LEN} characters` });
}

Try / catch

try {
  await SystemPromptVariables.create({ key, value });
} catch (err) {
  if (/less than 255 characters/.test(err.message)) {
    return res.status(400).json({ error: 'Key too long; shorten it' });
  }
  throw err;
}

Prevention

When it happens

Trigger: Keys generated by concatenating structured data (JSON.stringify of a config section) into the key; hashed or base64-encoded values used as keys; long descriptive labels pasted into the key field.

Common situations: Programmatic key generation without a length cap; importing external metadata wholesale as keys; test fixtures with oversized identifiers.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@3aec848f28 (2026-08-18). Data as JSON: /api/errors/bb0298d0dd9e511f. Report an issue: GitHub.

Appendix: source

Thrown at server/models/systemPromptVariables.js:362

    } 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)