diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index 68cdc204af4..139aa64e74f 100644 --- a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py +++ b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py @@ -152,7 +152,12 @@ class _ToolCallFragment(TypedDict, total=False): class _ToolCallDelta(TypedDict, total=False): - tool_calls: ReadOnly[Sequence[_ToolCallFragment | ChatCompletionDeltaToolCall | ChatCompletionDeltaCustomToolCall]] + tool_calls: ReadOnly[ + Sequence[_ToolCallFragment | ChatCompletionDeltaToolCall | ChatCompletionDeltaCustomToolCall] + | _ToolCallFragment + | ChatCompletionDeltaToolCall + | ChatCompletionDeltaCustomToolCall + ] class _ToolCallChoice(TypedDict, total=False): @@ -485,11 +490,23 @@ class ChunkProcessor: if not delta: continue choice_index = choice.get("index", 0) - for tool_call in delta.get("tool_calls") or (): + raw_tool_calls = delta.get("tool_calls") + if not raw_tool_calls: + continue + if isinstance( + raw_tool_calls, + (dict, ChatCompletionDeltaToolCall, ChatCompletionDeltaCustomToolCall), + ): + tool_calls = (raw_tool_calls,) + else: + tool_calls = raw_tool_calls + for tool_call in tool_calls: if not tool_call: continue + key: _ToolCallKey if isinstance(tool_call, dict): - key = (choice_index, tool_call.get("index", 0)) + call_index: int = int(tool_call.get("index") or 0) + key = (choice_index, call_index) function = tool_call.get("function") if isinstance(function, dict): if fragment_arguments := function.get("arguments"): @@ -500,7 +517,8 @@ class ChunkProcessor: if isinstance(custom, dict) and (custom_input := custom.get("input")): yield key, "custom_input", custom_input else: - key = (choice_index, getattr(tool_call, "index", 0)) + call_index = int(getattr(tool_call, "index", 0) or 0) + key = (choice_index, call_index) function = getattr(tool_call, "function", None) if object_arguments := getattr(function, "arguments", None): yield key, "arguments", object_arguments @@ -531,8 +549,19 @@ class ChunkProcessor: for chunk in tool_call_chunks: choices = chunk["choices"] for choice in choices: - delta = choice.get("delta", {}) - tool_calls = delta.get("tool_calls") or () + delta = choice.get("delta") + if not delta: + continue + raw_tool_calls = delta.get("tool_calls") + if not raw_tool_calls: + continue + if isinstance( + raw_tool_calls, + (dict, ChatCompletionDeltaToolCall, ChatCompletionDeltaCustomToolCall), + ): + tool_calls = (raw_tool_calls,) + else: + tool_calls = raw_tool_calls choice_index = choice.get("index", 0) for tool_call in tool_calls: diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 7e8645fd0dd..5f118978ef3 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -1627,10 +1627,11 @@ class Delta(SafeAttributeModel, OpenAIObject): if function_call is not None and isinstance(function_call, dict): function_call = FunctionCall(**function_call) - if tool_calls is not None and isinstance(tool_calls, (list, tuple)): + if tool_calls is not None: + raw_tool_calls: Final = [tool_calls] if not isinstance(tool_calls, (list, tuple)) else tool_calls coerced_tool_calls: list[ChatCompletionDeltaToolCall | ChatCompletionDeltaCustomToolCall] = [] current_index = 0 - for tool_call in tool_calls: + for tool_call in raw_tool_calls: if isinstance(tool_call, dict): if tool_call.get("index", None) is None: tool_call["index"] = current_index diff --git a/tests/unit/litellm_core_utils/test_streaming_chunk_builder_utils.py b/tests/unit/litellm_core_utils/test_streaming_chunk_builder_utils.py index 5c56245cf33..333a8f3389f 100644 --- a/tests/unit/litellm_core_utils/test_streaming_chunk_builder_utils.py +++ b/tests/unit/litellm_core_utils/test_streaming_chunk_builder_utils.py @@ -19,6 +19,7 @@ from litellm.types.utils import ( ChatCompletionMessageToolCall, Delta, Function, + ModelResponse, ModelResponseStream, PromptTokensDetails, ServerToolUse, @@ -2517,3 +2518,420 @@ def test_stream_chunk_builder_keeps_reasoning_items_on_text_only_replies(): assert response is not None assert response.choices[0].message.content == "Hello world" assert response.choices[0].message.reasoning_items == reasoning_items + + +def test_stream_chunk_builder_single_dict_tool_call_coercion() -> None: + """Test that stream_chunk_builder properly handles delta.tool_calls as a single dict.""" + chunk: Final = { + "id": "chatcmpl-test-single", + "object": "chat.completion.chunk", + "created": 1234567890, + "model": "gpt-4", + "choices": [ + { + "index": 0, + "delta": { + "role": "assistant", + "tool_calls": { + "index": 0, + "id": "call_single_1", + "type": "function", + "function": { + "name": "get_weather", + "arguments": '{"location": "Dhaka"}', + }, + }, + }, + "finish_reason": "tool_calls", + } + ], + } + + rebuilt_response: Final = stream_chunk_builder(chunks=[chunk]) + + assert isinstance(rebuilt_response, ModelResponse) + assert len(rebuilt_response.choices) > 0 + tool_calls = rebuilt_response.choices[0].message.tool_calls + assert tool_calls is not None + assert len(tool_calls) == 1 + assert tool_calls[0].id == "call_single_1" + assert tool_calls[0].function.name == "get_weather" + assert tool_calls[0].function.arguments == '{"location": "Dhaka"}' + + +def test_delta_single_object_tool_calls_coercion() -> None: + """Test that Delta.__init__ coerces single object / dict tool_calls into a list.""" + delta_from_dict: Final = Delta(tool_calls={"id": "call_1", "function": {"name": "test"}}) + assert delta_from_dict.tool_calls is not None + assert isinstance(delta_from_dict.tool_calls, list) + assert len(delta_from_dict.tool_calls) == 1 + assert delta_from_dict.tool_calls[0].id == "call_1" + assert delta_from_dict.tool_calls[0].function.name == "test" + + tool_call_obj: Final = ChatCompletionDeltaToolCall( + id="call_2", + function=Function(name="test_obj", arguments="{}"), + type="function", + index=0, + ) + delta_from_obj: Final = Delta(tool_calls=tool_call_obj) + assert delta_from_obj.tool_calls is not None + assert isinstance(delta_from_obj.tool_calls, list) + assert len(delta_from_obj.tool_calls) == 1 + assert delta_from_obj.tool_calls[0].id == "call_2" + + +def test_stream_chunk_builder_list_tool_call_coercion() -> None: + """Verify existing list-based tool call chunks continue to build correctly.""" + chunk: Final = { + "id": "chatcmpl-test-list", + "object": "chat.completion.chunk", + "created": 1234567890, + "model": "gpt-4", + "choices": [ + { + "index": 0, + "delta": { + "role": "assistant", + "tool_calls": [ + { + "index": 0, + "id": "call_list_1", + "type": "function", + "function": { + "name": "get_weather", + "arguments": '{"location": "Dhaka"}', + }, + } + ], + }, + "finish_reason": "tool_calls", + } + ], + } + + rebuilt_response: Final = stream_chunk_builder(chunks=[chunk]) + + assert isinstance(rebuilt_response, ModelResponse) + assert len(rebuilt_response.choices) > 0 + tool_calls = rebuilt_response.choices[0].message.tool_calls + assert tool_calls is not None + assert len(tool_calls) == 1 + assert tool_calls[0].id == "call_list_1" + assert tool_calls[0].function.name == "get_weather" + + +def test_stream_chunk_builder_fragmented_single_dict_tool_call() -> None: + """Verify fragmented chunks with single dict tool_calls correctly reconstruct function arguments.""" + chunks: Final = [ + { + "id": "chatcmpl-frag-1", + "object": "chat.completion.chunk", + "created": 1234567890, + "model": "gpt-4", + "choices": [ + { + "index": 0, + "delta": { + "role": "assistant", + "tool_calls": { + "index": 0, + "id": "call_frag_1", + "type": "function", + "function": { + "name": "get_weather", + "arguments": "", + }, + }, + }, + "finish_reason": None, + } + ], + }, + { + "id": "chatcmpl-frag-1", + "object": "chat.completion.chunk", + "created": 1234567890, + "model": "gpt-4", + "choices": [ + { + "index": 0, + "delta": { + "tool_calls": { + "index": 0, + "function": { + "arguments": '{"location": ', + }, + }, + }, + "finish_reason": None, + } + ], + }, + { + "id": "chatcmpl-frag-1", + "object": "chat.completion.chunk", + "created": 1234567890, + "model": "gpt-4", + "choices": [ + { + "index": 0, + "delta": { + "tool_calls": { + "index": 0, + "function": { + "arguments": '"Dhaka"}', + }, + }, + }, + "finish_reason": "tool_calls", + } + ], + }, + ] + + rebuilt_response: Final = stream_chunk_builder(chunks=chunks) + + assert isinstance(rebuilt_response, ModelResponse) + assert len(rebuilt_response.choices) > 0 + tool_calls = rebuilt_response.choices[0].message.tool_calls + assert tool_calls is not None + assert len(tool_calls) == 1 + assert tool_calls[0].id == "call_frag_1" + assert tool_calls[0].function.name == "get_weather" + assert tool_calls[0].function.arguments == '{"location": "Dhaka"}' + + +def test_stream_chunk_builder_single_object_tool_call_coercion() -> None: + """Test that stream_chunk_builder handles delta.tool_calls as a single ChatCompletionDeltaToolCall object.""" + tool_call_obj: Final = ChatCompletionDeltaToolCall( + index=0, + id="call_obj_1", + type="function", + function=Function(name="get_weather", arguments='{"location": "Dhaka"}'), + ) + chunk: Final = { + "id": "chatcmpl-test-single-obj", + "object": "chat.completion.chunk", + "created": 1234567890, + "model": "gpt-4", + "choices": [ + { + "index": 0, + "delta": { + "role": "assistant", + "tool_calls": tool_call_obj, + }, + "finish_reason": "tool_calls", + } + ], + } + + rebuilt_response: Final = stream_chunk_builder(chunks=[chunk]) + + assert isinstance(rebuilt_response, ModelResponse) + assert len(rebuilt_response.choices) > 0 + tool_calls = rebuilt_response.choices[0].message.tool_calls + assert tool_calls is not None + assert len(tool_calls) == 1 + assert tool_calls[0].id == "call_obj_1" + assert tool_calls[0].function.name == "get_weather" + assert tool_calls[0].function.arguments == '{"location": "Dhaka"}' + + +def test_stream_chunk_builder_fragmented_single_object_tool_call() -> None: + """Verify fragmented chunks with single object tool_calls correctly reconstruct function arguments.""" + chunks: Final = [ + { + "id": "chatcmpl-frag-obj-1", + "object": "chat.completion.chunk", + "created": 1234567890, + "model": "gpt-4", + "choices": [ + { + "index": 0, + "delta": { + "role": "assistant", + "tool_calls": ChatCompletionDeltaToolCall( + index=0, + id="call_frag_obj_1", + type="function", + function=Function(name="get_weather", arguments=""), + ), + }, + "finish_reason": None, + } + ], + }, + { + "id": "chatcmpl-frag-obj-1", + "object": "chat.completion.chunk", + "created": 1234567890, + "model": "gpt-4", + "choices": [ + { + "index": 0, + "delta": { + "tool_calls": ChatCompletionDeltaToolCall( + index=0, + function=Function(arguments='{"location": '), + ), + }, + "finish_reason": None, + } + ], + }, + { + "id": "chatcmpl-frag-obj-1", + "object": "chat.completion.chunk", + "created": 1234567890, + "model": "gpt-4", + "choices": [ + { + "index": 0, + "delta": { + "tool_calls": ChatCompletionDeltaToolCall( + index=0, + function=Function(arguments='"Dhaka"}'), + ), + }, + "finish_reason": "tool_calls", + } + ], + }, + ] + + rebuilt_response: Final = stream_chunk_builder(chunks=chunks) + + assert isinstance(rebuilt_response, ModelResponse) + assert len(rebuilt_response.choices) > 0 + tool_calls = rebuilt_response.choices[0].message.tool_calls + assert tool_calls is not None + assert len(tool_calls) == 1 + assert tool_calls[0].id == "call_frag_obj_1" + assert tool_calls[0].function.name == "get_weather" + assert tool_calls[0].function.arguments == '{"location": "Dhaka"}' + + +def test_get_combined_tool_content_direct_single_dict_and_object() -> None: + """Directly test get_combined_tool_content with a single dict and single object tool call.""" + # Empty delta / None delta chunk to ensure robust skipping of choices without deltas or empty tool calls + empty_delta_chunk: Final = { + "choices": [ + { + "index": 0, + "delta": None, + }, + { + "index": 1, + "delta": {}, + }, + { + "index": 2, + "delta": {"tool_calls": None}, + }, + { + "index": 3, + "delta": {"tool_calls": []}, + }, + { + "index": 4, + "delta": {"tool_calls": [None]}, + }, + ] + } + + # Single dict tool_call + dict_chunk: Final = { + "choices": [ + { + "index": 0, + "delta": { + "tool_calls": { + "index": 0, + "id": "call_direct_dict", + "type": "function", + "function": { + "name": "lookup", + "arguments": '{"query": "litellm"}', + }, + } + }, + } + ] + } + processor: Final = ChunkProcessor(chunks=[dict_chunk, empty_delta_chunk]) + result_dict: Final = processor.get_combined_tool_content([dict_chunk, empty_delta_chunk]) + assert len(result_dict) == 1 + assert result_dict[0].id == "call_direct_dict" + assert result_dict[0].function.name == "lookup" + assert result_dict[0].function.arguments == '{"query": "litellm"}' + + # Single object tool_call + obj_chunk: Final = { + "choices": [ + { + "index": 0, + "delta": { + "tool_calls": ChatCompletionDeltaToolCall( + index=0, + id="call_direct_obj", + type="function", + function=Function(name="search", arguments='{"q": "test"}'), + ) + }, + } + ] + } + result_obj: Final = processor.get_combined_tool_content([obj_chunk, empty_delta_chunk]) + assert len(result_obj) == 1 + assert result_obj[0].id == "call_direct_obj" + assert result_obj[0].function.name == "search" + assert result_obj[0].function.arguments == '{"q": "test"}' + + +def test_stream_chunk_builder_empty_delta_choice() -> None: + """Test that stream_chunk_builder handles choices with empty delta alongside tool call choices.""" + chunks: Final = [ + { + "id": "chatcmpl-empty-delta", + "object": "chat.completion.chunk", + "created": 1234567890, + "model": "gpt-4", + "choices": [ + { + "index": 0, + "delta": { + "role": "assistant", + "tool_calls": { + "index": 0, + "id": "call_empty_delta", + "type": "function", + "function": { + "name": "get_weather", + "arguments": '{"location": "Tokyo"}', + }, + }, + }, + "finish_reason": "tool_calls", + }, + { + "index": 1, + "delta": {}, + "finish_reason": None, + }, + ], + } + ] + + rebuilt_response: Final = stream_chunk_builder(chunks=chunks) + + assert isinstance(rebuilt_response, ModelResponse) + assert len(rebuilt_response.choices) > 0 + tool_calls = rebuilt_response.choices[0].message.tool_calls + assert tool_calls is not None + assert len(tool_calls) == 1 + assert tool_calls[0].id == "call_empty_delta" + assert tool_calls[0].function.name == "get_weather" + assert tool_calls[0].function.arguments == '{"location": "Tokyo"}' +