ConversationalRetrievalChain cannot be imported from langchain.chains
ImportError: cannot import name 'ConversationalRetrievalChain' from 'langchain.chains'Short answer
In LangChain v1 the legacy chains moved out of the langchain package into langchain-classic, so langchain.chains no longer has ConversationalRetrievalChain. Install langchain-classic and import from langchain_classic.chains to keep old code running. The class is deprecated, so new code should use create_history_aware_retriever and create_retrieval_chain.
Fix it fast
1Find out which cause you have
Check which LangChain packages the failing Python has. If langchain is 1.x and langchain-classic is absent, the first cause applies.
python -m pip list | grep -i langchain2Match what you see, then jump to its fix
What does the error look like?
ImportError: cannot import name 'ConversationalRetrievalChain' from 'langchain.chains'Importing ConversationalRetrievalChain from langchain.chains isn't working
Cause 1 of 2
Legacy chains now live in langchain-classic
1Check
The v1 migration guide lists legacy chains among the code moved to langchain-classic, and the classic reference shows the new import path.
python -c "import langchain, sys; print(langchain.__version__)"2Fix
Install langchain-classic and change the import to its namespace. Nothing else in the class changes.
pip install langchain-classic
# in Python
from langchain_classic.chains import ConversationalRetrievalChain3Verify
The import succeeds.
python -c "from langchain_classic.chains import ConversationalRetrievalChain; print('ok')"Confirmed by docs.langchain.com
Cause 2 of 2
The class is deprecated and meant to be replaced
1Check
The classic reference marks ConversationalRetrievalChain as deprecated since 0.1.17 and to be removed in 2.0.0, so relying on it is a short-term fix.
python -m pip show langchain-classic2Fix
Build the same behaviour from two parts: a history-aware retriever that rewrites follow-up questions, and a retrieval chain that answers from the documents. Pass chat history as a list of messages.
from langchain_classic.chains import create_history_aware_retriever, create_retrieval_chain
from langchain_classic.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
rewrite = ChatPromptTemplate.from_messages([
("system", "Rewrite the latest question as a standalone question using the chat history. Do not answer it."),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
])
history_retriever = create_history_aware_retriever(model, retriever, rewrite)
answer = ChatPromptTemplate.from_messages([
("system", "Answer using only this context:\n\n{context}"),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
])
rag_chain = create_retrieval_chain(history_retriever, create_stuff_documents_chain(model, answer))
result = rag_chain.invoke({"input": question, "chat_history": []})3Verify
A follow-up question that uses a pronoun is answered from the right documents.
print(result['answer'])Confirmed by reference.langchain.com
How do I stop it coming back?
- Pin langchain and langchain-classic to the same major version in requirements.txt.
- Read the migration guide before a major upgrade, then grep your code for moved import paths.
- Do not build new features on classes marked deprecated; port them in the same change.
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
LangChain no longer exports AgentExecutor from langchain.agents
ImportError: cannot import name 'AgentExecutor' from 'langchain.agents'
LangChain v1 moved legacy classes into a separate package called langchain-classic, and AgentExecutor lives there now.
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