From 4065e846f6d3b42041d93790dbba46341b2a49e8 Mon Sep 17 00:00:00 2001 From: "Md. Mamun Hossain" Date: Thu, 18 Jun 2026 17:31:02 +0600 Subject: [PATCH] =?UTF-8?q?fix(ts-sdk):=20prevent=20hallucinated=20memorie?= =?UTF-8?q?s=20on=20empty=20messages=20payload=20=E2=80=A6=20(#5613)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mem0-ts/src/client/mem0.ts | 5 +++++ mem0-ts/src/client/tests/memoryClient.crud.test.ts | 13 ++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/mem0-ts/src/client/mem0.ts b/mem0-ts/src/client/mem0.ts index b2d1cca85..713650d34 100644 --- a/mem0-ts/src/client/mem0.ts +++ b/mem0-ts/src/client/mem0.ts @@ -253,6 +253,11 @@ export default class MemoryClient { messages: Array, options: AddMemoryOptions & Record = {}, ): Promise> { + // Tightly scoped validation guard to resolve #5465 + if (!messages || (Array.isArray(messages) && messages.length === 0)) { + throw new Error("Cannot process an empty messages payload."); + } + if (this.telemetryId === "") await this.ping(); const payload = this._preparePayload(messages, options); diff --git a/mem0-ts/src/client/tests/memoryClient.crud.test.ts b/mem0-ts/src/client/tests/memoryClient.crud.test.ts index 39e3f1733..0da9be2a9 100644 --- a/mem0-ts/src/client/tests/memoryClient.crud.test.ts +++ b/mem0-ts/src/client/tests/memoryClient.crud.test.ts @@ -59,16 +59,15 @@ describe("MemoryClient - add()", () => { expect(getFetchBody(call!).user_id).toBe("user_1"); }); - test("sends empty messages array without crashing", async () => { - const extra = new Map(); - extra.set("/v3/memories/add/", { status: 200, body: [] }); - const mock = setupMockFetch(extra); + test("throws an error when given an empty messages array", async () => { + setupMockFetch(); const client = new MemoryClient({ apiKey: TEST_API_KEY }); - await client.add([], { userId: "u1" }); - const call = findFetchCall(mock, "/v3/memories/add/", "POST"); - expect(getFetchBody(call!).messages).toEqual([]); + //Asserts that the validation guard catches the empty input early + await expect(client.add([], { userId: "u1" })).rejects.toThrow( + "Cannot process an empty messages payload.", + ); }); });