AzureAIMemoryChatMessageHistory(
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(),
)| Name | Type | Description |
|---|---|---|
store_name* | str | |
scope* | str | |
base_history* | BaseChatMessageHistory | |
project_endpoint | Optional[str |
Default: None |
credential | Optional[TokenCredential] | Default: None |
update_delay | Optional[int] | Default: None |
role_mapper | Optional[Callable[[BaseMessage], EasyInputMessageParam]] | Default: None |
| update_delay | Optional[int] |
| role_mapper | Optional[Callable[[BaseMessage], EasyInputMessageParam]] |
Memory store name in Azure AI Foundry.
Memory scope (for example, user:{user_id} or
tenant:{org_id}) used for long-term recall across sessions.
Underlying short-term history instance to wrap.
Return the underlying thread messages (short-term transcript).
Memory store name.
Memory scope (e.g., user ID or tenant ID).
Persist in short-term transcript AND asynchronously update Foundry Memory.
This method adds the message to the base history and then fires off an asynchronous update to Foundry Memory without blocking the chat flow.
Convenience: add multiple messages (each forwarded to Foundry).
Clear the short-term transcript for this session (no Foundry deletion).
Create a retriever bound to this store/scope/history.
History-bound retrievers always use incremental search with multi-turn conversation context for better contextual memory retrieval.
Azure AI project endpoint. If not provided,
reads from AZURE_AI_PROJECT_ENDPOINT.
Azure credential for authentication. If not provided,
uses DefaultAzureCredential().
Optional delay before memory extraction. Use None
for service default (about 300s) or 0 for immediate updates.
Optional custom function to map LangChain messages to Foundry message items.