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

ImportError: cannot import name 'Mapped' from 'sqlalchemy.orm' (/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/sqlalchemy/orm/__init__.py) #3655

Closed
nickyi1990 opened this issue Apr 27, 2023 · 6 comments

Comments

@nickyi1990
Copy link

have no idea, just install langchain and run code below, the error popup, any idea?

from langchain.document_loaders import UnstructuredPDFLoader, OnlinePDFLoader, UnstructuredImageLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-756b21b77eab> in <module>
----> 1 from langchain.document_loaders import UnstructuredPDFLoader, OnlinePDFLoader, UnstructuredImageLoader
      2 from langchain.text_splitter import RecursiveCharacterTextSplitter

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/__init__.py in <module>
      4 from typing import Optional
      5 
----> 6 from langchain.agents import MRKLChain, ReActChain, SelfAskWithSearchChain
      7 from langchain.cache import BaseCache
      8 from langchain.callbacks import (

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/agents/__init__.py in <module>
      1 """Interface for agents."""
----> 2 from langchain.agents.agent import (
      3     Agent,
      4     AgentExecutor,
      5     AgentOutputParser,

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/agents/agent.py in <module>
     15 from langchain.agents.tools import InvalidTool
     16 from langchain.callbacks.base import BaseCallbackManager
---> 17 from langchain.chains.base import Chain
     18 from langchain.chains.llm import LLMChain
     19 from langchain.input import get_color_mapping

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/chains/__init__.py in <module>
      1 """Chains are easily reusable components which can be linked together."""
----> 2 from langchain.chains.api.base import APIChain
      3 from langchain.chains.api.openapi.chain import OpenAPIEndpointChain
      4 from langchain.chains.combine_documents.base import AnalyzeDocumentChain
      5 from langchain.chains.constitutional_ai.base import ConstitutionalChain

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/chains/api/base.py in <module>
      6 from pydantic import Field, root_validator
      7 
----> 8 from langchain.chains.api.prompt import API_RESPONSE_PROMPT, API_URL_PROMPT
      9 from langchain.chains.base import Chain
     10 from langchain.chains.llm import LLMChain

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/chains/api/prompt.py in <module>
      1 # flake8: noqa
----> 2 from langchain.prompts.prompt import PromptTemplate
      3 
      4 API_URL_PROMPT_TEMPLATE = """You are given the below API Documentation:
      5 {api_docs}

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/prompts/__init__.py in <module>
      1 """Prompt template classes."""
      2 from langchain.prompts.base import BasePromptTemplate, StringPromptTemplate
----> 3 from langchain.prompts.chat import (
      4     AIMessagePromptTemplate,
      5     BaseChatPromptTemplate,

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/prompts/chat.py in <module>
      8 from pydantic import BaseModel, Field
      9 
---> 10 from langchain.memory.buffer import get_buffer_string
     11 from langchain.prompts.base import BasePromptTemplate, StringPromptTemplate
     12 from langchain.prompts.prompt import PromptTemplate

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/memory/__init__.py in <module>
     21 from langchain.memory.summary_buffer import ConversationSummaryBufferMemory
     22 from langchain.memory.token_buffer import ConversationTokenBufferMemory
---> 23 from langchain.memory.vectorstore import VectorStoreRetrieverMemory
     24 
     25 __all__ = [

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/memory/vectorstore.py in <module>
      8 from langchain.memory.utils import get_prompt_input_key
      9 from langchain.schema import Document
---> 10 from langchain.vectorstores.base import VectorStoreRetriever
     11 
     12 

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/vectorstores/__init__.py in <module>
      1 """Wrappers on top of vector stores."""
----> 2 from langchain.vectorstores.analyticdb import AnalyticDB
      3 from langchain.vectorstores.annoy import Annoy
      4 from langchain.vectorstores.atlas import AtlasDB
      5 from langchain.vectorstores.base import VectorStore

/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/langchain/vectorstores/analyticdb.py in <module>
      9 from sqlalchemy import REAL, Index
     10 from sqlalchemy.dialects.postgresql import ARRAY, JSON, UUID
---> 11 from sqlalchemy.orm import Mapped, Session, declarative_base, relationship
     12 from sqlalchemy.sql.expression import func
     13 

ImportError: cannot import name 'Mapped' from 'sqlalchemy.orm' (/home/nvme2/kunzhong/anaconda3/lib/python3.8/site-packages/sqlalchemy/orm/__init__.py)
@hrist0stoichev
Copy link
Contributor

Same thing here. I think Mapped is from SQLAlchemy >= 2.0.0 but the current requirement is SQLAlchemy > 1.3. I hope the fix remains compatible with SQLAlchemy > 1.3 and not the other way around.

@MichaelJThomas-2016
Copy link

MichaelJThomas-2016 commented Apr 28, 2023

Mapped was added in 1.4:

    .. tip::

        The :class:`_orm.Mapped` class represents attributes that are handled
        directly by the :class:`_orm.Mapper` class. It does not include other
        Python descriptor classes that are provided as extensions, including
        :ref:`hybrids_toplevel` and the :ref:`associationproxy_toplevel`.
        While these systems still make use of ORM-specific superclasses
        and structures, they are not :term:`instrumented` by the
        :class:`_orm.Mapper` and instead provide their own functionality
        when they are accessed on a class.

    .. versionadded:: 1.4

I am updating the deps to declare 1.4 as a min requirement, but i cannot assign the issue to myself :/

@vempaliakhil96
Copy link
Contributor

vempaliakhil96 commented Apr 29, 2023

Hey added a fix for it in this PR. SQLAlchemy support for Mapped is from v1.4. #3716. Meanwhile you can simply upgrade your SQLAlchemy package to 1.4

@hrist0stoichev
Copy link
Contributor

Updating SQLAlchemy from 1.3 to 1.4 is not "easy". It would be much better if Mapped is removed and the constraint for >1.3 is kept instead. I think Mapped is only for annotation purposes and doesn't serve any other use.

@MichaelJThomas-2016
Copy link

Ahh, ok. I always thought the big migration was from 1.* -> 2. If it's there for annotations, then yeah, I agree with you.

hwchase17 pushed a commit that referenced this issue May 2, 2023
Related to [this
issue.](#3655 (comment))

The `Mapped` SQLAlchemy class is introduced in SQLAlchemy 1.4 but the
migration from 1.3 to 1.4 is quite challenging so, IMO, it's better to
keep backwards compatibility and not change the SQLAlchemy requirements
just because of type annotations.
@dosubot
Copy link

dosubot bot commented Sep 17, 2023

Hi, @nickyi1990! I'm Dosu, and I'm helping the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

Based on my understanding, the issue you reported is an ImportError that occurs when trying to import 'Mapped' from 'sqlalchemy.orm'. It seems that the error is encountered when running code that involves the 'langchain' package. In the comments, users hrist0stoichev and MichaelJThomas-2016 discuss that 'Mapped' is from SQLAlchemy 1.4, but the current requirement is SQLAlchemy 1.3. vempaliakhil96 suggests upgrading SQLAlchemy to 1.4 and provides a fix in a PR, while hrist0stoichev suggests removing 'Mapped' and keeping the constraint for >1.3. MichaelJThomas-2016 agrees with this suggestion.

The issue has been resolved by removing the import of 'Mapped' from 'sqlalchemy.orm' and keeping the constraint for SQLAlchemy version >1.3.

Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.

Thank you for your contribution to LangChain!

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 17, 2023
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 24, 2023
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 24, 2023
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

4 participants