mem0ai/mem0 · error · Error

sessionId is required for Mem0Memory

Error message

sessionId is required for Mem0Memory

What it means

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.

Source

Thrown at mem0-ts/src/community/src/integrations/langchain/mem0.ts:177

  aiPrefix = "AI";

  mem0Client: InstanceType<typeof MemoryClient>;

  memoryOptions: AddMemoryOptions | SearchMemoryOptions | GetAllMemoryOptions;

  mem0Options: ClientOptions;

  // 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.
  // Defaults to false (return a single HumanMessage) in order to allow more flexibility with different models.
  separateMessages?: boolean;

  constructor(fields: Mem0MemoryInput) {
    if (!fields.apiKey) {
      throw new Error("apiKey is required for Mem0Memory");
    }
    if (!fields.sessionId) {
      throw new Error("sessionId is required for Mem0Memory");
    }

    super({
      returnMessages: fields?.returnMessages ?? false,
      inputKey: fields?.inputKey,
      outputKey: fields?.outputKey,
    });

    this.apiKey = fields.apiKey;
    this.sessionId = fields.sessionId;
    this.humanPrefix = fields.humanPrefix ?? this.humanPrefix;
    this.aiPrefix = fields.aiPrefix ?? this.aiPrefix;
    this.memoryOptions = fields.memoryOptions ?? {};
    this.mem0Options = fields.mem0Options ?? {
      apiKey: this.apiKey,
    };
    this.separateMessages = fields.separateMessages ?? false;
    try {

View on GitHub (pinned to 001c235229)

Solutions

  1. Pass a stable per-user/conversation identifier: new Mem0Memory({ apiKey, sessionId: userId })
  2. If session varies per request, construct or reconfigure the memory per request rather than as a singleton
  3. Derive it defensively at the call site: sessionId: session?.id ?? throw, or assert the value before construction

Example fix

// before
const memory = new Mem0Memory({ apiKey: process.env.MEM0_API_KEY! });

// after
const memory = new Mem0Memory({
  apiKey: process.env.MEM0_API_KEY!,
  sessionId: session.userId,
});
Defensive patterns

Strategy: validation

Validate before calling

const sessionId = session?.userId;
if (!sessionId) throw new Error('sessionId (user/conversation id) is required');
const memory = new Mem0Memory({ apiKey, sessionId });

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/bcb7a33b150cb06e. Report an issue: GitHub.