fix(ts-oss): reject empty/blank messages in Memory.add() to prevent hallucinated memories (#5545)

This commit is contained in:
Bartok
2026-06-18 17:36:19 +05:30
committed by GitHub
parent 7ae6a8c36a
commit 1751e7bff9
2 changed files with 37 additions and 0 deletions
+19
View File
@@ -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,
@@ -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", () => {