Registry / llm-agents / langchain-text-splitters

langchain-text-splitters

JSON →
library1.1.2pypypi✓ verified 25d ago

LangChain Text Splitters (current version 1.1.1) provides a comprehensive set of utilities for breaking down large text documents into smaller, manageable chunks. This is crucial for applications like Retrieval-Augmented Generation (RAG) and fitting content within Language Model context windows. As an integral part of the LangChain ecosystem, it maintains an active and rapid release cadence, closely aligned with other LangChain libraries.

pip install langchain-text-splitters
INSTALL
IMPORT
SIG · LANGCHAIN-TEXT-SPL
L
langchain-text-splitters
llm-agentspythonv1.1.2
Install
7.4s avg
Import
1256ms
Disk
72MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.302s · 69.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 7.4s · import 1.210s · 78MB
72MB installed
● package 72MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RecursiveCharacterTextSplitter
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.text_splitter import RecursiveCharacterTextSplitter
Text splitters were moved to a dedicated package. Old `langchain.text_splitter` imports are deprecated or removed.
CharacterTextSplitter
from langchain_text_splitters import CharacterTextSplitter
MarkdownHeaderTextSplitter
from langchain_text_splitters import MarkdownHeaderTextSplitter

Demonstrates the basic usage of the `RecursiveCharacterTextSplitter`, the most commonly recommended text splitter. It shows how to initialize the splitter with `chunk_size` and `chunk_overlap`, and then split a long string into smaller text chunks.

from langchain_text_splitters import RecursiveCharacterTextSplitter # Example long text long_text = ( "LangChain is a framework designed to simplify the creation of applications using large language models. " "It provides tools for chaining together different components, making it easier to build complex LLM workflows. " "Text splitting is a fundamental step in processing long documents for LLMs, ensuring that chunks fit within context windows and maintain semantic coherence. " "The RecursiveCharacterTextSplitter is often the recommended default for general-purpose text." ) # Initialize the splitter # chunk_size: maximum size of each chunk (in characters by default) # chunk_overlap: number of characters to overlap between consecutive chunks text_splitter = RecursiveCharacterTextSplitter( chunk_size=100, chunk_overlap=20 ) # Split the text chunks = text_splitter.split_text(long_text) # Print the chunks for i, chunk in enumerate(chunks): print(f"Chunk {i+1}:\n{chunk}\n---")
Debug
Known issues
breakingThe text splitter modules have been moved from `langchain.text_splitter` to the standalone `langchain-text-splitters` package. Direct imports from `langchain.text_splitter` will no longer work.
fix
Update all text splitter imports from `from langchain.text_splitter import ...` to `from langchain_text_splitters import ...`.
affects: <1.0.0 (old LangChain) to >=1.0.0 (new LangChain/langchain-text-splitters)
gotchaWhen using `create_documents()` method, it expects a *list* of strings (or `Document` objects). Passing a single string will result in each character being treated as a separate document.
fix
For a single string, use `text_splitter.split_text(your_string)`. If you intend to pass multiple strings to `create_documents`, ensure they are wrapped in a list: `text_splitter.create_documents([your_string])`.
affects: All versions
gotchaThe `chunk_size` parameter for character-based splitters specifies the *target* maximum chunk size. Due to the splitter's logic (e.g., trying to split on specific separators first), the actual chunk length may not be exactly `chunk_size`.
fix
Understand that `chunk_size` is a guideline. For more precise length control (e.g., token-based), consider `TokenTextSplitter` or custom `length_function` with an appropriate tokenizer.
affects: All versions
gotchaMixing major versions of LangChain ecosystem packages (e.g., `langchain-text-splitters==1.x.x` with `langchain-core==0.3.x`) can lead to compatibility issues and unexpected behavior.
fix
Always strive to keep all `langchain-` prefixed packages within the same major version series (e.g., all `1.x.x` or all `0.3.x`) to ensure compatibility.
affects: All versions where major versions of LangChain packages diverge
gotchaSome specialized splitters, like `MarkdownHeaderTextSplitter` and `HTMLHeaderTextSplitter`, do not inherit from the base `TextSplitter` class. This means they might have slightly different method signatures or expectations.
fix
Always consult the specific documentation or API reference for specialized text splitters to understand their unique behavior and interfaces.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'langchain.text_splitter' OR ImportError: cannot import name 'RecursiveCharacterTextSplitter' from 'langchain.text_splitter'
The `RecursiveCharacterTextSplitter` and other text splitters have been moved from the `langchain` package to the dedicated `langchain_text_splitters` package as part of LangChain's modularization.
fix
First, ensure `langchain-text-splitters` is installed: `pip install -U langchain-text-splitters`. Then, update your import statement: `from langchain_text_splitters import RecursiveCharacterTextSplitter`.
ModuleNotFoundError: No module named 'langchain_text_splitters'
The `langchain-text-splitters` package, which contains the text splitting utilities, has not been installed in your Python environment.
fix
Install the package using pip: `pip install langchain-text-splitters`.
AttributeError: module 'langchain.text_splitter' has no attribute 'RecursiveCharacterTextSplitter'
This error occurs when an older version of LangChain is installed, or the `langchain-text-splitters` package is not correctly referenced, leading to the `RecursiveCharacterTextSplitter` class not being found in the `langchain.text_splitter` module.
fix
Ensure you have `langchain-text-splitters` installed and are importing from the correct module: `pip install -U langchain-text-splitters` and then `from langchain_text_splitters import RecursiveCharacterTextSplitter`.
ImportError: cannot import name 'RegexTextSplitter' from 'langchain.text_splitter'
The `RegexTextSplitter` class has been deprecated and its functionality is now integrated into `RecursiveCharacterTextSplitter` using the `is_separator_regex` parameter.
fix
Use `RecursiveCharacterTextSplitter` and set `is_separator_regex=True` with your regular expression separators: `from langchain_text_splitters import RecursiveCharacterTextSplitter; text_splitter = RecursiveCharacterTextSplitter(separators=[r'\n\n', r'\n'], is_separator_regex=True)`.
Upgrade
Version history
1.1.2latest on PyPI · released Apr 16, 2026
Audit
Dependencies
langchain-corerequiredCore utilities and interfaces for LangChain components.
Agent activity
86 hits · last 30 days
node
82
OpenAI (training)
1
Resources
langchain-text-splitters — pip install langchain-text-splitters · libregistry