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

MarkdownTextSplitter And LatexTextSplitter Never Split On Headings/Sections (Separators Are Regex-Escaped)

MarkdownTextSplitter and LatexTextSplitter fail to pass is_separator_regex=True to their parent RecursiveCharacterTextSplitter, causing their intended regex separators to be escaped and never match. This silently falls back to generic whitespace splitting, disregarding document structure.

mediumConfidence 95%Langchainlangchain-coreChainsAffected V1.1.3

Origin Analysis

Both subclasses call super().__init__() without setting is_separator_regex=True. RecursiveCharacterTextSplitter therefore applies re.escape to each separator, converting regex patterns like '\n#{1,6} ' into literal strings that cannot match real headings or LaTeX sections.
Run the following Python code: from langchain_text_splitters import MarkdownTextSplitter, LatexTextSplitter, RecursiveCharacterTextSplitter, Language md = "# T\nintro\n## B\ntext b\n### C\ntext c\n" print(MarkdownTextSplitter(chunk_size=20, chunk_overlap=0).split_text(md)) print(RecursiveCharacterTextSplitter.from_language(Language.MARKDOWN, chunk_size=20, chunk_overlap=0).split_text(md)) tex = "\\section{A} aa bb\n\\section{B} cc dd\n\\section{C} ee ff" print(LatexTextSplitter(chunk_size=24, chunk_overlap=0).split_text(tex)) print(RecursiveCharacterTextSplitter.from_language(Language.LATEX, chunk_size=24, chunk_overlap=0).split_text(tex)) s = MarkdownTextSplitter(chunk_size=20, chunk_overlap=0) print(s._is_separator_regex, s._separators) Observe that the direct MarkdownTextSplitter and LatexTextSplitter produce chunks that ignore heading/section boundaries, while the from_language variants (which set is_separator_regex=True) split correctly.

Fixing Code Block

# In langchain_text_splitters/markdown.py class MarkdownTextSplitter(RecursiveCharacterTextSplitter): def __init__(self, **kwargs: Any) -> None: separators = self.get_separators_for_language(Language.MARKDOWN) super().__init__(separators=separators, is_separator_regex=True, **kwargs) # In langchain_text_splitters/latex.py class LatexTextSplitter(RecursiveCharacterTextSplitter): def __init__(self, **kwargs: Any) -> None: separators = self.get_separators_for_language(Language.LATEX) super().__init__(separators=separators, is_separator_regex=True, **kwargs)
Passing is_separator_regex=True to the parent constructor prevents re.escape from being applied to the separators. The separators from get_separators_for_language are already valid regular expressions, so they will now match actual Markdown headings and LaTeX section commands, restoring the intended splitting behavior.

Edge Case Audit

This change modifies chunk boundaries for all existing users of MarkdownTextSplitter and LatexTextSplitter. Documents that previously split by paragraphs or spaces may now split at heading/section markers, altering chunk sizes and content distribution. Users who depend on the old (broken) behavior should pin to a version before the fix or explicitly override is_separator_regex=False. Before upgrading, test with representative documents and consider rebuilding any downstream indexes or vector stores.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state