Repository navigation
fix: report finish_reason "length" from non-streaming /v1/completions - #229
Merged
solderzzc merged 2 commits intoOct 11, 2026
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>
handleTextNonStreaming took generationTokenCount from the final .info event but ignored info.stopReason and hardcoded finish_reason to "stop", so a completion cut off by max_tokens looked like a natural end. It now uses the same mapping as handleChatNonStreaming; the streaming text path already had it. A client stop sequence still reports "stop" whatever ended generation. Contract Test 11 gains the non-streaming /v1/completions case it listed as pending, and TextCompletionFinishReasonTests drives the handler with a synthetic stream. Closes SharpAI#227.
Member
|
Thanks @HamedShams — reviewed. The mapping matches |
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.
Closes #227. Stacked on #228, which rewrote the same loop; the first commit here is #228's and the diff collapses to the second once it lands.
What
/v1/completionswithoutstreamnow reportsfinish_reason: "length"when generation hitmax_tokens. The handler readinfo.generationTokenCountfrom the final.infoevent but ignoredinfo.stopReason, sofinish_reasonwas hardcoded to"stop". It now uses the same mapping ashandleChatNonStreaming; the streaming text path already had it.A stop sequence still wins: if the text was cut at a client stop string, the answer is
"stop"regardless of why generation ended, which is the case you flagged in review. With #228's early exit the.infoevent never arrives on that path, so the reason stays at its.stopdefault, and the post-loop trim forces"stop"as well.Why
finish_reasonis how a client tells a truncated answer from a finished one. With this path always saying"stop", a completion that ran out of budget looked complete. Repro and line references in #227.Verified
TextCompletionFinishReasonTests(new) driveshandleTextNonStreamingwith a syntheticGenerationstream, no model needed:.length→"length",.stop→"stop", a client stop sequence →"stop"with the text trimmed (even when.infosays.length), andusagestill taken from the.infoevent. The first case is red on fix: stop non-streaming generation at the first stop sequence (+ contract tests) #228's branch without this change./v1/completionsatmax_tokens→"length". Red on main, green here.swift test --filter SwiftLMTests: 312 tests green (M1 Max, Xcode 27 / Swift 6.4).tests/test-contract.shagainst the CI model (mlx-community/Qwen2.5-0.5B-Instruct-4bit): 20 passed, 0 failed, 2 skipped (the script's tool-call / JSON capability skips), with fix: stop non-streaming generation at the first stop sequence (+ contract tests) #228's Tests 11, 12 and 12b all green.