langchain-ai / langchain-postgres

LangChain abstractions backed by Postgres Backend

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

gingters opened this issue · comments

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:

  • langchain 0.2.0
  • langchain-postgres 0.0.6
  • psycopg 3.1.19

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'
+------------------------------------

same error, looking forward to fix

Double clicking this again

The community should feel free to open a PR to fix