From 33f4411f17b73e62332ff658ebadcb9579b99783 Mon Sep 17 00:00:00 2001 From: Joe Cheng Date: Fri, 2 Aug 2024 13:05:23 -0700 Subject: [PATCH] Fix tool call coalescing The previous code seemed to assume that the tool call index property started at 0, but Anthropic sometimes returns them starting at 1. This was causing an extra null-ish tool call to be materialized. --- litellm/main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/litellm/main.py b/litellm/main.py index 989e0b11063..36267aec1d6 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -5094,7 +5094,7 @@ def stream_chunk_builder( name = None type = None tool_calls_list = [] - prev_index = 0 + prev_index = None prev_id = None curr_id = None curr_index = 0 @@ -5120,6 +5120,8 @@ def stream_chunk_builder( name = tool_calls[0].function.name if tool_calls[0].type: type = tool_calls[0].type + if prev_index is None: + prev_index = curr_index if curr_index != prev_index: # new tool call combined_arguments = "".join(argument_list) tool_calls_list.append( @@ -5138,6 +5140,7 @@ def stream_chunk_builder( tool_calls_list.append( { "id": id, + "index": curr_index, "function": {"arguments": combined_arguments, "name": name}, "type": type, }