diff --git a/libs/core/langchain_core/chat_history.py b/libs/core/langchain_core/chat_history.py index a4a76f534a..76dba298aa 100644 --- a/libs/core/langchain_core/chat_history.py +++ b/libs/core/langchain_core/chat_history.py @@ -3,10 +3,11 @@ from __future__ import annotations from abc import ABC, abstractmethod -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any from pydantic import BaseModel, Field +from langchain_core._api import deprecated from langchain_core.messages import ( AIMessage, BaseMessage, @@ -19,6 +20,14 @@ if TYPE_CHECKING: from collections.abc import Sequence +@deprecated( + since="1.6.4", + removal="2.0.0", + addendum=( + "See the short-term memory documentation for recommended alternatives: " + "https://docs.langchain.com/oss/python/langchain/short-term-memory" + ), +) class BaseChatMessageHistory(ABC): """Abstract base class for storing chat message history. @@ -96,6 +105,10 @@ class BaseChatMessageHistory(ABC): layer, so this operation is expected to incur some latency. """ + def __init__(self, *args: Any, **kwargs: Any) -> None: + """Initialize cooperatively to preserve multiple inheritance.""" + super().__init__(*args, **kwargs) + async def aget_messages(self) -> list[BaseMessage]: """Async version of getting messages. @@ -199,6 +212,14 @@ class BaseChatMessageHistory(ABC): return get_buffer_string(self.messages) +@deprecated( + since="1.6.4", + removal="2.0.0", + addendum=( + "See the short-term memory documentation for recommended alternatives: " + "https://docs.langchain.com/oss/python/langchain/short-term-memory" + ), +) class InMemoryChatMessageHistory(BaseChatMessageHistory, BaseModel): """In memory implementation of chat message history. diff --git a/libs/core/tests/unit_tests/chat_history/test_chat_history.py b/libs/core/tests/unit_tests/chat_history/test_chat_history.py index 7557d6988f..9fda88dc57 100644 --- a/libs/core/tests/unit_tests/chat_history/test_chat_history.py +++ b/libs/core/tests/unit_tests/chat_history/test_chat_history.py @@ -1,9 +1,36 @@ from collections.abc import Sequence -from langchain_core.chat_history import BaseChatMessageHistory +import pytest + +from langchain_core.chat_history import ( + BaseChatMessageHistory, + InMemoryChatMessageHistory, +) from langchain_core.messages import BaseMessage, HumanMessage +@pytest.mark.parametrize( + "history_class", [BaseChatMessageHistory, InMemoryChatMessageHistory] +) +def test_chat_history_deprecated(history_class: type[BaseChatMessageHistory]) -> None: + assert "deprecated" in (history_class.__doc__ or "") + assert "https://docs.langchain.com/oss/python/langchain/short-term-memory" in ( + history_class.__doc__ or "" + ) + + +async def test_in_memory_history_initialization() -> None: + messages = [HumanMessage(content="Hello")] + history = InMemoryChatMessageHistory(messages=messages) + assert await history.aget_messages() == messages + other_history = InMemoryChatMessageHistory() + await other_history.aadd_messages([HumanMessage(content="World")]) + assert history.messages == messages + await history.aclear() + assert history.messages == [] + assert other_history.messages == [HumanMessage(content="World")] + + def test_add_message_implementation_only() -> None: """Test implementation of add_message only."""