Skip to content

Do not count whitespace runs as words in RecursiveDocumentSplitter fallback - #12139

Open
TimurRakhmatullin86 wants to merge 2 commits into
deepset-ai:mainfrom
TimurRakhmatullin86:fix/recursive-splitter-whitespace-word-count
Open

Do not count whitespace runs as words in RecursiveDocumentSplitter fallback#12139
TimurRakhmatullin86 wants to merge 2 commits into
deepset-ai:mainfrom
TimurRakhmatullin86:fix/recursive-splitter-whitespace-word-count

Conversation

@TimurRakhmatullin86

Copy link
Copy Markdown
Contributor

Summary

When RecursiveDocumentSplitter falls back to word-based splitting, the fallback counts
consecutive whitespace runs (" ", "\t", "\f", page breaks, …) as words. As a result:

  • chunks end up shorter than the requested split_length (whitespace "words" inflate the count), and
  • pure-whitespace chunks can be emitted.

Root cause

The fallback iterates the split pieces and increments the word count for every non-empty piece,
but a run of whitespace is a non-empty piece. The guard if word != " ": only skips a single
space, not tabs, form feeds, or multi-space runs.

Fix

Skip any piece that is whitespace-only (if word.strip():) so only real words contribute to the
count. Two existing tests asserted the old (buggy) behavior — splitting a \f\f page break into
its own mid-sentence chunk and counting it toward split_length — and are updated to the correct
expectation.

Added a release note under releasenotes/notes/.

@TimurRakhmatullin86
TimurRakhmatullin86 requested a review from a team as a code owner July 23, 2026 16:41
@TimurRakhmatullin86
TimurRakhmatullin86 requested review from davidsbatista and removed request for a team July 23, 2026 16:41
@vercel

vercel Bot commented Jul 23, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

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

github-actions Bot commented Jul 23, 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
Project Total  

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

@TimurRakhmatullin86
TimurRakhmatullin86 force-pushed the fix/recursive-splitter-whitespace-word-count branch from 5d735bb to c1b243e Compare July 23, 2026 22:02
TimurRakhmatullin86 and others added 2 commits July 23, 2026 15:03
…llback

The word-mode fixed-size fallback split words with re.findall(r"\S+|\s+", text),
which also yields multi-character whitespace tokens ("  ", "\t", "\f"). The loop
counted every token that was not exactly a single space toward the chunk length,
so a multi-space or page-break token was counted as a word - producing chunks
shorter than split_length and, for a whitespace-only document, a whitespace-only
chunk. Count a token only when it contains a real word (word.strip()).
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.

2 participants