Skip to content

Conversation

Amnah199
Copy link
Contributor

@Amnah199 Amnah199 commented Jul 18, 2025

Related Issues

Proposed Changes:

adopt the new StreamingChunk and stream also tool calls
pass component info to StreamingChunk

How did you test it?

Updated the tests
Added new tests for streaming chunks

Notes for the reviewer

Checklist

@github-actions github-actions bot added integration:anthropic type:documentation Improvements or additions to documentation labels Jul 18, 2025
@Amnah199 Amnah199 marked this pull request as ready for review July 23, 2025 13:25
@Amnah199 Amnah199 requested a review from a team as a code owner July 23, 2025 13:25
@Amnah199 Amnah199 requested review from davidsbatista and removed request for a team July 23, 2025 13:25
@@ -455,6 +570,12 @@ def test_convert_streaming_chunks_to_chat_message(self):
"index": 1,
"content_block": {"type": "tool_use", "id": "toolu_123", "name": "weather", "input": {}},
},
component_info=ComponentInfo.from_component(self),
index=1,
tool_calls=[ToolCallDelta(index=1, id="toolu_123", tool_name="weather", arguments=None)],
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we talked about this, but this should be index=0 since it's the first and only tool call in this chunk.

Suggested change
tool_calls=[ToolCallDelta(index=1, id="toolu_123", tool_name="weather", arguments=None)],
tool_calls=[ToolCallDelta(index=0, id="toolu_123", tool_name="weather", arguments=None)],

Copy link
Contributor

Choose a reason for hiding this comment

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

Same goes for the rest of the chunks in this test.

Copy link
Contributor Author

@Amnah199 Amnah199 Aug 4, 2025

Choose a reason for hiding this comment

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

Sorry if its already discussed.
Anthropic response chunks iteratively increase the index for each content block regardless of text and tool calls. So here index=1 is because its the second content block in stream that has tool call.
See the raw response here
To achieve what you suggest we will have to handle this on our end by separately keeping track of tool calls indexes.
WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

So here index=1 is because its the second content block in stream that has tool call.
Reference

Right! So the overall index of the StreamingChunk should be 1 which you have already. So StreamingChunk.index == 1, but you're right we should then separately keep track of the tool call indexes. Remember though the tool call index is only relevant for the number of ToolCallDeltas present in a single StreamingChunk. So if there is only ever one ToolCallDelta in a StreamingChunk then the tool call index will always be 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, so a single StreamingChunk always has a single ToolCallDelta as each InputJSONDelta is passed as a separate RawContentBlockDeltaEvent.
So maybe we can always assign tool call index = 0.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good!

Copy link
Contributor Author

@Amnah199 Amnah199 Aug 4, 2025

Choose a reason for hiding this comment

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

I couldn't trigger multiple ToolCalls, as anthropic api kept producing malformed JSON args, like you mentioned.


def test_check_duplicate_tool_names(self, tools):
"""Test that the AnthropicChatGenerator component fails to initialize with duplicate tool names."""
with pytest.raises(ValueError):
AnthropicChatGenerator(tools=tools + tools)

def test_convert_anthropic_chunk_to_streaming_chunk(self):
def test_convert_anthropic_completion_chunks_with_tools_to_streaming_chunks(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sjrl I updated this test to be more through in checking the conversion of each chunk to streaming chunk. Additionally I removed the old test as it should be covered here.

The mock_anthropic_completion_chunks are also not needed anymore, so I'll remove them. But they were helpful in understanding the flow of response chunks from anthropic.

Copy link
Contributor

@sjrl sjrl left a comment

Choose a reason for hiding this comment

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

Looks good!

@Amnah199 Amnah199 merged commit dc8eebb into main Aug 5, 2025
11 checks passed
@Amnah199 Amnah199 deleted the update-anthropic-streaming branch August 5, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration:anthropic type:documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: Update AnthropicChatGenerator to use the StreamingChunk fields
2 participants