{"record":{"id":"55bc12517ff5e942","repo":"mem0ai/mem0","slug":"messages-string-cannot-be-empty-provide-non-empty","errorCode":null,"errorMessage":"messages string cannot be empty. Provide non-empty content.","messagePattern":"messages string cannot be empty\\. Provide non-empty content\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/memory/index.ts","lineNumber":755,"sourceCode":"        \"messages is required and cannot be undefined or null. Provide a string or array of messages.\",\n      );\n    }\n    if (Array.isArray(messages)) {\n      if (messages.length === 0) {\n        throw new Error(\n          \"messages array cannot be empty. Provide at least one message with non-empty content.\",\n        );\n      }\n      const allBlank = messages.every(\n        (m) => typeof m.content === \"string\" && m.content.trim() === \"\",\n      );\n      if (allBlank) {\n        throw new Error(\n          \"messages array cannot contain only blank content. Provide at least one message with non-empty content.\",\n        );\n      }\n    } else if (messages.trim() === \"\") {\n      throw new Error(\n        \"messages string cannot be empty. Provide non-empty content.\",\n      );\n    }\n\n    const temporalUsageNotice = detectTemporalUsageFromMetadata(\n      config?.metadata,\n    );\n\n    await this._ensureInitialized();\n    await this._captureEvent(\"add\", {\n      message_count: Array.isArray(messages) ? messages.length : 1,\n      has_metadata: !!config.metadata,\n      has_filters: !!config.filters,\n      infer: config.infer,\n    });\n    const { filters = {}, infer = true } = config;\n    const metadata = stripIdentityKeys(config.metadata);\n","sourceCodeStart":737,"sourceCodeEnd":773,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/memory/index.ts#L737-L773","documentation":"Thrown by the OSS Memory.add() method when messages is a plain string that is empty or contains only whitespace. As with the array form, the SDK validates before calling the LLM or embedder. It exists so callers get a clear error instead of a nonsense LLM extraction or an embedding failure downstream.","triggerScenarios":"Calling memory.add('') or memory.add('   \\n\\t') — any string where messages.trim() === ''. Array inputs never hit this branch.","commonSituations":"Passing a variable that was never assigned (empty template literal); forwarding chat input without trimming; a pipeline step that collapses the message to an empty string.","solutions":["Guard before calling: if (!text.trim()) skip or prompt the user","Log the raw input length when it fails to find where the empty string originates","If the string comes from JSON, verify the field exists and was parsed, not undefined stringified later"],"exampleFix":"// before\nawait memory.add(userText); // userText === '  '\n\n// after\nif (userText.trim()) {\n  await memory.add(userText);\n}","handlingStrategy":"validation","validationCode":"if (typeof messages === 'string' && !messages.trim()) {\n  // nothing to store — skip the add() call\n  return;\n}","typeGuard":"const isNonEmptyString = (s: unknown): s is string =>\n  typeof s === 'string' && s.trim() !== '';","tryCatchPattern":"try { await memory.add(text, { userId }); } catch (e) { if (e instanceof Error && e.message.includes('cannot be empty')) return; throw e; }","preventionTips":["Centralize a guard: if (!text.trim()) return; before every add()","Log input lengths at ingestion to catch empty strings early in the pipeline"],"tags":["validation","typescript","oss","memory-add"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}