mirror of
https://github.com/langchain-ai/langchain-postgres.git
synced 2026-07-16 01:33:18 -04:00
How to use PostgresChatMessageHistory with async connections when using RunnableWithMessageHistory?
#48
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @lgabs on GitHub (Sep 29, 2024).
I'm attempting to implement an asynchronous approach using PostgresChatMessageHistory in combination with RunnableWithMessageHistory, but I'm encountering some challenges. While PostgresChatMessageHistory appears to support asynchronous connections with async methods, I haven't found clear guidance on how to properly use them. Additionally, I couldn't locate any relevant documentation addressing this use case.
Any ideas on how to resolve this problem?
Here the a sample code I'm using and the traceback error:
Traceback error:
The error indicates that 'coroutine' object has no attribute 'aget_messages', which suggests the asynchronous method handling might not be correctly invoked.
@pprados commented on GitHub (Sep 30, 2024):
Read the TU here
@lgabs commented on GitHub (Sep 30, 2024):
Thanks, @pprados
However, it's still unclear to me how to properly use RunnableWithMessageHistory. Even after reviewing the examples in the unit tests, I encountered the exact same error mentioned earlier (see the code below). Based on the error message, I believe the issue is that RunnableWithMessageHistory expects a function that returns an instance of BaseChatMessageHistory for a given session_id. The last few lines of the error indicate that the instance is being created with:
However, since hist is not awaited, its value remains a coroutine, which doesn't have the attribute aget_messages, as the error suggests.
Here's a new approach, yet failing, based on the unit tests:
Let me know if there's something I'm still missing or if any adjustments are needed. Thanks!
@pprados commented on GitHub (Oct 1, 2024):
thank you for this analysis. Can you suggest a PR?
@jackadair commented on GitHub (Oct 3, 2024):
@lgabs Any luck solving this issue?
@lgabs commented on GitHub (Oct 3, 2024):
I found a workaround to for that problem, but before preparing a PR, I'll share the idea for discussion. I've made some changes to the async methods of PostgresChatMessageHistory to open an async connection when the class instance does not have the async conn created in its instantiation, using a new attribute conn_str. While this works, it opens a new connection every time, making it bad for connection reusage. What do you think?
Here it is the full code: