Fix whitespace handling in the sentence splitters - #5
Merged
IvoLeist merged 1 commit intoAug 24, 2026
Conversation
…itters Two bugs silently changed the user's text. spaCy with "strip whitespace" deleted the space between sentences. langchain strips every sentence before joining them and we join with an empty separator, so "mat. The" became "mat.The". The chunk then no longer occurred in the input, so its start index was reported as null. spaCy is now built with strip_whitespace=False and the stripping is done on the finished chunk, which is what the option promises and keeps the chunk a slice of the input. Whitespace-only chunks were dropped for every splitter. A run of blank lines is content for the token and character splitters, and dropping it renumbered the chunks that followed, so 64 characters of a chapter break disappeared. They are now only dropped when stripping was asked for and nothing is left. Also: - the spaCy input limit is set per pipeline; one limit of 10 million characters allowed about 44 GB with the English model - the "text does not occur in the input" warning no longer claims token splitting is the only cause - the NLTK dropped-text check uses the chunk's start index instead of rfind(), which found the last occurrence and so reported no loss when the dropped text repeated the final chunk - strip_whitespace is reported once from the argument instead of being hardcoded per splitter - the punkt_tab notes match the nltk_data requirement - test 9 pins the NLTK warning instead of failing on purpose - new test on space separated prose; every other sentence fixture separates with a line break, which is why this was never caught
IvoLeist
merged commit Aug 24, 2026
850231d
into
IvoLeist:add-langchain_text_splitters
10 checks passed
5 tasks
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.
bgruening#1947
Fixes two bugs in the sentence splitters that silently changed the user's text, and the failing test.
spaCy + "strip whitespace" removed the space between sentences
langchain strips each sentence before joining, and we join with an empty separator, so the space is gone:
The chunk then does not occur in the input any more, so
start_indexbecame null and the warning blamed a UTF-8 token problem that was not there.Test 13 passed because
sentence_spacy.txtseparates sentences with\n, which spaCy attaches to the next sentence. I added a fixture with space separated sentences, which is the normal case for prose.Fix: build the splitter with
strip_whitespace=Falseand strip the finished chunk instead. That is what the option's label already promises ("Whitespace within chunks is preserved"), and the chunk stays a slice of the input, so the start index stays valid.Whitespace-only chunks were dropped for every splitter
With the token splitter on a chapter break, 64 characters disappeared and the remaining chunks were renumbered. They are now only dropped when stripping was asked for and nothing is left.
The spaCy input limit was one number for two pipelines
10 million characters allows about 44 GB with the English model, which needs about 4 kB per character. The limit is now per pipeline: 2 million for the model, 20 million for the sentencizer, which needs about 0.2 kB per character.
About the trailing newline TODO
Test 9 is correct as it is. The dropped character is the
\nGalaxy appends, and punkt reports sentence spans with trailing whitespace trimmed, so it falls outside the last span. I flipped the assertion to expect the warning, so the behaviour is pinned instead of red.removesuffix("\n")would fix it in the wrong place. An uploadedabcand an uploadedabc\nboth arrive asabc\n, so we cannot tell them apart, and we would cut a real character off every input that already ends in a newline. It would also not settle NLTK, because punkt drops all trailing whitespace, so a file ending in a blank line still loses more.Adding a newline to the test files does not silence the warning either, I tested it. It is still worth doing so the file on disk matches what Galaxy uploads, but it changes no result.
Also
punkt_tabnotes in the code and the help now match thenltk_datarequirement; both still said the data had to be installed by handstrip_whitespaceis reported once from the argument instead of being hardcoded per splitter, so it cannot reportfalsewhile stripping28/28 tests pass. I also ran seven parameter combinations against a live Galaxy, including three the test suite does not cover.