Skip to content

Commit 0f69092

Browse files
fix: add write messages to Chat History (#265)
* fix: add write definition for messages property in Chat History * chore: add test for setting messages property in Chat History * chore: lint fix
1 parent 381c5e8 commit 0f69092

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/langchain_google_cloud_sql_pg/chat_message_history.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,17 @@ def create_sync(
106106
history = engine._run_as_sync(coro)
107107
return cls(cls.__create_key, engine, history)
108108

109-
@property # type: ignore[override]
109+
@property
110110
def messages(self) -> list[BaseMessage]:
111-
"""The abstraction required a property."""
111+
"""Fetches all messages stored in AlloyDB."""
112112
return self._engine._run_as_sync(self._history._aget_messages())
113113

114+
@messages.setter
115+
def messages(self, value: list[BaseMessage]) -> None:
116+
"""Clear the stored messages and appends a list of messages to the record in AlloyDB."""
117+
self.clear()
118+
self.add_messages(value)
119+
114120
async def aadd_message(self, message: BaseMessage) -> None:
115121
"""Append the message to the record in PostgreSQL"""
116122
await self._engine._run_as_async(self._history.aadd_message(message))

tests/test_chatmessagehistory.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ async def test_chat_message_history_sync_messages(
163163
assert len(history2.messages) == 0
164164

165165

166+
@pytest.mark.asyncio
167+
async def test_chat_message_history_set_messages(
168+
async_engine: PostgresEngine,
169+
) -> None:
170+
history = await PostgresChatMessageHistory.create(
171+
engine=async_engine, session_id="test", table_name=table_name_async
172+
)
173+
msg1 = HumanMessage(content="hi!")
174+
msg2 = AIMessage(content="bye -_-")
175+
# verify setting messages property adds to message history
176+
history.messages = [msg1, msg2]
177+
assert len(history.messages) == 2
178+
179+
166180
@pytest.mark.asyncio
167181
async def test_chat_table_async(async_engine):
168182
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)