Skip to content

fix: stop non-streaming generation at the first stop sequence (+ contract tests) - #228

Merged
solderzzc merged 1 commit into
mainfrom
test/contract-stream-parity
Oct 11, 2026
Merged

solderzzc merged 1 commit into
mainfrom
test/contract-stream-parity

Conversation

@solderzzc

Copy link
Copy Markdown
Member

Part of #128 (Tier 2, contract tests). Found while auditing finish_reason / usage across the four response paths after #227.

Problem

The streaming handlers end generation as soon as a stop sequence matches. handleChatNonStreaming and handleTextNonStreaming did not: they generated to EOS or max_tokens and trimmed the text afterwards. The same request therefore spent more compute and reported a larger completion_tokens when it was not streamed.

Change

Both non-streaming loops now stop at the first stop sequence, using the same windowed checkStopSequences scan as the streaming paths. .info does not arrive on that exit, so completion_tokens is the chunk count, the same trade-off the streaming path already documents.

Tests (tests/test-contract.sh)

  • Test 11: finish_reason is length when max_tokens ends a chat completion (non-streaming and streaming) and a streamed text completion. These paths were already correct; this guards them. The non-streaming /v1/completions case is left to Non-streaming /v1/completions reports finish_reason: "stop" when max_tokens was hit #227.
  • Test 12: for chat, completion_tokens after a stop sequence agree between streaming and non-streaming.
  • Test 12b: for non-streaming /v1/completions, a stop sequence reduces completion_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):

Test 12 Test 12b Total
main (7e219b4) FAIL, 38 vs 14 tokens FAIL, 60 vs 60 tokens 17 passed, 2 failed, 2 skipped
this branch pass, 14 vs 14 pass, 29 vs 60 19 passed, 0 failed, 2 skipped

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_calls over length when a tool call was parsed, while the streaming path reports length. Which should win is a behaviour decision, so it is left alone here.

🤖 Generated with Claude Code

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>
@solderzzc
solderzzc merged commit efe75d3 into main Oct 11, 2026
15 checks passed
@solderzzc
solderzzc deleted the test/contract-stream-parity branch October 11, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant