Mem0MemoryRetriever
Retrieve memories from a Mem0MemoryStore as a list of ChatMessage objects for use in AI agent pipelines.
Key Features
- Fetches relevant memories from Mem0 memory storage before passing context to a language model or agent.
- Returns memories as
ChatMessagesystem messages, ready to be injected into a chat history. - Supports scoping by
user_id,run_id,agent_id, orapp_idto serve multiple users from one pipeline. - Pass
query=Noneto retrieve all memories within the given scope instead of doing a relevance search. - Supports Haystack-style filters combinable with entity ID scoping.
- Each returned message carries Mem0 retrieval metadata (
memory_id,user_id,score, timestamps) undermeta["mem0"].
Configuration
- Drag the
Mem0MemoryRetrievercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Configure the
Mem0MemoryStorewith your Mem0 API credentials. - Set
top_kto control the maximum number of memories to retrieve.
- Configure the
Connections
Mem0MemoryRetriever receives a text query, typically from the Input component, and optional scoping parameters such as user_id. It returns a list of ChatMessage objects through its memories output. Connect the output to a component that accepts chat messages, such as a chat generator or an agent.
Source Code
To check this component's source code, open retriever.py in the Haystack Core Integrations repository.
Usage Examples
Basic Configuration
Mem0MemoryRetriever:
type: haystack_integrations.components.retrievers.mem0.retriever.Mem0MemoryRetriever
init_parameters:
top_k: 5
memory_store:
type: haystack_integrations.memory_stores.mem0.memory_store.Mem0MemoryStore
init_parameters:
api_key: YOUR_MEM0_API_KEY
Using the Component in a Pipeline
# haystack-pipeline
components:
Mem0MemoryRetriever:
type: haystack_integrations.components.retrievers.mem0.retriever.Mem0MemoryRetriever
init_parameters:
top_k: 5
memory_store:
type: haystack_integrations.memory_stores.mem0.memory_store.Mem0MemoryStore
init_parameters:
api_key: YOUR_MEM0_API_KEY
connections: []
max_runs_per_component: 100
metadata: {}
inputs:
query:
- Mem0MemoryRetriever.query
user_id:
- Mem0MemoryRetriever.user_id
outputs:
memories: Mem0MemoryRetriever.memories
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
query | Optional[str] | Text query used to search for relevant memories. Pass None to retrieve all memories within the given scope. |
user_id | Optional[str] | User ID to scope the search. |
run_id | Optional[str] | Run ID to scope the search. |
agent_id | Optional[str] | Agent ID to scope the search. |
app_id | Optional[str] | App ID to scope the search. |
filters | Optional[Dict[str, Any]] | Haystack-style filters to apply. Combined with entity ID parameters when both are provided. |
top_k | Optional[int] | Maximum number of memories to return. Overrides the init-time default. |
Outputs
| Parameter | Type | Description |
|---|---|---|
memories | List[ChatMessage] | Retrieved memories as ChatMessage system messages. User-provided Mem0 metadata is included in each message's meta. Mem0 retrieval fields such as memory_id, user_id, score, and timestamps are available under meta["mem0"]. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
memory_store | Mem0MemoryStore | An instance of Mem0MemoryStore to retrieve memories from. | |
top_k | int | 5 | Default maximum number of memories to return per query. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Default | Description |
|---|---|---|---|
query | Optional[str] | Text query to search for relevant memories. Pass None to retrieve all memories in scope. | |
user_id | Optional[str] | None | User ID to scope the search. |
run_id | Optional[str] | None | Run ID to scope the search. |
agent_id | Optional[str] | None | Agent ID to scope the search. |
app_id | Optional[str] | None | App ID to scope the search. |
filters | Optional[Dict[str, Any]] | None | Haystack-style filters. Combined with entity ID parameters when both are provided. |
top_k | Optional[int] | None | Override the init-time top_k setting. |
Related Information
Was this page helpful?