Azure AI Foundry Memory integration with LangChain.
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")History wrapper that updates Azure AI Foundry Memory per chat turn.
This class decorates a LangChain BaseChatMessageHistory, preserving
short-term transcript storage while forwarding each turn to Foundry Memory
for long-term extraction and consolidation.
Setup:
We will need to set the required environment variables:
export AZURE_AI_PROJECT_ENDPOINT="<YOUR_PROJECT_ENDPOINT>"
export MEMORY_STORE_CHAT_MODEL_DEPLOYMENT_NAME="<YOUR_CHAT_MODEL_DEPLOYMENT>"
export MEMORY_STORE_EMBEDDING_MODEL_DEPLOYMENT_NAME="<YOUR_EMBEDDING_DEPLOYMENT>"
Before using this class, create the memory store explicitly in your Azure AI Foundry project. The store is not created automatically by this integration.
import os
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import (
MemoryStoreDefaultDefinition,
MemoryStoreDefaultOptions,
)
from azure.core.exceptions import ResourceNotFoundError
from azure.identity import DefaultAzureCredential
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential(),
)
store_name = "my_store"
try:
client.beta.memory_stores.get(store_name)
except ResourceNotFoundError:
client.beta.memory_stores.create(
name=store_name,
description="Long-term memory store",
definition=MemoryStoreDefaultDefinition(
chat_model=os.environ[
"MEMORY_STORE_CHAT_MODEL_DEPLOYMENT_NAME"
],
embedding_model=os.environ[
"MEMORY_STORE_EMBEDDING_MODEL_DEPLOYMENT_NAME"
],
options=MemoryStoreDefaultOptions(
user_profile_enabled=True,
chat_summary_enabled=True,
),
),
)
Examples
With explicit endpoint and credential:
from azure.identity import DefaultAzureCredential
from langchain_core.chat_history import InMemoryChatMessageHistory
history = AzureAIMemoryChatMessageHistory(
project_endpoint="https://myproject.api.azureml.ms",
credential=DefaultAzureCredential(),
store_name="my_store",
scope="user:123",
base_history=InMemoryChatMessageHistory(),
)
With endpoint from environment variable:
history = AzureAIMemoryChatMessageHistory(
store_name="my_store",
scope="user:123",
base_history=InMemoryChatMessageHistory(),
)