Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
AI and LLM integration

RecursiveCharacterTextSplitter is missing after a LangChain upgrade

ErrorImportError: cannot import name 'RecursiveCharacterTextSplitter' from 'langchain'

Short answer

Text splitters have their own package, langchain-text-splitters. The class is imported with from langchain_text_splitters import RecursiveCharacterTextSplitter, not from the langchain package. Install the package into the Python that runs your code, change the import, and use split_text for strings or create_documents for Document objects.

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

Fix it fast

  1. 1Find out which cause you have

    Ask pip whether the splitters package is installed in the interpreter that fails.

    python -m pip show langchain-text-splitters
  2. 2Match what you see, then jump to its fix

    1. 1pip show finds the package, but the code says from langchain.text_splitter import ...The import still uses the old langchain pathFix
    2. 2ModuleNotFoundError: No module named 'langchain_text_splitters'langchain-text-splitters is not installed in this environmentFix

What does the error look like?

  • ImportError: cannot import name 'RecursiveCharacterTextSplitter' from 'langchain'
  • ModuleNotFoundError: No module named 'langchain_text_splitters'

Cause 1 of 2

The import still uses the old langchain path

1Check

Search for the old module name.

grep -rn "text_splitter" --include=*.py .

2Fix

Import from the dedicated module.

from langchain_text_splitters import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_text(text)

3Verify

The import and a split run without an error.

python -c "from langchain_text_splitters import RecursiveCharacterTextSplitter; print('ok')"

Confirmed by docs.langchain.com

Did this fix it?

Cause 2 of 2

langchain-text-splitters is not installed in this environment

1Check

pip show prints nothing for the package. In a notebook or an IDE, the Python may differ from your terminal's.

python -c "import sys; print(sys.executable)"
python -m pip list | grep -i langchain

2Fix

Install with that same Python, then restart the kernel or the IDE run configuration.

python -m pip install -U langchain-text-splitters

3Verify

pip show prints a version and the import works.

python -m pip show langchain-text-splitters

Confirmed by pypi.org

Did this fix it?

How do I stop it coming back?

  • List langchain-text-splitters in requirements.txt next to langchain; do not assume one pulls the other.
  • Use python -m pip so installs go to the interpreter you run.
  • Check a tutorial's import lines against the current docs before copying them.

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.