Bug: TypeError: argument 1 must be a string or unicode object: got Composed instead #42

Closed
opened 2026-02-16 05:16:15 -05:00 by yindo · 1 comment
Owner

Originally created by @pranavb-senslyze on GitHub (Aug 30, 2024).

Given example code in the docs:

import uuid

from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
from langchain_postgres import PostgresChatMessageHistory
import psycopg2

# Establish a synchronous connection to the database
# (or use psycopg.AsyncConnection for async)
sync_connection = psycopg2.connect("dbname=postgres user=postgres password=password host=localhost port=5432")

# Create the table schema (only needs to be done once)
table_name = "chat_history"
PostgresChatMessageHistory.create_tables(sync_connection, str(table_name))

session_id = str(uuid.uuid4())

# Initialize the chat history manager
chat_history = PostgresChatMessageHistory(
    table_name,
    session_id,
    sync_connection=sync_connection
)

# Add messages to the chat history
chat_history.add_messages([
    SystemMessage(content="Meow"),
    AIMessage(content="woof"),
    HumanMessage(content="bark"),
])

print(chat_history.messages)

Error Stacktrace:

{
	"name": "TypeError",
	"message": "argument 1 must be a string or unicode object: got Composed instead",
	"stack": "---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[30], line 13
     11 # Create the table schema (only needs to be done once)
     12 table_name = \"chat_history\"
---> 13 PostgresChatMessageHistory.create_tables(sync_connection, str(table_name))
     15 session_id = str(uuid.uuid4())
     17 # Initialize the chat history manager

File ~/Projects/chatbot/venv/lib/python3.12/site-packages/langchain_postgres/chat_message_histories.py:218, in PostgresChatMessageHistory.create_tables(connection, table_name)
    216 with connection.cursor() as cursor:
    217     for query in queries:
--> 218         cursor.execute(query)
    219 connection.commit()

TypeError: argument 1 must be a string or unicode object: got Composed instead"
}
Originally created by @pranavb-senslyze on GitHub (Aug 30, 2024). Given example code in the docs: ``` import uuid from langchain_core.messages import SystemMessage, AIMessage, HumanMessage from langchain_postgres import PostgresChatMessageHistory import psycopg2 # Establish a synchronous connection to the database # (or use psycopg.AsyncConnection for async) sync_connection = psycopg2.connect("dbname=postgres user=postgres password=password host=localhost port=5432") # Create the table schema (only needs to be done once) table_name = "chat_history" PostgresChatMessageHistory.create_tables(sync_connection, str(table_name)) session_id = str(uuid.uuid4()) # Initialize the chat history manager chat_history = PostgresChatMessageHistory( table_name, session_id, sync_connection=sync_connection ) # Add messages to the chat history chat_history.add_messages([ SystemMessage(content="Meow"), AIMessage(content="woof"), HumanMessage(content="bark"), ]) print(chat_history.messages) ``` Error Stacktrace: ``` { "name": "TypeError", "message": "argument 1 must be a string or unicode object: got Composed instead", "stack": "--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[30], line 13 11 # Create the table schema (only needs to be done once) 12 table_name = \"chat_history\" ---> 13 PostgresChatMessageHistory.create_tables(sync_connection, str(table_name)) 15 session_id = str(uuid.uuid4()) 17 # Initialize the chat history manager File ~/Projects/chatbot/venv/lib/python3.12/site-packages/langchain_postgres/chat_message_histories.py:218, in PostgresChatMessageHistory.create_tables(connection, table_name) 216 with connection.cursor() as cursor: 217 for query in queries: --> 218 cursor.execute(query) 219 connection.commit() TypeError: argument 1 must be a string or unicode object: got Composed instead" } ```
yindo closed this issue 2026-02-16 05:16:15 -05:00
Author
Owner

@eyurtsev commented on GitHub (Sep 12, 2024):

this library is not compatible with psycopg2 use psycopg 3 please (pip install --upgrade psycopg) and update your code to use psycopg

@eyurtsev commented on GitHub (Sep 12, 2024): this library is not compatible with psycopg2 use psycopg 3 please (pip install --upgrade psycopg) and update your code to use psycopg
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langchain-postgres#42