1

I use a code to create a SQLDatabaseChain with LangChain that once worked just fine. However, it does not work anymore, which is very strange. Here is the part that's causing the error:

llm = ...
memory = ...
db = SQLDatabase.from_uri("sqlite:///Test.db")
print(type(db)) --> <class 'langchain.utilities.sql_database.SQLDatabase'>
print(db) --> <langchain.utilities.sql_database.SQLDatabase object at 0x0000015965D4F7D0>

sql_chain = SQLDatabaseChain.from_llm(
        llm=llm,
        db=db,
        verbose=True,
        memory=memory,
        use_query_checker=True,
        top_k=20,)
...
result = sql_chain({"query": question}, return_only_outputs=False)
...

This is the error I get:

ValidationError                           Traceback (most recent call last)
File c:\Users\...\SQLChain-Test_01.py:1
----> 1 sql_chain = SQLDatabaseChain.from_llm(
      2     llm=llm,
      3     db=db,
      4     verbose=True,
      5     memory=memory,
      6     use_query_checker=True,
      7     top_k=20,)

File c:\Users\...\Lib\site-packages\langchain_experimental\sql\base.py:228, in SQLDatabaseChain.from_llm(cls, llm, db, prompt, **kwargs)
    226 prompt = prompt or SQL_PROMPTS.get(db.dialect, PROMPT)
    227 llm_chain = LLMChain(llm=llm, prompt=prompt)
--> 228 return cls(llm_chain=llm_chain, database=db, **kwargs)

File c:\Users\...\AppData\Local\anaconda3\envs\...\Lib\site-packages\langchain\load\serializable.py:97, in Serializable.__init__(self, **kwargs)
     96 def __init__(self, **kwargs: Any) -> None:
---> 97     super().__init__(**kwargs)
     98     self._lc_kwargs = kwargs

File c:\Users\...\AppData\Local\anaconda3\envs\...\Lib\site-packages\pydantic\v1\main.py:341, in BaseModel.__init__(__pydantic_self__, **data)
    339 values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
    340 if validation_error:
--> 341     raise validation_error
    342 try:
    343     object_setattr(__pydantic_self__, '__dict__', values)

ValidationError: 1 validation error for SQLDatabaseChain
database
  instance of SQLDatabase expected (type=type_error.arbitrary_type; expected_arbitrary_type=SQLDatabase)

Strangely, when I check the type of my db it seems to be all correct: print(type(db)) --> <class 'langchain.utilities.sql_database.SQLDatabase'> Any ideas what is happening here? Maybe I am overseeing something.

Langchain version = 0.1.6
Langchain experimental version = 0.0.51
Python version = 3.11.5

Many thanks for any help.

1
  • What are your imports? I think that must be for those package versions: from langchain.sql_database import SQLDatabase from langchain_experimental.sql import SQLDatabaseChain Commented Feb 28, 2024 at 11:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.