fix: MarkdownHeaderSplitter drops content before an embedded header or code-fence comment - #11920
Merged
sjrl merged 1 commit intoJul 9, 2026
Conversation
|
@Otis0408 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
sjrl
reviewed
Jul 9, 2026
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
With keep_headers=False the secondary split re-stripped a leading header using a non-anchored re.search, which matched the first '#' line anywhere in the body (an embedded lower-level header, or a '#' comment inside a fenced code block) and discarded everything before it. Anchor the match to the start of the content with re.match so only a genuine leading header is stripped.
otiscuilei
force-pushed
the
fix/markdown-splitter-content-loss
branch
from
July 9, 2026 05:29
399ad7a to
440f4fc
Compare
Contributor
Author
|
Thanks for the quick review and merge! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
MarkdownHeaderSplittersilently drops document content whenkeep_headers=Falseand a secondary split is configured.In
_apply_secondary_splitting, the header-stripping step used a non-anchoredre.search(self._header_pattern, doc.content). Because the header pattern is(?m)^(#{1,6}) (.+)$(MULTILINE),re.searchmatches the first#line anywhere in the chunk, not just a genuine leading header. Everything before that match (doc.content[header_match.end():]) is then discarded.This triggers whenever a chunk contains a
#line that is not its leading header, for example:##whenheader_split_levels=[1]), or#comment inside a fenced code block (e.g. a Python comment).Anyone using
MarkdownHeaderSplitter(keep_headers=False, secondary_split=...)on realistic Markdown (nested sections or code samples) loses the prose that precedes the first such#line.Fix
Anchor the match to the start of the chunk with
re.matchinstead ofre.search, so only a header at the very beginning of the content is stripped.re.matchanchors to position 0 regardless of the MULTILINE flag, leaving embedded headers and code-fence comments untouched.Before / after
Tests
Two regression tests in
test/components/preprocessors/test_markdown_header_splitter.py, one per trigger case:test_secondary_split_keeps_content_before_embedded_headertest_secondary_split_keeps_content_before_code_fence_commentBoth fail on the pre-fix source (content dropped) and pass with the fix; the full file (34 tests) passes. A release note is included under
releasenotes/notes/.