cancel
Showing results for 
Search instead for 
Did you mean: 
Generative AI
Explore discussions on generative artificial intelligence techniques and applications within the Databricks Community. Share ideas, challenges, and breakthroughs in this cutting-edge field.
cancel
Showing results for 
Search instead for 
Did you mean: 

Databricks SQL Agent is failing

SandipCoder
New Contributor

Goal :

I want to build one Databricks SQL Agent using that any Databricks users can interact with a specified schema in Unity Catalog and generate insights on their data.

Followed the url as reference :https://docs.databricks.com/en/large-language-models/langchain.html

SampleCode:

from langchain.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain import OpenAI

db = SQLDatabase.from_databricks(catalog="genai", schema="vector_db")
#llm = OpenAI(temperature=.7)
toolkit = SQLDatabaseToolkit(db=db, llm=llm_handler)
agent = create_sql_agent(llm=llm_handler, toolkit=toolkit, verbose=True)

Error :

AttributeError: module 'sqlalchemy.types' has no attribute 'Uuid'
File <command-4275727126297453>, line 6
3 from langchain.sql_database import SQLDatabase
4 from langchain import OpenAI
----> 6 db = SQLDatabase.from_databricks(catalog="genai", schema="vector_db")
7 #llm = OpenAI(temperature=.7)
8 toolkit = SQLDatabaseToolkit(db=db, llm=llm_handler)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-21fa55bd-1c7c-42e6-8617-6775fdb15bae/lib/python3.11/site-packages/databricks/sqlalchemy/_types.py:35
13 """This method is supposed to accept a Python type and return a string representation of that type.
14 But due to some weirdness in the way SQLAlchemy's literal rendering works, we have to return
15 the value itself because, by the time it reaches our custom type code, it's already been converted

 

Sandip Bhowmick
2 REPLIES 2

Walter_C
Honored Contributor

The error message you're seeing, AttributeError: module 'sqlalchemy.types' has no attribute 'Uuid', suggests that the SQLAlchemy library you're using doesn't have the 'Uuid' attribute.

This could be due to a few reasons:

  1. Version Mismatch: You might be using an older version of SQLAlchemy that doesn't support the 'Uuid' attribute. You can check your SQLAlchemy version by running print(sqlalchemy.__version__). If it's not the latest version, you might want to upgrade it using pip: pip install --upgrade sqlalchemy.

  2. Incorrect Import: Make sure you're importing the correct module. The 'Uuid' attribute is part of the sqlalchemy.dialects.postgresql module, not the sqlalchemy.types module. So, you should import it like this: from sqlalchemy.dialects.postgresql import UUID.

  3. Dependency Issue: There might be a dependency issue with the LangChain library. If the above solutions don't work, you might want to reach out to the LangChain support team for assistance.

amaan784
New Contributor II

Following up on @Walter_C insights, I encountered the same issue and wanted to share the complete steps I took to fully resolve the issue:

1) Upgrade SQLAlchemy: Running the following command before any other code resolved the sqlalchemy.types error:

 

%pip install --upgrade sqlalchemy

2) Address LangChain Deprecation Warning: I received a deprecation warning for the OpenAI class in LangChain:


"LangChainDeprecationWarning
: The class `OpenAI` was deprecated in LangChain 0.0.10 and will be removed in 0.3.0. An updated version of the class exists in the langchain-openai package."

To resolve this:

  • I replaced: 

 

from langchain import OpenAI

 

with: 

 

from langchain_openai import OpenAI

 

  • Before this definitely install the necessary packages again, this time including langchain-openai:

 

%pip install --upgrade langchain databricks-sql-connector sqlalchemy langchain-openai

 

3) Update Method Call: I switched from using chain.run to chain.invoke as the run method was deprecated:

 

agent.invoke("What is the longest trip distance and how long did it take?")

 

I’ve uploaded the complete updated code to my GitHub. You can check it out here: Databricks Text to SQL using Langchain.ipynb 

After making these updates, everything worked smoothly. Hope this helps!

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group