-
Notifications
You must be signed in to change notification settings - Fork 18.7k
fix(text-splitters): add validation to prevent infinite loop and prevent empty token splitter #32205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
with1015
wants to merge
7
commits into
langchain-ai:master
Choose a base branch
from
with1015:chore/text-splitter-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+25
−3
Open
fix(text-splitters): add validation to prevent infinite loop and prevent empty token splitter #32205
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
531d6b5
chore: add validation to prevent infinite loop and prevent empty toke…
with1015 283a729
chore: add unit test when decode returns empty string chunks
with1015 412dc32
chore: remove debugging out log
with1015 3c173bf
Merge branch 'master' into chore/text-splitter-validation
with1015 8590a7c
chore: add annotated type for list in unit test
with1015 e0a6dd7
Merge branch 'master' into chore/text-splitter-validation
mdrxy 50cdebc
Merge branch 'master' into chore/text-splitter-validation
mdrxy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -338,13 +338,20 @@ def split_text_on_tokens(*, text: str, tokenizer: Tokenizer) -> list[str]: | |||||
splits: list[str] = [] | ||||||
input_ids = tokenizer.encode(text) | ||||||
start_idx = 0 | ||||||
if tokenizer.tokens_per_chunk <= tokenizer.chunk_overlap: | ||||||
msg = "tokens_per_chunk must be greater than chunk_overlap" | ||||||
raise ValueError(msg) | ||||||
cur_idx = min(start_idx + tokenizer.tokens_per_chunk, len(input_ids)) | ||||||
chunk_ids = input_ids[start_idx:cur_idx] | ||||||
Comment on lines
348
to
349
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line duplicates the calculation from before the while loop. The redundant calculations of cur_idx and chunk_ids should be removed to improve code clarity.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
while start_idx < len(input_ids): | ||||||
splits.append(tokenizer.decode(chunk_ids)) | ||||||
cur_idx = min(start_idx + tokenizer.tokens_per_chunk, len(input_ids)) | ||||||
chunk_ids = input_ids[start_idx:cur_idx] | ||||||
if not chunk_ids: | ||||||
break | ||||||
decoded = tokenizer.decode(chunk_ids) | ||||||
if decoded: | ||||||
splits.append(decoded) | ||||||
if cur_idx == len(input_ids): | ||||||
break | ||||||
start_idx += tokenizer.tokens_per_chunk - tokenizer.chunk_overlap | ||||||
cur_idx = min(start_idx + tokenizer.tokens_per_chunk, len(input_ids)) | ||||||
chunk_ids = input_ids[start_idx:cur_idx] | ||||||
return splits |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This calculation is duplicated from line 348. Consider calculating cur_idx and chunk_ids once at the beginning of each loop iteration to avoid redundancy.
Copilot uses AI. Check for mistakes.