Repository navigation
fix: MarkdownHeaderSplitter drops the leading header of an unsplit document - #12478
Conversation
|
@Vedant-Agarwal is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
…cument
`_apply_secondary_splitting` stripped a leading header line from every chunk
when `keep_headers=False`, matching any header level 1-6 regardless of
`header_split_levels`. For chunks produced by a real header split this is a
no-op, because their content begins after the header line. It only took effect
on the two fallback paths of `_split_text_by_markdown_headers` ("no headers
found" and "only headers with no content"), which return the whole document
with empty meta. There the header line was cut from the content and, with no
`header` key in the meta to hold it, lost from the output entirely.
This also hit documents whose only header sits at a level excluded from
`header_split_levels`.
The strip is now guarded on the chunk carrying `header` metadata, i.e. on it
actually having come from a header split, where removing the header line from
the content avoids duplicating what the metadata already holds.
Co-Authored-By: Claude Opus 5 <[email protected]>
22d4dfb to
8a867eb
Compare
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||
… metadata already has a header key
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Good catch, and you're right — the meta key isn't a reliable signal for provenance. User-supplied meta defeating the guard is exactly the case I didn't test: I checked that the header survives the fallback paths, but never that the guard itself couldn't be fooled. Threading Pulled your commits and verified locally: your repro ( Thanks for taking it the rest of the way. |
Just out of curiosity, was this written by you or AI? |
|
AI-assisted. I use Claude Code for the implementation, and it drafted that comment; I direct the work and review before sending. That said, I do build locally and run the tests before opening anything — the case you found slipped through because every test I wrote built documents with empty meta, so the guard's own assumption never got exercised. |
Related Issues
Proposed Changes:
_apply_secondary_splittingstripped a leading header line from every chunk whenkeep_headers=False, matching any header level 1–6 viaself._header_patternand ignoringheader_split_levels.For chunks produced by an actual header split this is a no-op — their content begins right after the header line, so it starts with
\nandre.matchnever fires. The strip therefore only ever took effect on the two fallback paths of_split_text_by_markdown_headers("no headers found", "only headers with no content"), which return the whole document as a single chunk with empty meta. There the header line was cut from the content and, with noheaderkey in the meta to hold it, lost from the output entirely.The strip is now guarded on the chunk carrying
headermetadata, i.e. on it actually having come from a header split — which is precisely the case where removing the header line from the content is correct, because the metadata already holds it. Chunks returned by the fallback paths keep their content intact.This also covers documents whose only header sits at a level excluded from
header_split_levels.Before:
After:
'# Title\nsome content here'and'# Alpha\n# Beta'respectively.How did you test it?
Three unit tests added to
test/components/preprocessors/test_markdown_header_splitter.py, following the style of the neighbouringtest_secondary_split_keeps_content_before_*tests:test_secondary_split_keeps_header_of_headers_only_document— headers-only document keeps its header texttest_secondary_split_keeps_header_at_non_split_level— header at a level excluded fromheader_split_levelssurvivestest_secondary_split_still_strips_header_from_header_split_chunks— regression guard: withkeep_headers=False, real header-split chunks still keep the header only inmeta["header"]and not in the contentThe two bug tests fail on
mainand pass with the fix; the regression test passes both before and after, confirming existing behaviour is unchanged.Neighbouring preprocessor tests show no change against a clean-
mainbaseline (49 failed, 251 passed before → 49 failed, 254 passed after; the same pre-existing failures from missing offline model/optional dependencies in the local env, +3 from the new tests).ruff check,ruff format --checkandmypyare clean on the changed files.Notes for the reviewer
The alternative fix is deleting the strip outright, since it is dead code for header-split chunks. I kept it behind the
"header" in doc.metaguard instead, so the original intent — don't duplicate the header in the content when it is already in the metadata — stays explicit and keeps working if chunk content ever starts carrying its header line.A release note has been added at
releasenotes/notes/markdown-header-splitter-first-header-loss-d507cfc0fadea2f5.yaml.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.(AI-assisted: implemented with Claude Code.)