Do not count whitespace runs as words in RecursiveDocumentSplitter fallback - #12139
Open
TimurRakhmatullin86 wants to merge 2 commits into
Open
Conversation
TimurRakhmatullin86
requested review from
davidsbatista
and removed request for
a team
July 23, 2026 16:41
|
@TimurRakhmatullin86 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
TimurRakhmatullin86
force-pushed
the
fix/recursive-splitter-whitespace-word-count
branch
from
July 23, 2026 22:02
5d735bb to
c1b243e
Compare
…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()).
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.
Summary
When
RecursiveDocumentSplitterfalls back to word-based splitting, the fallback countsconsecutive whitespace runs (
" ","\t","\f", page breaks, …) as words. As a result:split_length(whitespace "words" inflate the count), andRoot 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 singlespace, 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 thecount. Two existing tests asserted the old (buggy) behavior — splitting a
\f\fpage break intoits own mid-sentence chunk and counting it toward
split_length— and are updated to the correctexpectation.
Added a release note under
releasenotes/notes/.