Skip to content

fix: avoid whitespace-only and over-long chunks in RecursiveDocumentSplitter - #13693

Merged
bogdankostic merged 2 commits into
deepset-ai:mainfrom
MaiAditya:fix/recursive-splitter-whitespace-chunks
Dec 2, 2026
Merged

bogdankostic merged 2 commits into
deepset-ai:mainfrom
MaiAditya:fix/recursive-splitter-whitespace-chunks

Conversation

@MaiAditya

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

  • After _chunk_text and before overlap is applied, a whitespace-only chunk is joined to a neighbouring chunk when the result stays within split_length. In word mode whitespace counts as zero words, so it always joins. In char/token mode a whitespace run longer than split_length can still form a chunk of its own, since joining it would exceed split_length. Nothing is dropped: the chunks still join back to the input, and split_idx_start, page_number and _split_overlap are computed on the joined chunks.
  • With the "sentence" separator, the last sentence split now runs to the end of the text, so the grouping accounts for its trailing whitespace. This replaces the trailing_text append from feat!: align page_number metadata across splitters and fix drift in overlapping splits #13006, which added the whitespace after grouping and could produce a chunk of 16 characters for split_length=14 ("eta zeta? iota\n\n"). The trailing whitespace is still kept.

How did you test it?

  • New exact-content tests (word, char, token) for a text with a run of 24 newlines, checking contents, split_id, split_idx_start, and that the chunks join back to the input; and a test for the sentence-separator case. All fail on main.
  • test_run_multiple_separators_with_overlap_applies_overlap_only_once now expects 5 chunks instead of 6: on main the extra chunk was a lone "\n" after the overlap, which is now joined to the previous chunk.
  • pytest test/components/preprocessors/test_recursive_splitter.py: 87 passed (incl. 14 integration). ruff check/format and mypy on the module pass.
  • I also compared main and this branch over ~3,000 splitter runs (random texts with newline/tab/form-feed runs, word/char units, several separator sets, overlap 0/1): joined chunks equal the input in every run without overlap, whitespace-only documents are unchanged, and no run produced more over-length chunks than main.

Notes for the reviewer

  • Chunk boundaries and counts change for text with whitespace runs, so the release note has an upgrade entry suggesting re-splitting and re-indexing.
  • The join happens once on the final chunk list (not inside the recursion), similar to how overlap is applied once.

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 disclosure: I used Claude Code (Anthropic) to trace the splitter logic, write the comparison script, and draft the tests and this description. I reviewed the change and ran the tests above.

…plitter

Text with runs of blank lines, common in PDF extractions, produced chunks of
only whitespace in word, char and token mode. A whitespace-only chunk is now
joined to a neighbouring chunk when the result fits split_length, which in
word mode is always the case. The chunks still join back to the input text.
In char and token mode a whitespace run longer than split_length can still
form a chunk of its own, since joining it would exceed split_length.

With the "sentence" separator, the whitespace after the last sentence was
appended after grouping, which could push a char/token chunk past
split_length. The last sentence split now runs to the end of the text so the
grouping accounts for it.
@MaiAditya
MaiAditya requested a review from a team as a code owner November 11, 2026 14:17
@MaiAditya
MaiAditya requested review from bogdankostic and removed request for a team November 11, 2026 14:17
@vercel

vercel Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

@MaiAditya is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Nov 11, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Nov 11, 2026
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/preprocessors
  recursive_splitter.py
  haystack/core/pipeline
  pipeline.py
Project Total  

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

@MaiAditya

Copy link
Copy Markdown
Contributor Author

Gentle ping on this one when you have a moment. CI is green and the branch merges cleanly; happy to adjust anything.

@bogdankostic bogdankostic 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, thanks @MaiAditya!

@bogdankostic
bogdankostic enabled auto-merge (squash) December 2, 2026 13:26
@bogdankostic
bogdankostic merged commit 7a8ad41 into deepset-ai:main Dec 2, 2026
24 of 25 checks passed
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.

RecursiveDocumentSplitter returns whitespace-only chunks for text with runs of blank lines

3 participants