Mem0MemoryRetriever
Retrieves long-term memories from Mem0 as ChatMessage objects.
| Most common position in a pipeline | Before an Agent or Chat Generator in memory-augmented pipelines |
| Mandatory init variables | memory_store: A Mem0MemoryStore instance |
| Mandatory run variables | query: A text query or None; at least one Mem0 scope through user_id, run_id, agent_id, app_id, or filters |
| Output variables | memories: A list of ChatMessage objects |
| Mem0 API docs | Search Memories, Memory Filters |
| GitHub link | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mem0 |
| Package name | mem0-haystack |
Overview
Mem0MemoryRetriever retrieves memories from a Mem0MemoryStore and returns them as system ChatMessage objects.
Use it to inject long-term memory into an Agent or a chat generation pipeline before the model produces a response.
The query input can be a string or None.
When query is a string, the component searches for relevant memories and applies top_k.
When query is None, it returns all memories matching the provided scope.
Scope the retrieval with at least one Mem0 entity ID: user_id, run_id, agent_id, or app_id.
You can also pass Haystack-style filters; when filters and ID parameters are both provided, they are combined with an AND condition.
For general filter syntax, see Metadata Filtering.
User-provided Mem0 metadata is included in each returned message's meta.
Mem0 retrieval fields such as memory_id, user_id, score, and timestamps are included under meta["mem0"].
Installation
Install the Mem0 integration:
Set your Mem0 API key:
Usage
On its own
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.retrievers.mem0 import Mem0MemoryRetriever
from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore
store = Mem0MemoryStore()
store.add_memories(
messages=[ChatMessage.from_user("Alice prefers concise Python examples.")],
user_id="alice",
infer=False,
)
retriever = Mem0MemoryRetriever(memory_store=store, top_k=3)
result = retriever.run(query="answer style", user_id="alice")
memories = result["memories"]
for memory in memories:
print(memory.text)
To retrieve all memories in scope, pass query=None:
In a Pipeline
This example retrieves memories, prepends them to the current user message, and passes the combined message list to an Agent.
from haystack import Pipeline
from haystack.components.agents import Agent
from haystack.components.converters import OutputAdapter
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.retrievers.mem0 import Mem0MemoryRetriever
from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore
store = Mem0MemoryStore()
pipeline = Pipeline()
pipeline.add_component("retriever", Mem0MemoryRetriever(memory_store=store, top_k=5))
pipeline.add_component(
"memory_context",
OutputAdapter(
template="{{ memories + user_messages }}",
output_type=list[ChatMessage],
unsafe=True,
),
)
pipeline.add_component(
"agent",
Agent(
chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
system_prompt=(
"Use any system messages at the start of the conversation as long-term memory. "
"Answer concisely."
),
streaming_callback=print_streaming_chunk,
),
)
pipeline.connect("retriever.memories", "memory_context.memories")
pipeline.connect("memory_context.output", "agent.messages")
query = "Give me a short implementation tip."
pipeline.run(
{
"retriever": {
"query": query,
"user_id": "alice",
},
"memory_context": {
"user_messages": [
ChatMessage.from_user(query),
],
},
},
)