Mem0MemoryWriter
Writes ChatMessage objects to Mem0 as long-term memories.
| Most common position in a pipeline | After an Agent 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 |
| GitHub link | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mem0 |
| Package name | mem0-haystack |
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=Truelets 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=Falsestores 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:
Set your Mem0 API key:
Usage
On its own
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.
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"])