{"record":{"id":"b2b0cba2fdb5a56a","repo":"mem0ai/mem0","slug":"cannot-process-an-empty-messages-payload","errorCode":null,"errorMessage":"Cannot process an empty messages payload.","messagePattern":"Cannot process an empty messages payload\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"mem0-ts/src/client/mem0.ts","lineNumber":322,"sourceCode":"    } catch (error: any) {\n      // Pass through structured exceptions and APIError\n      if (error instanceof MemoryError || error instanceof APIError) {\n        throw error;\n      } else {\n        throw new APIError(\n          `Failed to ping server: ${error.message || \"Unknown error\"}`,\n        );\n      }\n    }\n  }\n\n  async add(\n    messages: Array<Message>,\n    options: AddMemoryOptions & Record<string, any> = {},\n  ): Promise<Array<Memory>> {\n    // Tightly scoped validation guard to resolve #5465\n    if (!messages || (Array.isArray(messages) && messages.length === 0)) {\n      throw new Error(\"Cannot process an empty messages payload.\");\n    }\n\n    const payload = this._preparePayload(messages, options);\n    const payloadKeys = Object.keys(payload);\n    this._captureEvent(\"add\", [payloadKeys]);\n\n    const response = await this._fetchWithErrorHandling(\n      `${this.host}/v3/memories/add/`,\n      {\n        method: \"POST\",\n        headers: this.headers,\n        body: JSON.stringify(payload),\n      },\n    );\n    return response;\n  }\n\n  async update(","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/client/mem0.ts#L304-L340","documentation":"MemoryClient.add() refuses a messages argument that is null/undefined or an empty array before building any payload (the comment marks this as the guard for issue #5465). Mem0's add endpoint requires at least one conversation message to extract memories from, so the client validates this client-side rather than shipping an empty POST.","triggerScenarios":"await client.add([]) or await client.add(null as any) — e.g. piping a chat history array that turned out empty, filtering messages down to nothing, or calling add unconditionally after every turn including turns with no transcript.","commonSituations":"Guard-less loops that call add() even on first message before history exists; upstream UI bug producing empty conversation arrays; test harnesses calling add with placeholder data.","solutions":["Skip the call when there is nothing to add: if (messages.length) await client.add(messages).","Fix the upstream producer so the conversation array is actually populated (check the filter/map that built it).","If using a string convenience form, pass at least one message object: [{ role: 'user', content: '...' }]."],"exampleFix":"// before\nawait client.add(messages); // messages may be []\n\n// after\nif (messages?.length) {\n  await client.add(messages);\n}","handlingStrategy":"validation","validationCode":"function assertNonEmptyMessages(messages: Array<{ role: string; content: string }>): void {\n  if (!messages || messages.length === 0) throw new Error('add() requires at least one message');\n}","typeGuard":"const isNonEmptyMessages = (m: unknown): m is Array<{ role: string; content: string }> =>\n  Array.isArray(m) && m.length > 0;","tryCatchPattern":null,"preventionTips":["Guard every add() call with a length check.","Build message arrays from a single source of truth so they cannot silently empty via filters.","Cover the empty-input path in unit tests for conversation pipelines."],"tags":["validation","add-memories","empty-input"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}