ModuleNotFoundError for langchain.chains, or RetrievalQA that will not import
ModuleNotFoundError: 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.
Fix it fast
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__)"2Match what you see, then jump to its fix
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-classic2Fix
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 RetrievalQA3Verify
The import now runs without an error.
python -c "from langchain_classic.chains import RetrievalQA; print('ok')"Confirmed by docs.langchain.com
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 32Fix
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
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:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
AI
ConversationalRetrievalChain cannot be imported from langchain.chains
Importing ConversationalRetrievalChain from langchain.chains isn't working
In LangChain v1 the legacy chains moved out of the langchain package into langchain-classic, so langchain.chains no longer has ConversationalRetrievalChain.
2 causes, each with a fixAI
Azure OpenAI says the API deployment for this resource does not exist
The API deployment for this resource does not exist
Azure OpenAI does not take a model name in the model field.
2 causes, each with a fixAI
Azure OpenAI fails with an SSL certificate verification error
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED]
The client checks the endpoint's certificate against its CA bundle, which is certifi by default.
2 causes, each with a fixAI
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 fixAI
Gemini API returns 429 RESOURCE_EXHAUSTED on the free tier
429 RESOURCE_EXHAUSTED
Gemini limits are applied per Google Cloud project, not per API key, so a new key in the same project does not reset anything.
3 causes, each with a fixAI
ChatPromptTemplate is missing the variable agent_scratchpad
Expected: ['agent_scratchpad', 'input']
A tool-calling agent keeps its tool calls and tool results in a prompt slot named agent_scratchpad.
2 causes, each with a fix