{"record":{"id":"95fc73329f8a8a55","repo":"mem0ai/mem0","slug":"invalid-name-cannot-be-empty-or-whitespace-onl","errorCode":null,"errorMessage":"Invalid ${name}: cannot be empty or whitespace-only. Provide a valid identifier.","messagePattern":"Invalid (.+?): cannot be empty or whitespace-only\\. Provide a valid identifier\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/memory/index.ts","lineNumber":168,"sourceCode":"}\n\n/**\n * Validates and normalizes an entity ID.\n * - Coerces non-string ids (e.g. numeric database keys) to string\n * - Trims leading/trailing whitespace\n * - Rejects empty or whitespace-only strings\n * - Rejects strings containing internal whitespace\n * @returns The trimmed entity ID, or undefined if input is undefined/null\n * @throws Error if entity ID is invalid\n */\nfunction validateAndTrimEntityId(\n  value: string | number | undefined | null,\n  name: string,\n): string | undefined {\n  if (value == null) return undefined;\n  const trimmed = String(value).trim();\n  if (trimmed === \"\") {\n    throw new Error(\n      `Invalid ${name}: cannot be empty or whitespace-only. Provide a valid identifier.`,\n    );\n  }\n  if (/\\s/.test(trimmed)) {\n    throw new Error(\n      `Invalid ${name}: cannot contain whitespace. Provide a valid identifier without spaces.`,\n    );\n  }\n  return trimmed;\n}\n\n/**\n * Validates search parameters.\n * @throws Error if threshold or topK are invalid\n */\nfunction validateSearchParams(threshold?: number, topK?: number): void {\n  if (threshold !== undefined) {\n    if (typeof threshold !== \"number\" || isNaN(threshold)) {","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/memory/index.ts#L150-L186","documentation":"Thrown by validateAndTrimEntityId when an entity id (userId, agentId, runId, etc. under filters) is a string that is empty or contains only whitespace after trimming. The SDK coerces values to strings and trims them, and rejects ids that would be blank because they cannot meaningfully scope memory. The error names which identifier is invalid.","triggerScenarios":"Passing filters: { userId: '' }, { userId: '   ' }, or a value that stringifies to blank (e.g. an empty variable) to add/search/getAll/deleteAll etc. Also filters built from template strings where the variable is unset, like `user-${id}` with id undefined producing literal text — no, blank only when the whole result trims to empty, e.g. String(null) is 'null' but '' or ' ' pass through.","commonSituations":"Defaulting ids to empty string (userId = process.env.USER_ID || ''), reading ids from JSON/headers that are absent, whitespace copied from user input or CSV data, or test fixtures with placeholder blank ids.","solutions":["Provide a non-empty id: filters: { userId: 'u1' } — check the variable actually holds a value before the call.","If the id comes from optional input, guard upstream: if (!id?.trim()) skip or fetch a real id instead of calling with ''.","Replace || '' defaults with meaningful fallbacks or fail-fast validation at the API boundary.","Trim ids once at ingestion so stored scoping is clean."],"exampleFix":"// before\nawait memory.add(text, { filters: { userId: user?.id ?? '' } }); // throws when blank\n\n// after\nconst userId = user?.id?.trim();\nif (!userId) throw new Error('userId required to store memory');\nawait memory.add(text, { filters: { userId } });","handlingStrategy":"type-guard","validationCode":"function requireEntityId(value: unknown, name: string): string {\n  const trimmed = value == null ? '' : String(value).trim();\n  if (trimmed === '') throw new TypeError(`${name} is required`);\n  return trimmed;\n}","typeGuard":"function isValidEntityId(value: unknown): value is string {\n  return typeof value === 'string' && value.trim() !== '' && !/\\s/.test(value.trim());\n}","tryCatchPattern":"try {\n  await memory.add(text, { filters: { userId } });\n} catch (err) {\n  if (err instanceof Error && /cannot be empty or whitespace-only/.test(err.message)) {\n    return skipOrPromptForId(); // recover: ask for the id instead of crashing the flow\n  }\n  throw err;\n}","preventionTips":["Never default ids to '' — throw or skip upstream when the id source is empty.","Trim ids at ingestion from users/CSVs/APIs before they reach Memory calls.","Share one requireEntityId helper across all call sites so validation is uniform."],"tags":["validation","entity-id","filters","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}