fix: report finish_reason "length" when output hits the token limit#10
Merged
Merged
Conversation
Both the streaming and non-streaming chat-completion paths hardcoded finish_reason to "tool_calls" or "stop", so a response truncated at max_tokens was reported as a natural "stop". OpenAI clients rely on finish_reason to decide whether to continue a cut-off response. Add a finishReason helper: "length" when the generated token count reaches the requested limit, "tool_calls" when the model emitted tool calls, otherwise "stop". Used by both completion paths. Fixes defilantech#9 Signed-off-by: Christopher Maher <chris@mahercode.io>
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.
What
Chat completions truncated at
max_tokensnow reportfinish_reason: "length"instead of"stop".Why
Both the streaming and non-streaming paths in
ChatCompletion.swifthardcodedfinish_reasonto"tool_calls"or"stop". A response cut off at the token limit was reported as a natural"stop". OpenAI clients usefinish_reasonto decide whether to continue a truncated response, so this misled them. Found dogfooding the LLMKube metal-agent mlx-server runtime: three runs each generated exactlymax_tokens(512) tokens, all reporting"stop".How
A
finishReasonhelper returns"tool_calls"when the model emitted tool calls,"length"when the generated token count reached the requested limit, otherwise"stop". Truncation is inferred by comparinggenerationTokenCountagainstparameters.maxTokens, since the generator does not surface a stop reason directly. Both completion paths use it. Unit-tested.Fixes #9