---
title: "Mem0MemoryWriter"
id: mem0memorywriter
slug: "/mem0memorywriter"
description: "Writes ChatMessage objects to Mem0 as long-term memories."
---

# Mem0MemoryWriter

Writes `ChatMessage` objects to Mem0 as long-term memories.

<div className="key-value-table">

|  |  |
| --- | --- |
| **Most common position in a pipeline** | After an [`Agent`](../agents-1/agent.mdx) or Chat Generator in memory-augmented pipelines |
| **Mandatory init variables** | `memory_store`: A `Mem0MemoryStore` instance |
| **Mandatory run variables** | `messages`: A list of `ChatMessage` objects; at least one Mem0 scope through `user_id`, `run_id`, `agent_id`, or `app_id` |
| **Output variables** | `memories_written`: The number of memories written |
| **Mem0 API docs** | [Add Memories](https://docs.mem0.ai/api-reference/memory/add-memories) |
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mem0 |
| **Package name** | `mem0-haystack` |

</div>

## Overview

`Mem0MemoryWriter` writes a list of `ChatMessage` objects to a `Mem0MemoryStore`. Use it near the end of a memory-augmented pipeline to persist conversation facts, user preferences, and durable project context for future runs.

Scope written memories with at least one Mem0 entity ID: `user_id`, `run_id`, `agent_id`, or `app_id`. These are runtime inputs, so one pipeline instance can write memories for multiple users, sessions, agents, or applications.

The `infer` init parameter controls how Mem0 stores the incoming messages:

- `infer=True` lets Mem0 extract memories from the messages. This is useful when writing a full Agent turn that includes the user message, tool context, and final assistant response.
- `infer=False` stores the supplied message text as-is. This is useful when the upstream component has already selected the exact memory text.

### Installation

Install the Mem0 integration:

```shell
pip install mem0-haystack
```

Set your Mem0 API key:

```shell
export MEM0_API_KEY="your-mem0-api-key"
```

## Usage

### On its own

```python
from haystack.dataclasses import ChatMessage

from haystack_integrations.components.writers.mem0 import Mem0MemoryWriter
from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore

store = Mem0MemoryStore()
writer = Mem0MemoryWriter(memory_store=store, infer=False)

result = writer.run(
    messages=[ChatMessage.from_user("Alice prefers concise Python examples.")],
    user_id="alice",
)

print(result["memories_written"])
```

### In a Pipeline

This example connects an Agent's full `messages` output to `Mem0MemoryWriter` with `infer=True`, so Mem0 can extract memories from the full turn context.

```python
from haystack import Pipeline
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.generators.utils import print_streaming_chunk
from haystack.dataclasses import ChatMessage

from haystack_integrations.components.writers.mem0 import Mem0MemoryWriter
from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore

store = Mem0MemoryStore()

pipeline = Pipeline()
pipeline.add_component(
    "agent",
    Agent(
        chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
        system_prompt=(
            "Answer the user and preserve durable user facts or preferences for future conversations."
        ),
        streaming_callback=print_streaming_chunk,
    ),
)
pipeline.add_component("writer", Mem0MemoryWriter(memory_store=store, infer=True))

pipeline.connect("agent.messages", "writer.messages")

result = pipeline.run(
    {
        "agent": {
            "messages": [
                ChatMessage.from_user(
                    "My name is Alice and I prefer concise Python examples.",
                ),
            ],
        },
        "writer": {
            "user_id": "alice",
        },
    },
)

print(result["writer"]["memories_written"])
```
