Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PydanticUserError: SQLDatabaseToolkit is not fully defined; you should define Callbacks, then call SQLDatabaseToolkit.model_rebuild(). #29222

Open
5 tasks done
arno756 opened this issue Jan 15, 2025 · 1 comment
Labels
Ɑ: core Related to langchain-core investigate Flagged for investigation.

Comments

@arno756
Copy link

arno756 commented Jan 15, 2025

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

pip install "autogen-ext[azure]" python-dotenv langchain-community psycopg2 langchain_openai langchain_experimental "autogen-core" "autogen-agentchat"

from dotenv import load_dotenv
import os
from langchain_community.utilities.sql_database import SQLDatabase
import psycopg2
from langchain_openai import AzureChatOpenAI
from langchain_experimental.sql import SQLDatabaseChain
from langchain_experimental.sql.base import SQLDatabaseChain
from langchain.cache import InMemoryCache

# Load environment variables from the .env file from the same directory as notebook 
load_dotenv()

# Retrieve environment variables
POSTGRES_USER = os.getenv('POSTGRES_USER')
POSTGRES_PASSWORD = os.getenv('POSTGRES_PASSWORD')
POSTGRES_HOST = os.getenv('POSTGRES_HOST')
POSTGRES_PORT = os.getenv('POSTGRES_PORT')
POSTGRES_DB = os.getenv('POSTGRES_DB')
AZURE_OPENAI_KEY = os.getenv('AZURE_OPENAI_KEY')
AZURE_OPENAI_ENDPOINT = os.getenv('AZURE_OPENAI_ENDPOINT')
AZURE_OPENAI_DEPLOYMENT = os.getenv('AZURE_OPENAI_DEPLOYMENT')

llm_config = {
    "config_list": [
        {
        "model": AZURE_OPENAI_DEPLOYMENT,
        "temperature": 0.7,
        "api_key": AZURE_OPENAI_KEY,
        "azure_endpoint": AZURE_OPENAI_ENDPOINT,
        "api_type": "azure",
        "api_version": "2024-10-21"
        }]}

shipment_db_uri = f"postgresql+psycopg2://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DB}"

shipment_chain = SQLDatabaseChain(llm=azure_llm, database=shipment_db, verbose=True)

Error Message and Stack Trace (if applicable)


PydanticUserError Traceback (most recent call last)
Cell In[22], line 116
2 llm_config = {
3 "config_list": [
4 {
(...)
111 ]
112 }
115 # Initialize the database chains
--> 116 shipment_chain = SQLDatabaseChain(llm=azure_llm, database=shipment_db, verbose=True)
117 crm_chain = SQLDatabaseChain(llm=azure_llm, database=crm_db, verbose=True)
119 # Create assistant agents

File ~/Projects/multi-AI-test/azure-postgresql-openai-langchain-autogen-demo/new_env_0.4/lib/python3.12/site-packages/langchain_core/load/serializable.py:125, in Serializable.init(self, *args, **kwargs)
123 def init(self, *args: Any, **kwargs: Any) -> None:
124 """"""
--> 125 super().init(*args, **kwargs)

[... skipping hidden 1 frame]

File ~/Projects/multi-AI-test/azure-postgresql-openai-langchain-autogen-demo/new_env_0.4/lib/python3.12/site-packages/pydantic/_internal/_mock_val_ser.py:100, in MockValSer.getattr(self, item)
98 # raise an AttributeError if item doesn't exist
99 getattr(self._val_or_ser, item)
--> 100 raise PydanticUserError(self._error_message, code=self._code)

PydanticUserError: SQLDatabaseChain is not fully defined; you should define BaseCache, then call SQLDatabaseChain.model_rebuild().

Description

I saw this issue: https://github.com/langchain-ai/langchain/pull/28297 and that one (#28297) looked like it solved but it hasn't for me. I have to go back to 2.9.2 but now it's not compatible with autogen 0.4 and I have to get to the latest version.

System Info

System Information

OS: Darwin
OS Version: Darwin Kernel Version 24.2.0: Fri Dec 6 19:02:41 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6030
Python Version: 3.12.8 (main, Dec 3 2024, 18:42:41) [Clang 16.0.0 (clang-1600.0.26.4)]

Package Information

langchain_core: 0.3.29
langchain: 0.3.14
langchain_community: 0.3.14
langsmith: 0.2.10
langchain_experimental: 0.3.4
langchain_openai: 0.3.0
langchain_text_splitters: 0.3.5

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.11.11
async-timeout: Installed. No version info available.
dataclasses-json: 0.6.7
httpx: 0.28.1
httpx-sse: 0.4.0
jsonpatch: 1.33
langsmith-pyo3: Installed. No version info available.
numpy: 2.2.1
openai: 1.59.7
orjson: 3.10.14
packaging: 24.2
pydantic: 2.10.0
pydantic-settings: 2.7.1
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
SQLAlchemy: 2.0.37
tenacity: 9.0.0
tiktoken: 0.8.0
typing-extensions: 4.12.2
zstandard: Installed. No version info available.

@langcarl langcarl bot added the investigate Flagged for investigation. label Jan 15, 2025
@dosubot dosubot bot added the Ɑ: core Related to langchain-core label Jan 15, 2025
@keenborder786
Copy link
Contributor

You are using very old and experimental way of interacting with the database. You can do something as follow to construct a SQL Agent which talks with your data:

from langchain_community.agent_toolkits.sql.base import create_sql_agent
from langchain_openai import ChatOpenAI
from langchain_community.utilities.sql_database import SQLDatabase
from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit

agent = create_sql_agent(llm = ChatOpenAI(model = "gpt-4o-mini"),
                        agent_type = "openai-tools", 
                        verbose=True,
                        toolkit = SQLDatabaseToolkit(db = SQLDatabase.from_uri(database_uri = "postgresql+psycopg2://root:password@localhost:5432/dev_db"),llm = ChatOpenAI(model = "gpt-4o-mini")))

agent.invoke({"input": "How many calls happenend?"})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: core Related to langchain-core investigate Flagged for investigation.
Projects
None yet
Development

No branches or pull requests

2 participants