Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
AI and LLM integration

ModuleNotFoundError for langchain.chains, or RetrievalQA that will not import

ErrorModuleNotFoundError: No module named 'langchain.chains'

Short answer

LangChain 1.x moved its legacy chains out of the langchain package into langchain-classic, so langchain.chains and RetrievalQA are gone from the old path. Install langchain-classic and import from langchain_classic.chains, or better, replace RetrievalQA with create_retrieval_chain because the class is deprecated.

  • Applies to Python 3.12, langchain 1.x, langchain-classic 1.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the installed LangChain packages and the version of langchain. A 1.x version with no langchain-classic in the list points to the first cause.

    python -m pip list | grep -i langchain
    python -c "import langchain; print(langchain.__version__)"
  2. 2Match what you see, then jump to its fix

    1. 1langchain reports 1.x and the failing line is from langchain.chains import RetrievalQAThe legacy chains were moved to langchain-classicFix
    2. 2A deprecation warning names RetrievalQA, or you are about to write new retrieval codeRetrievalQA is deprecated, so the import is only a stopgapFix

What does the error look like?

  • ModuleNotFoundError: No module named 'langchain.chains'
  • ImportError: cannot import name 'RetrievalQA' from 'langchain.chains'

Cause 1 of 2

The legacy chains were moved to langchain-classic

1Check

The LangChain v1 migration guide names legacy chains among the code that left the main package and now sits in langchain-classic, imported as langchain_classic.*.

python -m pip show langchain-classic

2Fix

Install the package and change only the import line. The class keeps its name and arguments.

pip install langchain-classic

# in your module
from langchain_classic.chains import RetrievalQA

3Verify

The import now runs without an error.

python -c "from langchain_classic.chains import RetrievalQA; print('ok')"

Confirmed by docs.langchain.com

Did this fix it?

Cause 2 of 2

RetrievalQA is deprecated, so the import is only a stopgap

1Check

The langchain-classic reference marks RetrievalQA as deprecated since 0.1.17 and scheduled for removal in 2.0.0. It lists create_retrieval_chain with create_stuff_documents_chain as the migration path.

python -m pip show langchain-classic | head -n 3

2Fix

Build the chain from a document-combining step and a retrieval step. The prompt needs a {context} slot for the retrieved text and an {input} slot for the question. Note that this chain takes input and returns answer, not query and result.

from langchain_classic.chains import create_retrieval_chain
from langchain_classic.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "Use only this context to answer:\n\n{context}"),
    ("human", "{input}"),
])
chain = create_retrieval_chain(retriever, create_stuff_documents_chain(llm, prompt))
print(chain.invoke({"input": "What does the contract say about notice?"})["answer"])

3Verify

The chain returns a dictionary that has an answer key.

result = chain.invoke({"input": "test question"})
print(sorted(result.keys()))

Confirmed by reference.langchain.com

Did this fix it?

How do I stop it coming back?

  • Read the migration guide before moving a project to a new major LangChain version.
  • Keep langchain and langchain-classic on the same major version in requirements.txt.
  • Search your code for from langchain.chains after every upgrade.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

AI

LangChain says it could not import the chromadb package

ImportError: Could not import chromadb python package. Please install it with `pip install chromadb`.

The message comes from LangChain's old Chroma class, which cannot import chromadb in the Python that runs your code.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.