Skip to content

[anthropic] fix issue #1370 with tool call duplication #2670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mmazurkevich
Copy link

Without this fix during the stream event handling when EventType.MESSAGE_STOP occurs, the latest content block was resent again and it caused to the additional tool call(if it was the latest event)
Also I've described the root cause in the comment to the issue

@mmazurkevich mmazurkevich force-pushed the mmazurkevich/gh-1370-pr branch from de87a81 to 15dfed8 Compare April 8, 2025 12:08
@ilayaperumalg ilayaperumalg self-assigned this Apr 8, 2025
@ilayaperumalg ilayaperumalg added bug Something isn't working anthropic labels Apr 8, 2025
@ilayaperumalg ilayaperumalg added this to the 1.0.0-M7 milestone Apr 8, 2025
@ilayaperumalg
Copy link
Member

@mmazurkevich Thanks for debugging and submitting the PR to fix the issue.

Without this fix during the stream event handling when `EventType.MESSAGE_STOP` occurs, the latest content block was resent again and it caused to the additional tool call(if it was the latest event)

Signed-off-by: Mikhail Mazurkevich <[email protected]>
@mmazurkevich mmazurkevich force-pushed the mmazurkevich/gh-1370-pr branch from 15dfed8 to f3adcae Compare April 8, 2025 14:38
@mmazurkevich
Copy link
Author

@ilayaperumalg I've updated the formatting and force-pushed the changes because of the failed GH Action

@ilayaperumalg
Copy link
Member

@mmazurkevich With this change, some of the stream+toolcalling tests are failing:

AnthropicChatClientIT#streamFunctionCallTest
AnthropicChatModelIT#streamFunctionCallTest
AnthropicChatModelIT#streamFunctionCallUsageTest

Could you check?

@ilayaperumalg ilayaperumalg removed this from the 1.0.0-M7 milestone Apr 10, 2025
… the original request

Previously, internalStream used switchMap to process ChatCompletionResponses,
which caused the active stream (including potential recursive calls) to be
canceled whenever a new response arrived. This led to incomplete processing
of streaming tool calls and unexpected behavior when handling tool_use events.

Replaced switchMap with flatMap to ensure that each response is fully processed
without being interrupted, allowing recursive internalStream calls to complete
as expected.

Without this fix during the stream event handling when `EventType.MESSAGE_STOP` occurs, the latest content block was resent again and it caused to the additional tool call(if it was the latest event)

Signed-off-by: Mikhail Mazurkevich <[email protected]>
@mmazurkevich
Copy link
Author

@ilayaperumalg thank you for the ping. The failed test highlighted the problem that was in the code even before the change. Previously, internalStream used switchMap to process ChatCompletionResponses, which caused the active stream (including potential recursive calls) to be canceled whenever a new response arrived. This led to incomplete processing of streaming tool calls and unexpected behavior when handling tool_use events.

The fix has been pushed. Also, I've run all 50 tests locally with my own ANTHROPIC_API_KEY looks like all of them passed

@ilayaperumalg
Copy link
Member

@mmazurkevich Thank you so much for the quick response, fixing the issue and verifying all the tests! I will get this merged soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
anthropic bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants