Repository navigation
fix: stop non-streaming generation at the first stop sequence (+ contract tests) - #228
Merged
Merged
Conversation
The streaming handlers end generation when a stop sequence matches. The two non-streaming handlers (chat and text) kept generating to EOS or max_tokens and trimmed the text afterwards, so the same request spent more compute and reported a larger completion_tokens when it was not streamed. Contract tests: finish_reason is "length" on the chat and streaming-text paths (Test 11), and completion_tokens after a stop sequence agree between streaming and non-streaming for chat (Test 12) and drop for non-streaming completions (Test 12b). Tests 12 and 12b fail on main (38 vs 14 and 60 vs 60 tokens). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This was referenced Oct 11, 2026
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.
Part of #128 (Tier 2, contract tests). Found while auditing
finish_reason/usageacross the four response paths after #227.Problem
The streaming handlers end generation as soon as a stop sequence matches.
handleChatNonStreamingandhandleTextNonStreamingdid not: they generated to EOS ormax_tokensand trimmed the text afterwards. The same request therefore spent more compute and reported a largercompletion_tokenswhen it was not streamed.Change
Both non-streaming loops now stop at the first stop sequence, using the same windowed
checkStopSequencesscan as the streaming paths..infodoes not arrive on that exit, socompletion_tokensis the chunk count, the same trade-off the streaming path already documents.Tests (
tests/test-contract.sh)finish_reasonislengthwhenmax_tokensends a chat completion (non-streaming and streaming) and a streamed text completion. These paths were already correct; this guards them. The non-streaming/v1/completionscase is left to Non-streaming/v1/completionsreportsfinish_reason: "stop"whenmax_tokenswas hit #227.completion_tokensafter a stop sequence agree between streaming and non-streaming./v1/completions, a stop sequence reducescompletion_tokens.Both 12 and 12b first check that the unconstrained reply contains the stop word, and report SKIP otherwise, so they test the server and not the model.
Verification
Qwen2.5-0.5B-Instruct-4bit, release build, Xcode 26.3 (Swift 6.2.4):
main(7e219b4)The 2 skips are the existing tool-call and JSON-completeness checks, which the tiny model cannot satisfy. Unit suite: 435 tests, 9 skipped, 0 failures.
Not changed
Non-streaming chat reports
tool_callsoverlengthwhen a tool call was parsed, while the streaming path reportslength. Which should win is a behaviour decision, so it is left alone here.🤖 Generated with Claude Code