fix(text-splitters): prevent start_index=-1 in token-based splitters#35199
Open
Kyle Tse (shtse8) wants to merge 2 commits intolangchain-ai:masterfrom
Open
fix(text-splitters): prevent start_index=-1 in token-based splitters#35199Kyle Tse (shtse8) wants to merge 2 commits intolangchain-ai:masterfrom
Kyle Tse (shtse8) wants to merge 2 commits intolangchain-ai:masterfrom
Conversation
Fixes langchain-ai#29884 `TextSplitter.create_documents` calculates a character offset for `text.find()` using `self._chunk_overlap`. For token-based splitters like `TokenTextSplitter`, chunk_overlap is expressed in tokens rather than characters, so the offset can overshoot the actual position and `text.find()` returns -1. Add a fallback: when the initial `text.find()` misses (returns -1), retry from the beginning of the text. This ensures every chunk gets a valid `start_index` that correctly locates it within the source.
- Move TokenTextSplitter import to top-level imports
- Remove unused tiktoken variable assignment (use pytest.importorskip directly)
- Remove __import__('pytest') workaround (pytest already imported at top)
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.
Description
Fixes #29884
TextSplitter.create_documents()calculates a character offset fortext.find()usingself._chunk_overlap. For token-based splitters likeTokenTextSplitter,chunk_overlapis expressed in tokens rather than characters, so the offset can overshoot the actual chunk position andtext.find()returns-1.Root Cause
In
create_documents():previous_chunk_lenis in characters (fromlen(chunk)), butself._chunk_overlapis in tokens forTokenTextSplitter. The mismatch means the search window can start after where the chunk actually appears, causingtext.find()to miss and return-1.Fix
Add a fallback: when the initial
text.find()returns-1, retry the search from the beginning of the text:This is safe because chunks are always substrings of the source text, and searching from position 0 guarantees we find the first occurrence.
Tests
Added
test_token_text_splitter_start_index_no_negativewhich:TokenTextSplitterwithchunk_size=10, chunk_overlap=5, add_start_index=Truestart_indexvalues are>= 0start_indexcorrectly locates the chunk in the source text