Skip to content

fix: MarkdownHeaderSplitter drops the leading header of an unsplit document - #12478

Merged
davidsbatista merged 6 commits into
deepset-ai:mainfrom
Vedant-Agarwal:fix/markdown-header-splitter-first-header-loss
Aug 31, 2026
Merged

davidsbatista merged 6 commits into
deepset-ai:mainfrom
Vedant-Agarwal:fix/markdown-header-splitter-first-header-loss

Conversation

@Vedant-Agarwal

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

_apply_secondary_splitting stripped a leading header line from every chunk when keep_headers=False, matching any header level 1–6 via self._header_pattern and ignoring header_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 \n and re.match never 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 no header key in the meta to hold it, lost from the output entirely.

The strip is now guarded on the chunk carrying header metadata, 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:

s = MarkdownHeaderSplitter(
    keep_headers=False, header_split_levels=[2], secondary_split="word", split_length=5
)
s.run(documents=[Document(content="# Title\nsome content here")])["documents"]
# [Document(content='\nsome content here', meta={... no 'header' key ...})]   -> "Title" lost

s = MarkdownHeaderSplitter(keep_headers=False, secondary_split="word", split_length=5)
s.run(documents=[Document(content="# Alpha\n# Beta")])["documents"]
# [Document(content='\n# Beta', ...)]                                          -> "# Alpha" lost

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 neighbouring test_secondary_split_keeps_content_before_* tests:

  • test_secondary_split_keeps_header_of_headers_only_document — headers-only document keeps its header text
  • test_secondary_split_keeps_header_at_non_split_level — header at a level excluded from header_split_levels survives
  • test_secondary_split_still_strips_header_from_header_split_chunks — regression guard: with keep_headers=False, real header-split chunks still keep the header only in meta["header"] and not in the content

The two bug tests fail on main and pass with the fix; the regression test passes both before and after, confirming existing behaviour is unchanged.

pytest test/components/preprocessors/test_markdown_header_splitter.py
42 passed

Neighbouring preprocessor tests show no change against a clean-main baseline (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 --check and mypy are 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.meta guard 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

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

(AI-assisted: implemented with Claude Code.)

@Vedant-Agarwal
Vedant-Agarwal requested a review from a team as a code owner August 26, 2026 21:19
@Vedant-Agarwal
Vedant-Agarwal requested review from davidsbatista and removed request for a team August 26, 2026 21:19
@vercel

vercel Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

@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]>
@Vedant-Agarwal
Vedant-Agarwal force-pushed the fix/markdown-header-splitter-first-header-loss branch from 22d4dfb to 8a867eb Compare August 26, 2026 21:20
@github-actions

github-actions Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/preprocessors
  markdown_header_splitter.py 235-236, 249, 325
  haystack/core/pipeline
  pipeline.py
Project Total  

This report was generated by python-coverage-comment-action

@github-actions github-actions Bot mentioned this pull request Aug 27, 2026
3 tasks done
@github-actions github-actions Bot added the type:documentation Improvements on the docs label Aug 28, 2026
@davidsbatista

Copy link
Copy Markdown
Contributor
  • The guard not self.keep_headers and "header" in doc.meta uses "does doc.meta have a header key" as a stand-in for "did this chunk come from a real split." That's not reliable.

  • So a document whose input metadata already has a header key fools the check. Its real leading header gets stripped again — the same bug this PR was meant to fix.

  • Verified it: Document(content="# Alpha\n# Beta", meta={"header": "preexisting"}) loses "Alpha" in the output.

  • Fixed : _split_documents_by_markdown_headers now returns each Document paired with from_header_split: bool, set at chunk-build time. _apply_secondary_splitting checks that instead of the meta key.

  • Added a regression test for it. Confirmed it fails on the original guard, passes with the fix. Full suite, mypy, and ruff all clean.

  • Updated release notes

@vercel

vercel Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
haystack-docs Ready Ready Preview Aug 31, 2026 9:52am

Request Review

@Vedant-Agarwal

Copy link
Copy Markdown
Contributor Author

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 from_header_split through from the split is the correct fix — structural rather than inferred.

Pulled your commits and verified locally: your repro (meta={"header": "preexisting"}) keeps "Alpha" now, full test file passes (43), mypy clean.

Thanks for taking it the rest of the way.

@davidsbatista

Copy link
Copy Markdown
Contributor

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 from_header_split through from the split is the correct fix — structural rather than inferred.

Pulled your commits and verified locally: your repro (meta={"header": "preexisting"}) keeps "Alpha" now, full test file passes (43), mypy clean.

Thanks for taking it the rest of the way.

Just out of curiosity, was this written by you or AI?

@Vedant-Agarwal

Copy link
Copy Markdown
Contributor Author

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.

linhongyu510

This comment was marked as spam.

@davidsbatista davidsbatista left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

@davidsbatista
davidsbatista merged commit b7e1dd8 into deepset-ai:main Aug 31, 2026
24 checks passed

This branch was successfully deployed

1 active deployment
Preview — afe3ccb3 Deployed Aug 31, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MarkdownHeaderSplitter loses the leading header when a secondary split runs on an unsplit document

3 participants