Chat message history stores a history of the message interactions in a chat.
Class hierarchy:
BaseChatMessageHistory --> <name>ChatMessageHistory # Examples: CosmosDBChatMessageHistory
Main helpers:
AIMessage, HumanMessage, BaseMessage
Azure AI Foundry Memory integration with LangChain.
Azure CosmosDB Memory History ā DEPRECATED.
This module has moved to langchain_azure_cosmosdb.
Install and import directly from there instead::
pip install langchain-azure-cosmosdb
from langchain_azure_cosmosdb import CosmosDBChatMessageHistory
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(),
)