fix: correct off-by-one in is_partial_stop causing false positives#3791
Open
alvinttang wants to merge 1 commit intolm-sys:mainfrom
Open
fix: correct off-by-one in is_partial_stop causing false positives#3791alvinttang wants to merge 1 commit intolm-sys:mainfrom
alvinttang wants to merge 1 commit intolm-sys:mainfrom
Conversation
The is_partial_stop() function checks whether the end of the output contains the beginning of a stop string. However, the loop range starts at i=0, where output[-0:] in Python returns the entire string. This means stop_str.startswith(output) is evaluated, which incorrectly returns True whenever the full output happens to be a prefix of the stop string — even when no actual partial stop is present at the end. Additionally, the upper bound was exclusive at min(len(output), len(stop_str)), missing the check for the largest valid suffix length. Fix the range to start at 1 (smallest meaningful suffix) and end at min(len(output), len(stop_str)) + 1 (inclusive of the largest valid suffix). This prevents false positive partial-stop detection that would suppress token streaming output.
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
is_partial_stop()infastchat/utils.pyhas an off-by-one bug in its loop range that causes two problems:i=0,output[-0:]in Python evaluates to the entire string (not empty). This meansstop_str.startswith(output)is checked, which returnsTruewhenever the full output text happens to be a prefix of the stop string — even when no partial stop is actually present at the end of the output.min(len(output), len(stop_str))is exclusive, so the largest meaningful suffix is never checked.The fix changes the range from
range(0, min(...))torange(1, min(...) + 1), which correctly checks suffixes of length 1 throughmin(len(output), len(stop_str)).Impact
This bug affects the streaming output in
generate_stream()(infastchat/serve/inference.py). When the output is short and happens to be a prefix of a stop string,is_partial_stopfalsely returnsTrue, which suppresses yielding that streaming chunk to the client. This can cause delayed or missing intermediate streaming tokens.Example
Test plan
is_partial_stopexist beyondinference.py🤖 Generated with Claude Code