pythonlangchainretrieval-augmented-generation

ImportError: cannot import name 'RetrievalQA' from 'langchain.chains' in Python project


I’m trying to use LangChain in my Python project. My current retriever.py contains:

from langchain.chains import RetrievalQA

But when I run my code, I get:

ImportError: cannot import name 'RetrievalQA' from 'langchain.chains'

My current environment:

Python 3.13.7  
langchain version: 1.0.5  
OS: Windows 11

Solution

  • In LangChain 1.x, the legacy RetrievalQA class is no longer exported from langchain.chains (API was reorganized). Your import works only on older 0.x versions.

    You could Import the legacy class from its module path

    from langchain.chains.retrieval_qa.base import RetrievalQA
    

    (Works if the class still ships in your version, but it’s deprecated in favor of the functions above.)

    Or you could try and Pin to a compatible older LangChain

    pip install "langchain<0.1.0"
    

    Then:

    from langchain.chains import RetrievalQA