Skip to content

Conversation

@AstraBert
Copy link
Member

Add support for ToolCallBlock in Anthropic (tested e2e).

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Oct 16, 2025
assert "input" in tool_call
assert "name" in tool_call

if (tool_call["id"], tool_call["name"]) not in unique_tool_calls:
Copy link
Member Author

Choose a reason for hiding this comment

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

Added this check here to avoid duplicates

Comment on lines +430 to +454
def update_tool_calls(blocks: list[ContentBlock], tool_call: ToolCallBlock) -> None:
if len([block for block in blocks if isinstance(block, ToolCallBlock)]) == 0:
blocks.append(tool_call)
return
elif not any(
block.tool_call_id == tool_call.tool_call_id
for block in blocks
if isinstance(block, ToolCallBlock)
):
blocks.append(tool_call)
return
elif any(
block.tool_call_id == tool_call.tool_call_id
and block.tool_kwargs == tool_call.tool_kwargs
for block in blocks
if isinstance(block, ToolCallBlock)
):
return
else:
for i, block in enumerate(blocks):
if isinstance(block, ToolCallBlock):
if block.tool_call_id == tool_call.tool_call_id:
blocks[i] = tool_call
break
return
Copy link
Member Author

Choose a reason for hiding this comment

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

Added this logic because, since Anthropic streams partial JSON, tool calls with the same ID are streamed, but they might have different tool_kwargs. So:

  • We append the tool call if no other tool call is present/there is not tool call with the same ID
  • We skip if there is already a tool call with the same ID and arguments
  • We update the tool call if there is one with the same ID but it has different arguments

I added a test for this and I also tested end-to-end with the agent script, and now everything goes smooth (no duplicate calls, correct arguments parsing)

Copy link
Collaborator

@logan-markewich logan-markewich left a comment

Choose a reason for hiding this comment

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

This works well for me, thanks!

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Oct 27, 2025
@logan-markewich logan-markewich merged commit d2c766b into main Oct 27, 2025
10 of 11 checks passed
@logan-markewich logan-markewich deleted the clelia/anthropic-tool-call-block branch October 27, 2025 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants