Open Zolastic opened 3 days ago
After further investigation, I've discovered that the issue arises when one of my tools makes a database call. Specifically, when the assistant invokes a tool that interacts with the database using SQLAlchemy, I intermittently encounter the following error:
psycopg.errors.InvalidSqlStatementName: prepared statement "_pg3_4" does not exist
Relevant Code:
Here are the key snippets of my tools:
# tools.py
from langchain_core.tools import tool
from dxs.gerica.repository.sql import ProductSQLRepository
from dxs.gerica.utils.sql_utils import get_database_session
from .utils import Citation, Product as ProductDetails
@tool(
"fetch_products",
response_format="content_and_artifact"
)
def fetch_products():
try:
db_session = get_database_session()
product_repository = ProductSQLRepository(session=db_session)
products = product_repository.fetch_all()
if not products:
return "No products found."
context_str = "<products>\n"
for product in products:
context_str += f"""
<product>
<id>{product.id}</id>
<name>{product.name}</name>
<summary>{product.summary or 'N/A'}</summary>
<description>{product.description or 'N/A'}</description>
</product>
"""
context_str += "</products>"
citations = [
Citation(
id="",
type="URL",
title="Example Products Page",
url="https://www.example.com/products",
checksum="",
)
]
return context_str, {"citations": citations}
finally:
db_session.close()
@tool(
"fetch_product_details",
response_format="content_and_artifact"
)
def fetch_product_details(product_ids):
try:
db_session = get_database_session()
product_repository = ProductSQLRepository(session=db_session)
products = [product_repository.find_one(pid) for pid in product_ids]
if not products:
return "No products found for the given IDs."
product_details = []
context_str = "<products>\n"
for product in products:
context_str += f"""
<product>
<id>{product.id}</id>
<name>{product.name}</name>
<summary>{product.summary}</summary>
<description>{product.description}</description>
</product>
"""
product_details.append(ProductDetails(id=product.id, name=product.name))
context_str += "</products>"
citations = [
Citation(id=product.id, type="URL", title=product.name, url=product.product_page_url or "")
for product in products
]
return context_str, {"product_details": product_details, "citations": citations}
finally:
db_session.close()
# sql_utils.py
import os
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from dotenv import load_dotenv
load_dotenv()
def get_database_session():
db_url = os.getenv("DATABASE_URL")
db = sa.create_engine(db_url)
Session = sessionmaker(bind=db)
return Session()
Base = declarative_base()
Observation:
SQLAlchemy
with a synchronous engine).AsyncPostgresSaver
.Request for Assistance:
Would refactoring my tools to use asynchronous database sessions (e.g., SQLAlchemy AsyncIO
) resolve this issue? Are there other steps I should take to prevent this conflict between synchronous and asynchronous database interactions?
Any guidance or recommendations would be greatly appreciated! Thank you.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
I'm trying to use LangGraph with Supabase PostgreSQL as my database. I have an assistant implemented in
product_assistant.py
that usesAsyncPostgresSaver
for asynchronous database operations. My assistant works fine most of the time, but sometimes it throws the following error:This error seems to occur intermittently, and I haven't been able to identify a consistent pattern.
Additional Information:
AsyncPostgresSaver
in LangGraph) and synchronous database operations (via SQLAlchemy in my tools) might be causing conflicts or unexpected behavior.DEALLOCATE ALL
statements, but the error still occurs.Occasionally, I also encounter another error:
This error also happens intermittently, similar to the
InvalidSqlStatementName
error.Question:
How can I resolve this intermittent
InvalidSqlStatementName
error? Is there a recommended way to handle database operations in tools when using LangGraph, especially when both the assistant and the tools make database calls? Should I refactor my tools to use asynchronous database sessions? Any guidance on how to solve this issue would be greatly appreciated.System Info