{"record":{"id":"faf99a2713e845d8","repo":"mem0ai/mem0","slug":"messages-is-required-and-cannot-be-undefined-or-nu","errorCode":null,"errorMessage":"messages is required and cannot be undefined or null. Provide a string or array of messages.","messagePattern":"messages is required and cannot be undefined or null\\. Provide a string or array of messages\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/memory/index.ts","lineNumber":736,"sourceCode":"  }\n\n  async add(\n    messages: string | Message[],\n    config: AddMemoryOptions,\n  ): Promise<SearchResult> {\n    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() === \"\") {","sourceCodeStart":718,"sourceCodeEnd":754,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/memory/index.ts#L718-L754","documentation":"The first input validation in Memory.add(): messages must be a string or a Message[] array, and passing undefined or null throws immediately with this message. It exists to fail fast with an actionable message instead of letting a TypeError surface deep in the LLM/embedding pipeline.","triggerScenarios":"Calling memory.add(undefined, ...), memory.add(null, { filters: {...} }), or passing an optional variable that was never assigned — e.g. profile?.bio where bio is missing, or messages dropped between service layers.","commonSituations":"Optional chaining producing undefined (data?.messages), default parameters omitted, deserialization of a request body whose field is absent, or async race where the payload hasn't loaded when add() is called.","solutions":["Guard at the call site: if (!messages) throw your own input error before calling add().","Fix the upstream data flow so the variable actually contains the conversation text/array.","Type the parameter as string | Message[] (not optional) so the compiler catches missing args.","Validate request payloads at your API boundary instead of relying on the SDK to catch them."],"exampleFix":"// before\nawait memory.add(body.messages, { filters: { userId } }); // body.messages undefined -> throws\n\n// after\nif (body.messages == null) {\n  throw new TypeError('messages is required');\n}\nawait memory.add(body.messages, { filters: { userId } });","handlingStrategy":"type-guard","validationCode":"function hasMessages(messages: unknown): boolean {\n  return typeof messages === 'string' || Array.isArray(messages);\n}","typeGuard":"function isAddableInput(messages: unknown): messages is string | { role: string; content: string }[] {\n  if (typeof messages === 'string') return messages.length > 0;\n  return Array.isArray(messages) && messages.length > 0 && messages.every(\n    (m) => m && typeof (m as any).content === 'string',\n  );\n}","tryCatchPattern":null,"preventionTips":["Type the argument as string | Message[] (non-optional) at your own function boundaries.","Validate request payloads before they reach Memory instead of relying on SDK checks.","Beware optional chaining: data?.messages is undefined when the field is absent."],"tags":["validation","input","add","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}