# AzureAIMemoryChatMessageHistory

> **Class** in `langchain_azure_ai`

📖 [View in docs](https://reference.langchain.com/python/langchain-azure-ai/chat_history/azure_ai_memory/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:

```bash
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.

```python
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:

```python
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:

```python
history = AzureAIMemoryChatMessageHistory(
    store_name="my_store",
    scope="user:123",
    base_history=InMemoryChatMessageHistory(),
)
```

## Signature

```python
AzureAIMemoryChatMessageHistory(
    self,
    store_name: str,
    scope: str,
    base_history: BaseChatMessageHistory,
    *,
    project_endpoint: Optional[str] = None,
    credential: Optional[TokenCredential] = None,
    update_delay: Optional[int] = None,
    role_mapper: Optional[Callable[[BaseMessage], EasyInputMessageParam]] = None,
)
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `store_name` | `str` | Yes | Memory store name in Azure AI Foundry. |
| `scope` | `str` | Yes | Memory scope (for example, `user:{user_id}` or `tenant:{org_id}`) used for long-term recall across sessions. |
| `base_history` | `BaseChatMessageHistory` | Yes | Underlying short-term history instance to wrap. |
| `project_endpoint` | `Optional[str]` | No | Azure AI project endpoint. If not provided, reads from `AZURE_AI_PROJECT_ENDPOINT`. (default: `None`) |
| `credential` | `Optional[TokenCredential]` | No | Azure credential for authentication. If not provided, uses `DefaultAzureCredential()`. (default: `None`) |
| `update_delay` | `Optional[int]` | No | Optional delay before memory extraction. Use `None` for service default (about 300s) or `0` for immediate updates. (default: `None`) |
| `role_mapper` | `Optional[Callable[[BaseMessage], EasyInputMessageParam]]` | No | Optional custom function to map LangChain messages to Foundry message items. (default: `None`) |

## Extends

- `BaseChatMessageHistory`

## Constructors

```python
__init__(
    self,
    store_name: str,
    scope: str,
    base_history: BaseChatMessageHistory,
    *,
    project_endpoint: Optional[str] = None,
    credential: Optional[TokenCredential] = None,
    update_delay: Optional[int] = None,
    role_mapper: Optional[Callable[[BaseMessage], EasyInputMessageParam]] = None,
)
```

| Name | Type |
|------|------|
| `store_name` | `str` |
| `scope` | `str` |
| `base_history` | `BaseChatMessageHistory` |
| `project_endpoint` | `Optional[str]` |
| `credential` | `Optional[TokenCredential]` |
| `update_delay` | `Optional[int]` |
| `role_mapper` | `Optional[Callable[[BaseMessage], EasyInputMessageParam]]` |


## Properties

- `messages`
- `store_name`
- `scope`

## Methods

- [`add_message()`](https://reference.langchain.com/python/langchain-azure-ai/chat_history/azure_ai_memory/AzureAIMemoryChatMessageHistory/add_message)
- [`add_messages()`](https://reference.langchain.com/python/langchain-azure-ai/chat_history/azure_ai_memory/AzureAIMemoryChatMessageHistory/add_messages)
- [`clear()`](https://reference.langchain.com/python/langchain-azure-ai/chat_history/azure_ai_memory/AzureAIMemoryChatMessageHistory/clear)
- [`get_retriever()`](https://reference.langchain.com/python/langchain-azure-ai/chat_history/azure_ai_memory/AzureAIMemoryChatMessageHistory/get_retriever)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-azure/blob/7a6a664d617264df8dcb12fcfb1bacc09f21d504/libs/azure-ai/langchain_azure_ai/chat_history/azure_ai_memory.py#L78)