Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
AI and LLM integration

ConversationalRetrievalChain cannot be imported from langchain.chains

ErrorImportError: 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.

  • 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

    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 langchain
  2. 2Match what you see, then jump to its fix

    1. 1langchain is version 1 or newer and the import line starts with langchain.chainsLegacy chains now live in langchain-classicFix
    2. 2You are writing new code, or a deprecation notice mentions removal in a future major versionThe class is deprecated and meant to be replacedFix

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 ConversationalRetrievalChain

3Verify

The import succeeds.

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

Confirmed by docs.langchain.com

Did this fix it?

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-classic

2Fix

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

Did this fix it?

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:

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.