The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.
Python agentic-memoryagentic-memory-systemagentsaiai-agentschatgptgenaillmlong-term-memorymemorymemory-managementpythonragstate-management
fix(ts-oss): reject empty/blank messages in Memory.add() to prevent hallucinated memories (#5545)
Bartok1751e7bff9d0
diff --git a/mem0-ts/src/oss/src/memory/index.ts b/mem0-ts/src/oss/src/memory/index.tsindex 4768c635e..a8dde5d01 100644--- a/mem0-ts/src/oss/src/memory/index.ts+++ b/mem0-ts/src/oss/src/memory/index.ts@@ -619,6 +619,25 @@ export class Memory {"messages is required and cannot be undefined or null. Provide a string or array of messages.",);}+ if (Array.isArray(messages)) {+ if (messages.length === 0) {+ throw new Error(+ "messages array cannot be empty. Provide at least one message with non-empty content.",+ );+ }+ const allBlank = messages.every(+ (m) => typeof m.content === "string" && m.content.trim() === "",+ );+ if (allBlank) {+ throw new Error(+ "messages array cannot contain only blank content. Provide at least one message with non-empty content.",+ );+ }+ } else if (messages.trim() === "") {+ throw new Error(+ "messages string cannot be empty. Provide non-empty content.",+ );+ }const temporalUsageNotice = detectTemporalUsageFromMetadata(config?.metadata,diff --git a/mem0-ts/src/oss/tests/memory.validation.test.ts b/mem0-ts/src/oss/tests/memory.validation.test.tsindex 1227d930c..73808ba95 100644--- a/mem0-ts/src/oss/tests/memory.validation.test.ts+++ b/mem0-ts/src/oss/tests/memory.validation.test.ts@@ -84,6 +84,24 @@ describe("Memory Input Validation", () => {memory.add(null, { userId: testUserId }),).rejects.toThrow("messages is required");});++ it("should throw error when messages is an empty array", async () => {+ await expect(memory.add([], { userId: testUserId })).rejects.toThrow(+ "messages array cannot be empty",+ );+ });++ it("should throw error when messages array contains only blank content", async () => {+ await expect(+ memory.add([{ role: "user", content: " " }], { userId: testUserId }),+ ).rejects.toThrow("messages array cannot contain only blank content");+ });++ it("should throw error when messages is an empty string", async () => {+ await expect(memory.add(" ", { userId: testUserId })).rejects.toThrow(+ "messages string cannot be empty",+ );+ });});describe("search() threshold validation", () => {