Skip to content

fix aws bedrock claude tool call index #11842

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

Merged
merged 1 commit into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions litellm/llms/bedrock/chat/invoke_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ def __init__(self, model: str) -> None:
self.model = model
self.parser = EventStreamJSONParser()
self.content_blocks: List[ContentBlockDeltaEvent] = []
self.tool_calls_index: Optional[int] = None

def check_empty_tool_call_args(self) -> bool:
"""
Expand Down Expand Up @@ -1314,14 +1315,19 @@ def converse_chunk_parser(self, chunk_data: dict) -> ModelResponseStream:
response_tool_name = get_bedrock_tool_name(
response_tool_name=_response_tool_name
)
self.tool_calls_index = (
0
if self.tool_calls_index is None
else self.tool_calls_index + 1
)
tool_use = {
"id": start_obj["toolUse"]["toolUseId"],
"type": "function",
"function": {
"name": response_tool_name,
"arguments": "",
},
"index": index,
"index": self.tool_calls_index,
}
elif (
"reasoningContent" in start_obj
Expand All @@ -1346,7 +1352,9 @@ def converse_chunk_parser(self, chunk_data: dict) -> ModelResponseStream:
"name": None,
"arguments": delta_obj["toolUse"]["input"],
},
"index": index,
"index": self.tool_calls_index
if self.tool_calls_index is not None
else index,
}
elif "reasoningContent" in delta_obj:
provider_specific_fields = {
Expand Down
61 changes: 61 additions & 0 deletions tests/test_litellm/llms/bedrock/chat/test_invoke_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,64 @@ def test_transform_thinking_blocks_with_redacted_content():
assert len(transformed_thinking_blocks) == 1
assert transformed_thinking_blocks[0]["type"] == "redacted_thinking"
assert transformed_thinking_blocks[0]["data"] == "This is a redacted content"


def test_transform_tool_calls_index():
chunks = [
{
"delta": {"text": "Certainly! I can help you with the"},
"contentBlockIndex": 0,
},
{
"delta": {"text": " current weather and time in Tokyo."},
"contentBlockIndex": 0,
},
{"delta": {"text": " To get this information, I'll"}, "contentBlockIndex": 0},
{"delta": {"text": " need to use two"}, "contentBlockIndex": 0},
{"delta": {"text": " different tools: one"}, "contentBlockIndex": 0},
{"delta": {"text": " for the weather and one for"}, "contentBlockIndex": 0},
{"delta": {"text": " the time. Let me fetch"}, "contentBlockIndex": 0},
{"delta": {"text": " that data for you."}, "contentBlockIndex": 0},
{
"start": {
"toolUse": {
"toolUseId": "tooluse_JX1wqyUvRjyTcVSg_6-JwA",
"name": "Weather_Tool",
}
},
"contentBlockIndex": 1,
},
{"delta": {"toolUse": {"input": ""}}, "contentBlockIndex": 1},
{"delta": {"toolUse": {"input": '{"locatio'}}, "contentBlockIndex": 1},
{"delta": {"toolUse": {"input": 'n": "Toky'}}, "contentBlockIndex": 1},
{"delta": {"toolUse": {"input": 'o"}'}}, "contentBlockIndex": 1},
{
"start": {
"toolUse": {
"toolUseId": "tooluse_rxDBNjDMQ-mqA-YOp9_3cQ",
"name": "Query_Time_Tool",
}
},
"contentBlockIndex": 2,
},
{"delta": {"toolUse": {"input": ""}}, "contentBlockIndex": 2},
{"delta": {"toolUse": {"input": '{"locati'}}, "contentBlockIndex": 2},
{"delta": {"toolUse": {"input": 'on"'}}, "contentBlockIndex": 2},
{"delta": {"toolUse": {"input": ': "Tokyo"}'}}, "contentBlockIndex": 2},
{"stopReason": "tool_use"},
]
decoder = AWSEventStreamDecoder(model="test")
parsed_chunks = []
for chunk in chunks:
parsed_chunk = decoder._chunk_parser(chunk)
parsed_chunks.append(parsed_chunk)
tool_call_chunks1 = parsed_chunks[8:12]
tool_call_chunks2 = parsed_chunks[13:17]
for tool_call_hunk in tool_call_chunks1:
tool_call_hunk_dict = tool_call_hunk.model_dump()
for tool_call in tool_call_hunk_dict["choices"][0]["delta"]["tool_calls"]:
assert tool_call["index"] == 0
for tool_call_hunk in tool_call_chunks2:
tool_call_hunk_dict = tool_call_hunk.model_dump()
for tool_call in tool_call_hunk_dict["choices"][0]["delta"]["tool_calls"]:
assert tool_call["index"] == 1
Loading