-
Couldn't load subscription status.
- Fork 6.5k
feat: integrate anthropic with tool call block #20100
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
Conversation
| assert "input" in tool_call | ||
| assert "name" in tool_call | ||
|
|
||
| if (tool_call["id"], tool_call["name"]) not in unique_tool_calls: |
There was a problem hiding this comment.
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
| 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 |
There was a problem hiding this comment.
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)
There was a problem hiding this 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!
Add support for
ToolCallBlockin Anthropic (tested e2e).