langchain-ai / langchain-postgres

LangChain abstractions backed by Postgres Backend
MIT License
66 stars 22 forks source link

ChatMessageHistory does throw error after upgrading, neither sync nor async connection works #55

Open gingters opened 1 month ago

gingters commented 1 month ago

I moved from langchain_community to langchain_postgres.

With the previous community implementation everything worked, but we need to update as we have other dependencies that need newer versions.

When I use the (sync) Connection, I get this error:

    |   File "/home/sebastian/miniconda3/envs/ragapi/lib/python3.12/site-packages/langchain_core/runnables/history.py", line 441, in _aenter_history
    |     messages = (await hist.aget_messages()).copy()
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/home/sebastian/miniconda3/envs/ragapi/lib/python3.12/site-packages/langchain_postgres/chat_message_histories.py", line 336, in aget_messages
    |     async with self._aconnection.cursor() as cursor:
    | TypeError: 'Cursor' object does not support the asynchronous context manager protocol

That prompted me to use the AsyncConnection instead, but when using the AsyncConnection, I run into this error when the message history is to be fetched:

    |   File "/home/sebastian/miniconda3/envs/ragapi/lib/python3.12/site-packages/langchain_core/runnables/history.py", line 441, in _aenter_history
    |     messages = (await hist.aget_messages()).copy()
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/home/sebastian/miniconda3/envs/ragapi/lib/python3.12/site-packages/langchain_postgres/chat_message_histories.py", line 336, in aget_messages
    |     async with self._aconnection.cursor() as cursor:
    |                ^^^^^^^^^^^^^^^^^^^^^^^^
    | AttributeError: 'coroutine' object has no attribute 'cursor'

I currently use:

christianramsey commented 1 month ago

I have a similar error.

I am using RunnableWithMessageHistory and if I run it with sync it says I need to make it async.

then I change it to an async connection and get the same AttributeError.

I even try to async await the connection -

async def get_connection(): return await psycopg.AsyncConnection(POSTGRES_AI)

def create_message_history(session_id: str) -> PostgresChatMessageHistory: return PostgresChatMessageHistory( table_name, session_id, async_connection=get_connection() )

File "/Users/fanli/Library/Caches/pypoetry/virtualenvs/ai-6oX_ziT5-py3.12/lib/python3.12/site-packages/langchain_postgres/chat_message_histories.py", line 336, in aget_messages | async with self._aconnection.cursor() as cursor: | ^^^^^^^^^^^^^^^^^^^^^^^^ | AttributeError: 'coroutine' object has no attribute 'cursor' +------------------------------------

35sui-com commented 1 month ago

same error, looking forward to fix

christianramsey commented 4 weeks ago

Double clicking this again

eyurtsev commented 3 weeks ago

The community should feel free to open a PR to fix