Skip to content

The agent engineering platform.

fix(core): add back add_user_message and add_ai_message (#33340)

ccurmecbf8d46d3e3c
diff --git a/libs/core/langchain_core/chat_history.py b/libs/core/langchain_core/chat_history.py
index b440db352e..29d05d9969 100644
--- a/libs/core/langchain_core/chat_history.py
+++ b/libs/core/langchain_core/chat_history.py
@@ -22,7 +22,9 @@ from typing import TYPE_CHECKING
from pydantic import BaseModel, Field
from langchain_core.messages import (
+ AIMessage,
BaseMessage,
+ HumanMessage,
get_buffer_string,
)
from langchain_core.runnables.config import run_in_executor
@@ -124,6 +126,40 @@ class BaseChatMessageHistory(ABC):
"""
return await run_in_executor(None, lambda: self.messages)
+ def add_user_message(self, message: HumanMessage | str) -> None:
+ """Convenience method for adding a human message string to the store.
+
+ !!! note
+ This is a convenience method. Code should favor the bulk ``add_messages``
+ interface instead to save on round-trips to the persistence layer.
+
+ This method may be deprecated in a future release.
+
+ Args:
+ message: The human message to add to the store.
+ """
+ if isinstance(message, HumanMessage):
+ self.add_message(message)
+ else:
+ self.add_message(HumanMessage(content=message))
+
+ def add_ai_message(self, message: AIMessage | str) -> None:
+ """Convenience method for adding an AI message string to the store.
+
+ !!! note
+ This is a convenience method. Code should favor the bulk ``add_messages``
+ interface instead to save on round-trips to the persistence layer.
+
+ This method may be deprecated in a future release.
+
+ Args:
+ message: The AI message to add.
+ """
+ if isinstance(message, AIMessage):
+ self.add_message(message)
+ else:
+ self.add_message(AIMessage(content=message))
+
def add_message(self, message: BaseMessage) -> None:
"""Add a Message object to the store.