Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

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 ChatMessage system messages, ready to be injected into a chat history.
  • Supports scoping by user_id, run_id, agent_id, or app_id to serve multiple users from one pipeline.
  • Pass query=None to 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) under meta["mem0"].

Configuration​

  1. Drag the Mem0MemoryRetriever component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. On the General tab:
    • Configure the Mem0MemoryStore with your Mem0 API credentials.
    • Set top_k to control the maximum number of memories to retrieve.

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​

ParameterTypeDescription
queryOptional[str]Text query used to search for relevant memories. Pass None to retrieve all memories within the given scope.
user_idOptional[str]User ID to scope the search.
run_idOptional[str]Run ID to scope the search.
agent_idOptional[str]Agent ID to scope the search.
app_idOptional[str]App ID to scope the search.
filtersOptional[Dict[str, Any]]Haystack-style filters to apply. Combined with entity ID parameters when both are provided.
top_kOptional[int]Maximum number of memories to return. Overrides the init-time default.

Outputs​

ParameterTypeDescription
memoriesList[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:

ParameterTypeDefaultDescription
memory_storeMem0MemoryStoreAn instance of Mem0MemoryStore to retrieve memories from.
top_kint5Default 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.

ParameterTypeDefaultDescription
queryOptional[str]Text query to search for relevant memories. Pass None to retrieve all memories in scope.
user_idOptional[str]NoneUser ID to scope the search.
run_idOptional[str]NoneRun ID to scope the search.
agent_idOptional[str]NoneAgent ID to scope the search.
app_idOptional[str]NoneApp ID to scope the search.
filtersOptional[Dict[str, Any]]NoneHaystack-style filters. Combined with entity ID parameters when both are provided.
top_kOptional[int]NoneOverride the init-time top_k setting.