{"record":{"id":"dfb9e1c511811186","repo":"mem0ai/mem0","slug":"invalid-name-cannot-contain-whitespace-provid","errorCode":null,"errorMessage":"Invalid ${name}: cannot contain whitespace. Provide a valid identifier without spaces.","messagePattern":"Invalid (.+?): cannot contain whitespace\\. Provide a valid identifier without spaces\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/memory/index.ts","lineNumber":173,"sourceCode":" * - 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)) {\n      throw new Error(\"threshold must be a valid number\");\n    }\n    if (threshold < 0 || threshold > 1) {\n      throw new Error(\n        `Invalid threshold: ${threshold}. Must be between 0 and 1 (inclusive).`,","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/memory/index.ts#L155-L191","documentation":"Thrown by validateAndTrimEntityId when an entity id trims to a non-empty string but still contains internal whitespace (spaces, tabs, newlines). Because ids are used as vector-store and history keys, embedded whitespace is rejected to prevent subtle matching/key bugs; the error names the offending parameter.","triggerScenarios":"Passing filters: { userId: 'user 1' }, { agentId: 'agent\\t1' }, or an id containing a newline — any /\\s/ match inside the trimmed value. Common when ids are built by concatenation with spaces or taken from free-text fields instead of true identifiers.","commonSituations":"Using display names or emails with spaces as ids ('Ada Lovelace'), concatenating fields with ' ' separators, pasted ids with stray whitespace or line breaks, or generating ids via template literals that include spaces.","solutions":["Use a real identifier: slugify or hash free-text ('ada-lovelace', or a UUID) before passing it as an entity id.","If concatenating fields, join with a safe separator like '-' or '_'.","Sanitize once at the boundary: id.trim().replace(/\\s+/g, '-') before calling Memory APIs.","Prefer stable machine ids (UUIDs, database keys) over human-readable names."],"exampleFix":"// before\nawait memory.add(text, { filters: { userId: `${firstName} ${lastName}` } }); // 'Ada Lovelace' throws\n\n// after\nconst userId = `${firstName}-${lastName}`.trim().replace(/\\s+/g, '-');\nawait memory.add(text, { filters: { userId } }); // 'Ada-Lovelace'","handlingStrategy":"type-guard","validationCode":"function slugifyId(value: string): string {\n  return value.trim().replace(/\\s+/g, '-');\n}","typeGuard":"function isWhitespaceFreeId(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 contain whitespace/.test(err.message)) {\n    await memory.add(text, { filters: { userId: userId.replace(/\\s+/g, '-') } });\n    return;\n  }\n  throw err;\n}","preventionTips":["Prefer UUIDs or database keys as entity ids over names/emails.","Build composite ids with '-' or '_' separators, never spaces.","Sanitize all externally sourced ids through a single slugify step."],"tags":["validation","entity-id","filters","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}