Repository navigation
fix(backend): report tool_use stop_reason when tool calls finish with stop - #3120
Open
pcbeingused333 wants to merge 1 commit into
Open
pcbeingused333 wants to merge 1 commit into
pcbeingused333 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
1 issue found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/backend/src/routing/proxy/anthropic-messages-adapter.ts">
<violation number="1" location="packages/backend/src/routing/proxy/anthropic-messages-adapter.ts:653">
P2: Streaming still reports `end_turn` when the provider omits `finish_reason`. The surrounding `if (choice.finish_reason)` skips this call for absent or null values, and `closeStream` defaults to `end_turn`; derive the final reason from `state.toolCalls` when closing.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
|
|
||
| if (choice.finish_reason) { | ||
| state.stopReason = toAnthropicStopReason(choice.finish_reason); | ||
| state.stopReason = toAnthropicStopReason(choice.finish_reason, state.toolCalls.size > 0); |
There was a problem hiding this comment.
P2: Streaming still reports end_turn when the provider omits finish_reason. The surrounding if (choice.finish_reason) skips this call for absent or null values, and closeStream defaults to end_turn; derive the final reason from state.toolCalls when closing.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/backend/src/routing/proxy/anthropic-messages-adapter.ts, line 653:
<comment>Streaming still reports `end_turn` when the provider omits `finish_reason`. The surrounding `if (choice.finish_reason)` skips this call for absent or null values, and `closeStream` defaults to `end_turn`; derive the final reason from `state.toolCalls` when closing.</comment>
<file context>
@@ -641,7 +650,7 @@ function transformStreamChunk(chunk: string, state: StreamState): string | null
if (choice.finish_reason) {
- state.stopReason = toAnthropicStopReason(choice.finish_reason);
+ state.stopReason = toAnthropicStopReason(choice.finish_reason, state.toolCalls.size > 0);
if (isRecord(data.usage) && !state.finalUsage) {
state.finalUsage = toAnthropicUsage(data.usage);
</file context>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3120 +/- ##
==========================================
- Coverage 96.22% 96.21% -0.01%
==========================================
Files 674 674
Lines 39767 39771 +4
Branches 11108 11109 +1
==========================================
+ Hits 38264 38267 +3
- Misses 521 522 +1
Partials 982 982
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
This branch has not been deployed
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
On
/v1/messages, a response that contains tool calls but whose upstreamfinish_reasonisstop(or missing) is returned to the Anthropic client withstop_reason: "end_turn". Clients such as Claude Code treat that as a finished turn and never run the tools. Some OpenAI-compatible providers finish tool-calling turns this way. This is the same class of mismatch as the finish/tool_calls mapping fixed in the other adapters; the Google adapter already handles it withhasToolCalls.Root cause
toAnthropicStopReason(finish_reason)is called inchatCompletionsResponseToMessagesand in the streaming transformer without looking at whether tool calls were emitted.Fix
toAnthropicStopReasontakes ahasToolCallsflag. When tool calls are present and the finish reason isstopor absent, it returnstool_use. Other reasons (length,content_filter, ...) are unchanged, so a truncated turn still reportsmax_tokens. Non-streaming passes whether atool_useblock was built; streaming passes whether any tool call was seen. A patch changeset is included.Test
Two new cases in
anthropic-messages-adapter.spec.ts(non-streaming and streaming). Before the fix they fail; after it the suite passes (61 tests).This change was prepared with the assistance of an AI coding agent (Claude) and reviewed before submission.
Summary by cubic
Fixes
/v1/messagesreportingstop_reason: "end_turn"when an upstream provider returns tool calls with astop(or missing) finish reason, so clients like Claude Code run the tools instead of ending the turn.Bug Fixes
toAnthropicStopReasonnow returnstool_usewhen tool calls are present with astopor missing reason; other reasons likelengthstill map as before.Written for commit fcbac6a. Summary will update on new commits.