kevin lau
kevin lau

Reputation: 31

AttributeError: module 'sqlalchemy' has no attribute 'Select' when using SQLDatabaseLoader in langchain_community

I am new to Langchain and I am stuck at an issue. My end goal is to read the contents of a file and create a vectorstore of my data which I can query later. I'm encountering an error while trying to use the SQLDatabaseLoader from the langchain_community package. Here's the code that I'm running:

import os
from langchain_community.document_loaders import TextLoader
loader = TextLoader("state_of_the_union.txt")
loader.load()

However, upon execution, I'm encountering the following error:

AttributeError: module 'sqlalchemy' has no attribute 'Select'

It seems to be pointing to an issue within the SQLDatabaseLoader class. Specifically, the error occurs in the following part of the code:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[17], line 2
      1 import os
----> 2 from langchain_community.document_loaders import TextLoader
      3 loader = TextLoader("state_of_the_union.txt")
      4 loader.load()

File ~\AppData\Roaming\Python\Python310\site-packages\langchain_community\document_loaders\__init__.py:190
    188 from langchain_community.document_loaders.snowflake_loader import SnowflakeLoader
    189 from langchain_community.document_loaders.spreedly import SpreedlyLoader
--> 190 from langchain_community.document_loaders.sql_database import SQLDatabaseLoader
    191 from langchain_community.document_loaders.srt import SRTLoader
    192 from langchain_community.document_loaders.stripe import StripeLoader

File ~\AppData\Roaming\Python\Python310\site-packages\langchain_community\document_loaders\sql_database.py:10
      6 from langchain_community.document_loaders.base import BaseLoader
      7 from langchain_community.utilities.sql_database import SQLDatabase
---> 10 class SQLDatabaseLoader(BaseLoader):
     11     """
     12     Load documents by querying database tables supported by SQLAlchemy.
     13 
   (...)
     17     Each document represents one row of the result.
     18     """
     20     def __init__(
     21         self,
     22         query: Union[str, sa.Select],
   (...)
     30         include_query_into_metadata: bool = False,
     31     ):

File ~\AppData\Roaming\Python\Python310\site-packages\langchain_community\document_loaders\sql_database.py:22, in SQLDatabaseLoader()
     10 class SQLDatabaseLoader(BaseLoader):
     11     """
     12     Load documents by querying database tables supported by SQLAlchemy.
     13 
   (...)
     17     Each document represents one row of the result.
     18     """
     20     def __init__(
     21         self,
---> 22         query: Union[str, sa.Select],
     23         db: SQLDatabase,
     24         *,
     25         parameters: Optional[Dict[str, Any]] = None,
     26         page_content_mapper: Optional[Callable[..., str]] = None,
     27         metadata_mapper: Optional[Callable[..., Dict[str, Any]]] = None,
     28         source_columns: Optional[Sequence[str]] = None,
     29         include_rownum_into_metadata: bool = False,
     30         include_query_into_metadata: bool = False,
     31     ):
     32         """
     33         Args:
     34             query: The query to execute.
   (...)
     49               expression into the metadata dictionary. Default: False.
     50         """
     51         self.query = query

It seems to be referencing an attribute 'Select' from the 'sqlalchemy' module, but it's unable to find it. I'm not sure why this error is occurring, especially since I haven't directly used 'sqlalchemy.Select' in my code.

Can anyone help me understand what might be causing this error and how I can resolve it? Any insights or suggestions would be greatly appreciated. Thank you!

System Information OS: Microsoft Windows 11 10.0.22631

Package Information

langchain                       0.1.10
langchain-community             0.0.25
langchain-core                  0.1.29
langchain-openai                0.0.8
langchain-text-splitters        0.0.1
langchainhub                    0.1.15
langsmith                       0.1.18
SQLAlchemy                      2.0.28

Upvotes: 3

Views: 1159

Answers (1)

didrikm
didrikm

Reputation: 23

I just reinstalled the package and specified ver 2.0.0. The confusing part is pip told me I had ver 2.0.1 installed, but when I ran pip install sqlalchemy==2.0.0 I got Found existing installation: SQLAlchemy 1.4.39. Looks like you might have tried this already, but in case this works for anyone else having this problem I guess that's a win.

Upvotes: 1

Related Questions