AzureAIMemoryRetriever()LangChain retriever that queries Foundry Memory with multi-turn context.
This retriever is designed for close coupling with
AzureAIMemoryChatMessageHistory. When bound to a history instance via
history_ref, it provides incremental search capabilities with multi-turn
conversation context. Use standalone mode only for one-off queries without
conversation context.
This retriever queries Azure AI Foundry Memory, supporting both standalone
retrieval and history-bound incremental search with previous_search_id.
Examples
Standalone retriever (one-off search without context):
from azure.identity import DefaultAzureCredential
retriever = AzureAIMemoryRetriever(
project_endpoint="https://myproject.api.azureml.ms",
credential=DefaultAzureCredential(),
store_name="my_store",
scope="user:123",
k=5,
)
docs = retriever.invoke("What are my coffee preferences?")
With endpoint from environment variable:
retriever = AzureAIMemoryRetriever(
store_name="my_store",
scope="user:123",
k=5,
)
docs = retriever.invoke("What are my preferences?")
History-bound retriever (recommended):
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_azure_ai.chat_history import (
AzureAIMemoryChatMessageHistory,
)
history = AzureAIMemoryChatMessageHistory(
project_endpoint="https://myproject.api.azureml.ms",
store_name="my_store",
scope="user:123",
base_history=InMemoryChatMessageHistory(),
)
retriever = history.get_retriever(k=5)
docs = retriever.invoke("Tell me more")Memory store name.
Memory scope (e.g., user or tenant ID).
Optional session identifier for this retriever.
Maximum number of memories to retrieve.
Azure AI project endpoint.
Azure credential for authentication.
Optional reference to a AzureAIMemoryChatMessageHistory instance.