Skip to content

fix(backend): report tool_use stop_reason when tool calls finish with stop - #3120

Open
pcbeingused333 wants to merge 1 commit into
mnfst:mainfrom
pcbeingused333:fix/anthropic-stop-reason-tool-calls
Open

pcbeingused333 wants to merge 1 commit into
mnfst:mainfrom
pcbeingused333:fix/anthropic-stop-reason-tool-calls

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On /v1/messages, a response that contains tool calls but whose upstream finish_reason is stop (or missing) is returned to the Anthropic client with stop_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 with hasToolCalls.

Root cause

toAnthropicStopReason(finish_reason) is called in chatCompletionsResponseToMessages and in the streaming transformer without looking at whether tool calls were emitted.

Fix

toAnthropicStopReason takes a hasToolCalls flag. When tool calls are present and the finish reason is stop or absent, it returns tool_use. Other reasons (length, content_filter, ...) are unchanged, so a truncated turn still reports max_tokens. Non-streaming passes whether a tool_use block 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/messages reporting stop_reason: "end_turn" when an upstream provider returns tool calls with a stop (or missing) finish reason, so clients like Claude Code run the tools instead of ending the turn.

Bug Fixes

  • toAnthropicStopReason now returns tool_use when tool calls are present with a stop or missing reason; other reasons like length still map as before.
  • Applies to both non-streaming and streaming responses.
  • Adds coverage for both paths and a patch changeset.

Written for commit fcbac6a. Summary will update on new commits.

View guided diff

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 96.21%. Comparing base (e10162d) to head (fcbac6a).

Additional details and impacted files

Impacted file tree graph

@@            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              
Flag Coverage Δ
backend 95.22% <83.33%> (-0.01%) ⬇️
frontend 97.44% <ø> (ø)
shared 98.78% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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