{"record":{"id":"bcb7a33b150cb06e","repo":"mem0ai/mem0","slug":"sessionid-is-required-for-mem0memory","errorCode":null,"errorMessage":"sessionId is required for Mem0Memory","messagePattern":"sessionId is required for Mem0Memory","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/community/src/integrations/langchain/mem0.ts","lineNumber":177,"sourceCode":"\n  aiPrefix = \"AI\";\n\n  mem0Client: InstanceType<typeof MemoryClient>;\n\n  memoryOptions: AddMemoryOptions | SearchMemoryOptions | GetAllMemoryOptions;\n\n  mem0Options: ClientOptions;\n\n  // Whether to return separate messages for chat history with a SystemMessage containing (facts and summary) or return a single HumanMessage with the entire memory context.\n  // Defaults to false (return a single HumanMessage) in order to allow more flexibility with different models.\n  separateMessages?: boolean;\n\n  constructor(fields: Mem0MemoryInput) {\n    if (!fields.apiKey) {\n      throw new Error(\"apiKey is required for Mem0Memory\");\n    }\n    if (!fields.sessionId) {\n      throw new Error(\"sessionId is required for Mem0Memory\");\n    }\n\n    super({\n      returnMessages: fields?.returnMessages ?? false,\n      inputKey: fields?.inputKey,\n      outputKey: fields?.outputKey,\n    });\n\n    this.apiKey = fields.apiKey;\n    this.sessionId = fields.sessionId;\n    this.humanPrefix = fields.humanPrefix ?? this.humanPrefix;\n    this.aiPrefix = fields.aiPrefix ?? this.aiPrefix;\n    this.memoryOptions = fields.memoryOptions ?? {};\n    this.mem0Options = fields.mem0Options ?? {\n      apiKey: this.apiKey,\n    };\n    this.separateMessages = fields.separateMessages ?? false;\n    try {","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/community/src/integrations/langchain/mem0.ts#L159-L195","documentation":"Thrown by the Mem0Memory LangChain integration constructor when fields.sessionId is falsy. The sessionId is the memory namespace key: all memories are scoped to it via memoryOptions, so an absent session would silently read/write a shared undefined bucket. The constructor refuses to continue without it.","triggerScenarios":"new Mem0Memory({ apiKey, ... }) with no sessionId, or a sessionId computed from a request context that is undefined (e.g. req.query.userId when the param is absent, or building the memory inside a handler before the session is established).","commonSituations":"Server-side chat routes where sessionId should come from the authenticated user or conversation ID; agent frameworks that instantiate memory once at boot with no session context; renaming userId->sessionId after a docs example.","solutions":["Pass a stable per-user/conversation identifier: new Mem0Memory({ apiKey, sessionId: userId })","If session varies per request, construct or reconfigure the memory per request rather than as a singleton","Derive it defensively at the call site: sessionId: session?.id ?? throw, or assert the value before construction"],"exampleFix":"// before\nconst memory = new Mem0Memory({ apiKey: process.env.MEM0_API_KEY! });\n\n// after\nconst memory = new Mem0Memory({\n  apiKey: process.env.MEM0_API_KEY!,\n  sessionId: session.userId,\n});","handlingStrategy":"validation","validationCode":"const sessionId = session?.userId;\nif (!sessionId) throw new Error('sessionId (user/conversation id) is required');\nconst memory = new Mem0Memory({ apiKey, sessionId });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive sessionId from the authenticated user or conversation ID, never from an optional query param","Construct Mem0Memory per request when the session varies; do not cache a single instance across users","Use a startup assert so misconfigured routes fail in CI, not in production"],"tags":["langchain","integration","validation","session","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}