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

Open
opened 2026-02-16 05:16:05 -05:00 by yindo · 4 comments
Owner

Originally created by @gingters on GitHub (May 22, 2024).

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
Originally created by @gingters on GitHub (May 22, 2024). 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
yindo added the help wanted label 2026-02-16 05:16:05 -05:00
Author
Owner

@christianramsey commented on GitHub (May 22, 2024):

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

@christianramsey commented on GitHub (May 22, 2024): 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' +------------------------------------
Author
Owner

@35sui-com commented on GitHub (May 26, 2024):

same error, looking forward to fix

@35sui-com commented on GitHub (May 26, 2024): same error, looking forward to fix
Author
Owner

@christianramsey commented on GitHub (Jun 3, 2024):

Double clicking this again

@christianramsey commented on GitHub (Jun 3, 2024): Double clicking this again
Author
Owner

@eyurtsev commented on GitHub (Jun 10, 2024):

The community should feel free to open a PR to fix

@eyurtsev commented on GitHub (Jun 10, 2024): The community should feel free to open a PR to fix
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langchain-postgres#18