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.
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.