AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

CharacterTextSplitter/RecursiveCharacterTextSplitter Corrupt Chunk Content When Merging With Non-Lookaround Regex Separator And Keep_separator=False

Using a consuming regex separator (e.g. \s+) with is_separator_regex=True and keep_separator=False causes both CharacterTextSplitter and RecursiveCharacterTextSplitter to insert the literal regex pattern string as a merge separator, producing chunks that contain visible regex syntax and are not substrings of the source text.

highConfidence 95%Langchain-Text-SplittersAffected V<=1.1.2

Origin Analysis

When a regex separator consumes variable text and keep_separator=False, the splitter logic falls back to using the raw regex pattern string as the literal join separator when merging split pieces. This happens because the existing lookaround detection only zeroes the merge separator for zero-width lookaround patterns, leaving consuming patterns to be re-inserted as if they were the actual matched text.
Run the following Python code: ```python from langchain_text_splitters import CharacterTextSplitter, RecursiveCharacterTextSplitter text = "AAA BBB\tCCC\n\nDDD EEE" splitter = CharacterTextSplitter( chunk_size=15, chunk_overlap=0, add_start_index=True, separator=r"\s+", is_separator_regex=True, keep_separator=False, ) for doc in splitter.create_documents([text]): print(repr(doc.page_content), doc.metadata) text2 = "one two\tthree\n\nfour five" splitter2 = RecursiveCharacterTextSplitter( chunk_size=15, chunk_overlap=0, separators=[r"\s+"], is_separator_regex=True, keep_separator=False, ) print(splitter2.split_text(text2)) ``` Observe that the output contains literal `\s+` sequences and the start_index metadata is -1.

Fixing Code Block

Edge Case Audit

This stop-gap disallows previously accepted (though corrupt) configurations, which may break existing pipelines that relied on regex separators with keep_separator=False. It does not reconstruct correct output; users must change their code to use keep_separator=True or lookaround separators. The monkey-patch modifies class methods globally, so it must be executed before any splitter instantiation. For rollback, simply remove the patch and upgrade to the official fixed version when available.

Ecosystem Topology