# RedisChatMessageHistory

> **Class** in `langchain_redis`

📖 [View in docs](https://reference.langchain.com/python/langchain-redis/chat_message_history/RedisChatMessageHistory)

Redis-based implementation of chat message history using RedisVL.

This class provides a way to store and retrieve chat message history using Redis
with RedisVL for efficient indexing, querying, and document management.

## Signature

```python
RedisChatMessageHistory(
    self,
    session_id: str,
    redis_url: str = 'redis://localhost:6379',
    key_prefix: str = 'chat:',
    ttl: Optional[int] = None,
    index_name: str = 'idx:chat_history',
    redis_client: Optional[Redis] = None,
    overwrite_index: bool = False,
    **kwargs: Any = {},
)
```

## Description

**Example:**

.. code-block:: python

from langchain_redis import RedisChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage

history = RedisChatMessageHistory(
    session_id="user123",
    redis_url="redis://localhost:6379",
    ttl=3600  # Expire chat history after 1 hour
)

# Add messages to the history
history.add_message(HumanMessage(content="Hello, AI!"))
history.add_message(
  AIMessage(content="Hello, human! How can I assist you today?")
)

# Retrieve all messages
messages = history.messages
for message in messages:
    print(f"{message.type}: {message.content}")

# Clear the history for the session
history.clear()

**Note:**

- This class uses RedisVL for managing Redis JSON storage and search indexes,
  providing efficient querying and retrieval.
- A Redis search index is created to enable fast lookups and search
  capabilities over the chat history.
- If TTL is set, message entries will automatically expire after the
  specified duration.
- The session_id is used to group messages belonging to the same conversation
  or user session.
- RedisVL automatically handles tokenization and escaping for search queries.

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `session_id` | `str` | Yes | A unique identifier for the chat session. |
| `redis_url` | `str` | No | URL of the Redis instance. Defaults to "redis://localhost:6379". (default: `'redis://localhost:6379'`) |
| `key_prefix` | `str` | No | Prefix for Redis keys. Defaults to "chat:". (default: `'chat:'`) |
| `ttl` | `Optional[int]` | No | Time-to-live for entries in seconds. Defaults to None (no expiration). (default: `None`) |
| `index_name` | `str` | No | Name of the Redis search index. Defaults to "idx:chat_history". (default: `'idx:chat_history'`) |
| `redis_client` | `Optional[Redis]` | No | Existing Redis client instance. If provided, redis_url is ignored. (default: `None`) |
| `overwrite_index` | `bool` | No | Whether to overwrite an existing index if it already exists. Defaults to False. If False and an index exists with a different key_prefix, a warning will be logged. (default: `False`) |
| `**kwargs` | `Any` | No | Additional keyword arguments to pass to the Redis client. (default: `{}`) |

## Extends

- `BaseChatMessageHistory`

## Constructors

```python
__init__(
    self,
    session_id: str,
    redis_url: str = 'redis://localhost:6379',
    key_prefix: str = 'chat:',
    ttl: Optional[int] = None,
    index_name: str = 'idx:chat_history',
    redis_client: Optional[Redis] = None,
    overwrite_index: bool = False,
    **kwargs: Any = {},
) -> None
```

| Name | Type |
|------|------|
| `session_id` | `str` |
| `redis_url` | `str` |
| `key_prefix` | `str` |
| `ttl` | `Optional[int]` |
| `index_name` | `str` |
| `redis_client` | `Optional[Redis]` |
| `overwrite_index` | `bool` |


## Properties

- `redis_client`
- `session_id`
- `key_prefix`
- `ttl`
- `index_name`
- `overwrite_index`
- `id`
- `messages`

## Methods

- [`add_message()`](https://reference.langchain.com/python/langchain-redis/chat_message_history/RedisChatMessageHistory/add_message)
- [`clear()`](https://reference.langchain.com/python/langchain-redis/chat_message_history/RedisChatMessageHistory/clear)
- [`delete()`](https://reference.langchain.com/python/langchain-redis/chat_message_history/RedisChatMessageHistory/delete)
- [`search_messages()`](https://reference.langchain.com/python/langchain-redis/chat_message_history/RedisChatMessageHistory/search_messages)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-redis/blob/ae0db7690f041437d662d0810523cda4cb7f5174/libs/redis/langchain_redis/chat_message_history.py#L43)