{"record":{"id":"af737eb90ce63926","repo":"mem0ai/mem0","slug":"messages-array-cannot-be-empty-provide-at-least-o","errorCode":null,"errorMessage":"messages array cannot be empty. Provide at least one message with non-empty content.","messagePattern":"messages array cannot be empty\\. Provide at least one message with non-empty content\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/memory/index.ts","lineNumber":742,"sourceCode":"    if (config?.timestamp !== undefined) {\n      await this._getNoticeTelemetryId();\n      throw new Error(\n        await getTemporalFeatureErrorMessage(this, {\n          triggerFunction: \"add\",\n          triggerParameter: \"timestamp\",\n        }),\n      );\n    }\n\n    // Validate messages input\n    if (messages === undefined || messages === null) {\n      throw new Error(\n        \"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(","sourceCodeStart":724,"sourceCodeEnd":760,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/memory/index.ts#L724-L760","documentation":"The second input validation in Memory.add(): when messages is an array it must be non-empty — an empty array throws with this message before any LLM or embedding work starts. It prevents wasting a pipeline run (and API cost) on input that could not produce a memory.","triggerScenarios":"Calling memory.add([], { filters: { userId: 'u1' } }), or passing a filtered/mapped array that ended up empty — e.g. messages.filter(m => m.role === 'user') when no user messages exist.","commonSituations":"Chat pipelines where the transcript hasn't received messages yet (add called at conversation start), arrays emptied by filtering, slicing beyond length, or spreading an empty list; batching code that emits zero-item chunks.","solutions":["Skip the call when the array is empty: if (messages.length === 0) return;","Fix upstream filtering so the array retains intended content; log the pre-call length when debugging.","In chat loops, call add() only after at least one real message exists.","Batch emitters should drop empty chunks before invoking Memory."],"exampleFix":"// before\nawait memory.add(history.filter((m) => m.role === 'user'), opts); // [] when bot-only history -> throws\n\n// after\nconst userMsgs = history.filter((m) => m.role === 'user');\nif (userMsgs.length > 0) {\n  await memory.add(userMsgs, opts);\n}","handlingStrategy":"validation","validationCode":"function hasNonEmptyMessages(messages: unknown): boolean {\n  return Array.isArray(messages) && messages.length > 0;\n}","typeGuard":"function isNonEmptyMessageArray(messages: unknown): messages is { role: string; content: string }[] {\n  return (\n    Array.isArray(messages) &&\n    messages.length > 0 &&\n    messages.some((m) => typeof m?.content === 'string' && m.content.trim() !== '')\n  );\n}","tryCatchPattern":null,"preventionTips":["Skip the add() call when the array is empty — returning early is cheaper than catching.","Log pre-call message counts in chat pipelines to catch empty-transcript bugs.","Make batch emitters drop empty chunks before invoking Memory."],"tags":["validation","input","add","empty-array","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}