Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix aws bedrock claude tool call index (#11842)

Johnny.He8ce3995ca53
diff --git a/litellm/llms/bedrock/chat/invoke_handler.py b/litellm/llms/bedrock/chat/invoke_handler.py
index 2c3cf59585c..e33535043d5 100644
--- a/litellm/llms/bedrock/chat/invoke_handler.py
+++ b/litellm/llms/bedrock/chat/invoke_handler.py
@@ -1225,6 +1225,7 @@ class AWSEventStreamDecoder:
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:
"""
@@ -1314,6 +1315,11 @@ class AWSEventStreamDecoder:
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",
@@ -1321,7 +1327,7 @@ class AWSEventStreamDecoder:
"name": response_tool_name,
"arguments": "",
},
- "index": index,
+ "index": self.tool_calls_index,
}
elif (
"reasoningContent" in start_obj
@@ -1346,7 +1352,9 @@ class AWSEventStreamDecoder:
"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 = {
diff --git a/tests/test_litellm/llms/bedrock/chat/test_invoke_handler.py b/tests/test_litellm/llms/bedrock/chat/test_invoke_handler.py
index ed94c784c90..df3a4d09680 100644
--- a/tests/test_litellm/llms/bedrock/chat/test_invoke_handler.py
+++ b/tests/test_litellm/llms/bedrock/chat/test_invoke_handler.py
@@ -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