From 9cea796671af7ab66489a2f0b6238b6225d75ae3 Mon Sep 17 00:00:00 2001 From: mhwang-stripe <67120022+mhwang-stripe@users.noreply.github.com> Date: Wed, 4 Oct 2023 12:41:20 -0700 Subject: [PATCH] Make langchain compatible with SQLAlchemy<1.4.0 (#11390) ## Description Currently SQLAlchemy >=1.4.0 is a hard requirement. We are unable to run `from langchain.vectorstores import FAISS` with SQLAlchemy <1.4.0 due to top-level imports, even if we aren't even using parts of the library that use SQLAlchemy. See Testing section for repro. Let's make it so that langchain is still compatible with SQLAlchemy <1.4.0, especially if we aren't using parts of langchain that require it. The main conflict is that SQLAlchemy removed `declarative_base` from `sqlalchemy.ext.declarative` in 1.4.0 and moved it to `sqlalchemy.orm`. We can fix this by try-catching the import. This is the same fix as applied in https://github.com/langchain-ai/langchain/pull/883. (I see that there seems to be some refactoring going on about isolating dependencies, e.g. https://github.com/langchain-ai/langchain/commit/c87e9fb2ce0ae617e3b2edde52421c80adef54cc, so if this issue will be eventually fixed by isolating imports in langchain.vectorstores that also works). ## Issue I can't find a matching issue. ## Dependencies No additional dependencies ## Maintainer @hwchase17 since you reviewed https://github.com/langchain-ai/langchain/pull/883 ## Testing I didn't add a test, but I manually tested this. 1. Current failure: ``` langchain==0.0.305 sqlalchemy==1.3.24 ``` ``` python python -i >>> from langchain.vectorstores import FAISS Traceback (most recent call last): File "", line 1, in File "/pay/src/zoolander/vendor3/lib/python3.8/site-packages/langchain/vectorstores/__init__.py", line 58, in from langchain.vectorstores.pgembedding import PGEmbedding File "/pay/src/zoolander/vendor3/lib/python3.8/site-packages/langchain/vectorstores/pgembedding.py", line 10, in from sqlalchemy.orm import Session, declarative_base, relationship ImportError: cannot import name 'declarative_base' from 'sqlalchemy.orm' (/pay/src/zoolander/vendor3/lib/python3.8/site-packages/sqlalchemy/orm/__init__.py) ``` 2. This fix: ``` langchain== sqlalchemy==1.3.24 ``` ``` python python -i >>> from langchain.vectorstores import FAISS ``` --- libs/langchain/langchain/vectorstores/pgembedding.py | 7 ++++++- libs/langchain/langchain/vectorstores/pgvector.py | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/libs/langchain/langchain/vectorstores/pgembedding.py b/libs/langchain/langchain/vectorstores/pgembedding.py index 7a3df62c29..0d4562f96a 100644 --- a/libs/langchain/langchain/vectorstores/pgembedding.py +++ b/libs/langchain/langchain/vectorstores/pgembedding.py @@ -7,7 +7,12 @@ from typing import Any, Dict, Iterable, List, Optional, Tuple, Type import sqlalchemy from sqlalchemy import func from sqlalchemy.dialects.postgresql import JSON, UUID -from sqlalchemy.orm import Session, declarative_base, relationship +from sqlalchemy.orm import Session, relationship + +try: + from sqlalchemy.orm import declarative_base +except ImportError: + from sqlalchemy.ext.declarative import declarative_base from langchain.docstore.document import Document from langchain.schema.embeddings import Embeddings diff --git a/libs/langchain/langchain/vectorstores/pgvector.py b/libs/langchain/langchain/vectorstores/pgvector.py index 318ce607fd..30e1c2d470 100644 --- a/libs/langchain/langchain/vectorstores/pgvector.py +++ b/libs/langchain/langchain/vectorstores/pgvector.py @@ -23,7 +23,12 @@ import numpy as np import sqlalchemy from sqlalchemy import delete from sqlalchemy.dialects.postgresql import UUID -from sqlalchemy.orm import Session, declarative_base +from sqlalchemy.orm import Session + +try: + from sqlalchemy.orm import declarative_base +except ImportError: + from sqlalchemy.ext.declarative import declarative_base from langchain.docstore.document import Document from langchain.schema.embeddings import Embeddings