Mintplex-Labs/anything-llm · warning

Invalid scope

Error message

Invalid scope: ${JSON.stringify(v)}

What it means

Memory's scope validator rejects any value not in VALID_SCOPES (['workspace', 'global']). It runs when creating or updating memory entries; the parameter defaults to 'workspace' only when omitted entirely, so an explicit wrong value - including null - reaches this throw. The message embeds JSON.stringify(v) so you can see exactly what was received.

Solutions

  1. Use exactly 'workspace' or 'global', lowercase
  2. Omit the scope argument when you want the 'workspace' default
  3. Normalize/validate scope on the client against the same allowed list

Example fix

// before
Memory.create({ userId, workspaceId, scope: 'Workspace', content: '...' }); // throws: Invalid scope: "Workspace"

// after
Memory.create({ userId, workspaceId, scope: 'workspace', content: '...' });
Defensive patterns

Strategy: type-guard

Validate before calling

const VALID_SCOPES = ['workspace', 'global'];

if (scope !== undefined && !VALID_SCOPES.includes(scope)) {
  return res.status(400).json({ error: `scope must be one of ${VALID_SCOPES.join(', ')}` });
}

Type guard

/** @param {unknown} v */
function isMemoryScope(v) {
  return v === 'workspace' || v === 'global';
}

// usage: if (scope !== undefined && !isMemoryScope(scope)) throw new RangeError('bad scope');

Try / catch

try {
  await Memory.create({ userId, workspaceId, scope, content });
} catch (err) {
  if (/Invalid scope:/.test(err.message)) {
    return res.status(400).json({ error: 'scope must be workspace or global' });
  }
  throw err;
}

Prevention

When it happens

Trigger: Passing 'Workspace' or 'GLOBAL' (case-sensitive check); using 'document' or 'user' as a scope; explicitly passing null (JSON.stringify shows "null"); values from an unvalidated enum in the client.

Common situations: Client enums drifting from the server's allowed list; casing differences after a refactor; passing 0, false, or other falsy garbage from form defaults.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at server/models/memory.js:35

  if (!Number.isInteger(n))
    throw new Error(`Expected integer, got ${JSON.stringify(v)}`);
  return n;
}

const Memory = {
  GLOBAL_LIMIT: 5,
  WORKSPACE_LIMIT: 20,
  MAX_INJECTED_WORKSPACE_LIMIT: 5,
  VALID_SCOPES: ["workspace", "global"],

  validations: {
    id: (v) => toInt(v),
    userId: (v = null) => (v === null || v === undefined ? null : toInt(v)),
    workspaceId: (v = null) =>
      v === null || v === undefined ? null : toInt(v),
    scope: (v = "workspace") => {
      if (!Memory.VALID_SCOPES.includes(v))
        throw new Error(`Invalid scope: ${JSON.stringify(v)}`);
      return v;
    },
    content: (v) => {
      if (typeof v !== "string" || v.trim().length === 0)
        throw new Error("Content must be a non-empty string");
      return v;
    },
  },

  /**
   * List a user's workspace-scoped memories, newest first.
   * @param {number|null} userId
   * @param {number} workspaceId
   * @returns {Promise<Memory[]>}
   */
  forUserWorkspace: async function (userId, workspaceId) {
    try {
      const memories = await prisma.memories.findMany({

View on GitHub (pinned to f92433b4ea)