Mem0MemoryStore
Mem0MemoryStore is a memory store backed by the Mem0 cloud API. It is the shared data layer used by Mem0MemoryRetriever, Mem0MemoryWriter, and the Mem0 Memory Tools.
| Used by | Mem0MemoryRetriever, Mem0MemoryWriter, Mem0MemoryRetrieverTool, Mem0MemoryWriterTool |
| Optional init variables | api_key: Defaults to MEM0_API_KEY environment variable |
| API reference | Mem0 |
| GitHub link | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mem0 |
| Package name | mem0-haystack |
Overview
Mem0MemoryStore wraps the Mem0 cloud API and provides two core methods:
add_memories— stores a list ofChatMessageobjects as memories in Mem0.search_memories— retrieves memories from Mem0 that are relevant to a query.
Scope memories with at least one Mem0 entity ID: user_id, run_id, agent_id, or app_id. These are runtime parameters, so a single store instance can serve multiple users or sessions.
The infer parameter on add_memories controls how Mem0 processes incoming messages:
infer=Truelets Mem0 extract memories from the messages automatically. This is useful when storing a full Agent turn.infer=Falsestores the supplied message text as-is. This is useful when the exact memory text has already been selected upstream.
Installation
Install the Mem0 integration:
Set your Mem0 API key:
Usage
On its own
from haystack.dataclasses import ChatMessage
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,
)
memories = store.search_memories(
query="What does Alice prefer?",
user_id="alice",
top_k=3,
)
print([msg.text for msg in memories])
Scoping with multiple entity IDs
Mem0 supports narrowing the scope of reads and writes with user_id, run_id, agent_id, and app_id. Pass any combination at call time:
store.add_memories(
messages=[ChatMessage.from_user("Alice is working on a documentation search system.")],
user_id="alice",
run_id="docs-assistant-session-1",
infer=True,
)
memories = store.search_memories(
query="What project is Alice working on?",
user_id="alice",
run_id="docs-assistant-session-1",
)
print([msg.text for msg in memories])
Retrieving all memories in scope
Pass query=None to return all memories matching the provided scope without a relevance search: