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

LangChain - ImportError : cannot import name Executable from sqlalchemy #32

Closed
hab6 opened this issue Feb 15, 2024 · 10 comments
Closed
Assignees

Comments

@hab6
Copy link
Contributor

hab6 commented Feb 15, 2024

The following langchain test case succeeds when run with SQLAlchemy 2.0.27 but fails when run with SQLAlchemy 1.4.51.

Test Case

import os
import sys
import sqlalchemy
import sqlalchemy_ingres
import langchain
from langchain.sql_database import SQLDatabase
print('Python %s on %s' % (sys.version, sys.platform))
print('SQLAlchemy %r' % sqlalchemy.__version__)
print('langchain %r' % langchain.__version__)
driver_name = os.environ.get('SQLALCHEMY_INGRES_ODBC_DRIVER_NAME') or 'Actian'
print('SQLALCHEMY_INGRES_ODBC_DRIVER_NAME %r' % driver_name)
con_str = os.environ['DATABASE_URI']
db = SQLDatabase.from_uri(con_str)

Test Environment Versions

C:\>python -V
Python 3.10.7

langchain           0.1.7
langchain-community 0.0.20
langchain-core      0.1.23
langsmith           0.0.87
pyodbc              5.1.0
pypyodbc            1.3.6
setuptools          63.2.0
SQLAlchemy          1.4.51
sqlalchemy-ingres   0.0.5.dev1

Stack Dump & Error

C:\test> python repro1.py
Traceback (most recent call last):
  File "C:\test\repro1.py", line 9, in <module>
    from langchain.sql_database import SQLDatabase
  File "C:\test\.venv\lib\site-packages\langchain\sql_database.py",
     line 2, in <module>
    from langchain_community.utilities.sql_database import SQLDatabase
  File "C:\test\.venv\lib\site-packages\langchain_community\utilities\sql_database.py",
     line 9, in <module>
    from sqlalchemy import (
ImportError: cannot import name 'Executable' from 'sqlalchemy'
     (C:\test\.venv\lib\site-packages\sqlalchemy\__init__.py)
@hab6
Copy link
Contributor Author

hab6 commented Feb 16, 2024

This problem can be reproduced with the single import line:

from langchain.sql_database import SQLDatabase

e.g.

Works with SQLAlchemy 2.0.27

C:\test>pip list | grep SQLAlchemy
SQLAlchemy          2.0.27
C:\test>python
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from langchain.sql_database import SQLDatabase
>>>

Fails with SQLAlchemy 1.4.51

C:\test>pip list | grep SQLAlchemy
SQLAlchemy          1.4.51
C:\test>python
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from langchain.sql_database import SQLDatabase
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\mhabiger\Data\work\saDialect\github32\.venv\lib\site-packages\langchain\sql_database.py", line 2, in <module>
    from langchain_community.utilities.sql_database import SQLDatabase
  File "C:\Users\mhabiger\Data\work\saDialect\github32\.venv\lib\site-packages\langchain_community\utilities\sql_database.py", line 9, in <module>
    from sqlalchemy import (
ImportError: cannot import name 'Executable' from 'sqlalchemy' (C:\Users\mhabiger\Data\work\saDialect\github32\.venv\lib\site-packages\sqlalchemy\__init__.py)
>>>

@hab6
Copy link
Contributor Author

hab6 commented Feb 16, 2024

Key pieces of the SQLAlchemy support for LangChain are implemented in sql_database.py

The langchain current version (0.0.20) has this requirement (per link1, link2):

SQLAlchemy = ">=1.4,<3"

So, this version (0.0.20) of LangChain should work with SQLAlchemy 1.4.51.

@hab6 hab6 self-assigned this Feb 16, 2024
@hab6
Copy link
Contributor Author

hab6 commented Feb 16, 2024

This is a bug in langchain which causes an incompatibility with SQLAlchemy 1.x.

Langchain PR 17191 introduced the problem when merged to the master branch on Feb 7, 2024.

Langchain issue 17519 was opened on Feb 14, 2024 with a number of people encountering the same problem.

The fix was merged to master via 17520

In the meantime, the workaround is to downgrade langchain from 0.1.7 to 0.1.6. This works for me.

UPDATE/CORRECTION: The workaround is to downgrade both the langchain and langchain-community packages to these versions:

langchain           0.1.6
langchain-community 0.0.19

@clach04
Copy link
Member

clach04 commented Feb 17, 2024

nice work!

@vayvay0993
Copy link

vayvay0993 commented Feb 17, 2024

In the meantime, the workaround is to download langchain from 0.1.7 to 0.1.6. This works for me.

I've tried to l install langchain of 0.1.6 version and my sqlalchemy version is 1.4.46,
but I still got import error while try to import create_extraction_chain from langchain.chains

ImportError: cannot import name 'Executable' from 'sqlalchemy'

@clach04
Copy link
Member

clach04 commented Feb 17, 2024

This is a bug in langchain which causes an incompatibility with SQLAlchemy 1.x.

Langchain PR 17191 introduced the problem when merged to the master branch on Feb 7, 2024.

Langchain issue 17519 was opened on Feb 14, 2024 with a number of people encountering the same problem.

The fix was merged to master via 17520

In the meantime, the workaround is to download langchain from 0.1.7 to 0.1.6. This works for me.

@hab6 Is the other workaround to pull headrevs of langchain that includes the fix?

@vayvay0993 you may need to check in with the langchain product, it doesn't seem like the issue relates to this project based on the traceback and @hab6 research.

@hab6
Copy link
Contributor Author

hab6 commented Feb 19, 2024

In the meantime, the workaround is to download langchain from 0.1.7 to 0.1.6. This works for me.

I've tried to l install langchain of 0.1.6 version and my sqlalchemy version is 1.4.46, but I still got import error while try to import create_extraction_chain from langchain.chains

ImportError: cannot import name 'Executable' from 'sqlalchemy'

@vayvay0993 @clach04 My prior update on the workaround was incorrect. The workaround in fact requires that the langchain-community package be downgraded to version 0.0.19. Along with this, the langchain package must also be downgraded to version 0.1.6 to avoid other errors.

In summary, to resolve error:

ImportError: cannot import name 'Executable' from 'sqlalchemy'

The workaround is to use these package versions:

langchain           0.1.6
langchain-community 0.0.19

@hab6
Copy link
Contributor Author

hab6 commented Feb 19, 2024

@vayvay0993 The import works for me with these package versions.

(.venv) λ le | grep -e langchain -e chemy
langchain           0.1.6
langchain-community 0.0.19
langchain-core      0.1.23
SQLAlchemy          1.4.46
sqlalchemy-ingres   0.0.4

(.venv) λ python
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from langchain.chains import create_extraction_chain
>>>

Could you please retest and see if this works for you?

@vayvay0993
Copy link

Yes, I really appreciate your help! 🙏

@hab6
Copy link
Contributor Author

hab6 commented Feb 19, 2024

This issue is now fixed with Langchain version 0.1.8 released to PyPi today. With these versions installed:

langchain           0.1.8
langchain-community 0.0.21

SQLAlchemy 1.4.51 no longer reports the ImportError.

@hab6 hab6 closed this as completed Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants