langchain-ai/langchain · error · ImportError

Unable to import from langchain_text_splitters. Please speci

Error message

Unable to import from langchain_text_splitters. Please specify text_splitter or install langchain_text_splitters with `pip install -U langchain-text-splitters`.

What it means

`BaseLoader.load_and_split` (document_loaders/base.py) defaults to a `RecursiveCharacterTextSplitter` from the optional dependency `langchain_text_splitters`; if that import failed at module load, calling `load_and_split(text_splitter=None)` raises `ImportError` instructing you to install it or pass your own splitter.

Source

Thrown at libs/core/langchain_core/document_loaders/base.py:81

            text_splitter: `TextSplitter` instance to use for splitting documents.

                Defaults to `RecursiveCharacterTextSplitter`.

        Raises:
            ImportError: If `langchain-text-splitters` is not installed and no
                `text_splitter` is provided.

        Returns:
            List of `Document` objects.
        """
        if text_splitter is None:
            if not _HAS_TEXT_SPLITTERS:
                msg = (
                    "Unable to import from langchain_text_splitters. Please specify "
                    "text_splitter or install langchain_text_splitters with "
                    "`pip install -U langchain-text-splitters`."
                )
                raise ImportError(msg)

            text_splitter_: TextSplitter = RecursiveCharacterTextSplitter()
        else:
            text_splitter_ = text_splitter
        docs = self.load()
        return text_splitter_.split_documents(docs)

    # Attention: This method will be upgraded into an abstractmethod once it's
    #            implemented in all the existing subclasses.
    def lazy_load(self) -> Iterator[Document]:
        """A lazy loader for `Document`.

        Yields:
            The `Document` objects.
        """
        if type(self).load != BaseLoader.load:
            return iter(self.load())
        msg = f"{self.__class__.__name__} does not implement lazy_load()"

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Install the dependency: `pip install -U langchain-text-splitters` (or add it to pyproject dependencies)
  2. Or pass an explicit splitter: `loader.load_and_split(text_splitter=RecursiveCharacterTextSplitter(...))` from wherever it is available
  3. Verify with `python -c "import langchain_text_splitters"`
  4. If managing with uv: `uv add langchain-text-splitters` / ensure it's in the relevant dependency group

Example fix

# before
docs = loader.load_and_split()  # ImportError if package missing

# after (option 1)
# pip install -U langchain-text-splitters
docs = loader.load_and_split()

# after (option 2)
docs = loader.load_and_split(text_splitter=CharacterTextSplitter(chunk_size=1000))
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util, contextlib

def text_splitters_available() -> bool:
    return importlib.util.find_spec('langchain_text_splitters') is not None

splitter = None
if text_splitters_available():
    from langchain_text_splitters import RecursiveCharacterTextSplitter
    splitter = RecursiveCharacterTextSplitter()

Try / catch

try:
    docs = loader.load_and_split()
except ImportError as e:
    if 'langchain_text_splitters' in str(e):
        docs = loader.load()  # split later, or install the package
    else:
        raise

Prevention

When it happens

Trigger: Calling `loader.load_and_split()` on any document loader subclass when `langchain-text-splitters` is not installed in the environment; slim deployments (core-only installs); virtualenvs built from a partial requirements list.

Common situations: Installing only `langchain-core` (or a partner package) without the text-splitters extra; environments pinned to old versions where the package moved out of core; CI images trimmed for size.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/f12857e4200fc2f0. Report an issue: GitHub.