From 6aca828f7240d7e825a6781d2840d2c8a5660967 Mon Sep 17 00:00:00 2001 From: "Md. Fazlul Karim" Date: Sat, 24 Oct 2026 17:23:50 +0600 Subject: [PATCH 1/4] fix(stream_chunk_builder): coerce single object tool_calls in delta to list --- .../streaming_chunk_builder_utils.py | 7 +- litellm/types/utils.py | 5 + tests/test_stream_chunk_builder_unit.py | 191 ++++++++++++++++++ 3 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 tests/test_stream_chunk_builder_unit.py diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index 68cdc204af4..0f1f36b5136 100644 --- a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py +++ b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py @@ -485,7 +485,10 @@ class ChunkProcessor: if not delta: continue choice_index = choice.get("index", 0) - for tool_call in delta.get("tool_calls") or (): + tool_calls = delta.get("tool_calls") or () + if tool_calls and not isinstance(tool_calls, (list, tuple)): + tool_calls = [tool_calls] + for tool_call in tool_calls: if not tool_call: continue if isinstance(tool_call, dict): @@ -533,6 +536,8 @@ class ChunkProcessor: for choice in choices: delta = choice.get("delta", {}) tool_calls = delta.get("tool_calls") or () + if tool_calls and not isinstance(tool_calls, (list, tuple)): + tool_calls = [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 cc7ef851111..1e62163a80f 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -1627,6 +1627,9 @@ 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 not isinstance(tool_calls, (list, tuple)): + tool_calls = [tool_calls] + if tool_calls is not None and isinstance(tool_calls, (list, tuple)): coerced_tool_calls: list[ChatCompletionDeltaToolCall | ChatCompletionDeltaCustomToolCall] = [] current_index = 0 @@ -1649,6 +1652,8 @@ class Delta(SafeAttributeModel, OpenAIObject): coerced_tool_calls.append(ChatCompletionDeltaToolCall(**tool_call)) elif isinstance(tool_call, (ChatCompletionDeltaToolCall, ChatCompletionDeltaCustomToolCall)): coerced_tool_calls.append(tool_call) + else: + coerced_tool_calls.append(tool_call) tool_calls = coerced_tool_calls # Build the per-chunk state directly instead of round-tripping every diff --git a/tests/test_stream_chunk_builder_unit.py b/tests/test_stream_chunk_builder_unit.py new file mode 100644 index 00000000000..75275ec8975 --- /dev/null +++ b/tests/test_stream_chunk_builder_unit.py @@ -0,0 +1,191 @@ +"""Unit tests for stream_chunk_builder and Delta tool call handling.""" + +from litellm import stream_chunk_builder +from litellm.types.utils import ( + ChatCompletionDeltaToolCall, + Delta, + Function, + ModelResponse, +) + + +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 = { + "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 = 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 = 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 = ChatCompletionDeltaToolCall( + id="call_2", + function=Function(name="test_obj", arguments="{}"), + type="function", + index=0, + ) + delta_from_obj = 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 = { + "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 = 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 = [ + { + "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 = 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"}' From 1356ce9430e252c2f114a02b7af5cd00516307f9 Mon Sep 17 00:00:00 2001 From: "Md. Fazlul Karim" Date: Sat, 24 Oct 2026 18:47:05 +0600 Subject: [PATCH 2/4] fix(stream_chunk_builder): address coverage and lint checks --- litellm/types/utils.py | 8 +- tests/test_stream_chunk_builder_unit.py | 173 ++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 5 deletions(-) diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 1e62163a80f..1c27f493278 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -1627,13 +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 not isinstance(tool_calls, (list, tuple)): - tool_calls = [tool_calls] - - 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/test_stream_chunk_builder_unit.py b/tests/test_stream_chunk_builder_unit.py index 75275ec8975..6755eb4cef2 100644 --- a/tests/test_stream_chunk_builder_unit.py +++ b/tests/test_stream_chunk_builder_unit.py @@ -1,6 +1,7 @@ """Unit tests for stream_chunk_builder and Delta tool call handling.""" from litellm import stream_chunk_builder +from litellm.litellm_core_utils.streaming_chunk_builder_utils import ChunkProcessor from litellm.types.utils import ( ChatCompletionDeltaToolCall, Delta, @@ -69,6 +70,16 @@ def test_delta_single_object_tool_calls_coercion() -> None: assert len(delta_from_obj.tool_calls) == 1 assert delta_from_obj.tool_calls[0].id == "call_2" + class CustomToolCall: + pass + + custom_tc = CustomToolCall() + delta_from_custom = Delta(tool_calls=custom_tc) + assert delta_from_custom.tool_calls is not None + assert isinstance(delta_from_custom.tool_calls, list) + assert len(delta_from_custom.tool_calls) == 1 + assert delta_from_custom.tool_calls[0] is custom_tc + def test_stream_chunk_builder_list_tool_call_coercion() -> None: """Verify existing list-based tool call chunks continue to build correctly.""" @@ -189,3 +200,165 @@ def test_stream_chunk_builder_fragmented_single_dict_tool_call() -> None: 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 = ChatCompletionDeltaToolCall( + index=0, + id="call_obj_1", + type="function", + function=Function(name="get_weather", arguments='{"location": "Dhaka"}'), + ) + chunk = { + "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 = 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 = [ + { + "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 = 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.""" + # Single dict tool_call + dict_chunk = { + "choices": [ + { + "index": 0, + "delta": { + "tool_calls": { + "index": 0, + "id": "call_direct_dict", + "type": "function", + "function": { + "name": "lookup", + "arguments": '{"query": "litellm"}', + }, + } + }, + } + ] + } + processor = ChunkProcessor(chunks=[dict_chunk]) + result_dict = processor.get_combined_tool_content([dict_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 = { + "choices": [ + { + "index": 0, + "delta": { + "tool_calls": ChatCompletionDeltaToolCall( + index=0, + id="call_direct_obj", + type="function", + function=Function(name="search", arguments='{"q": "test"}'), + ) + }, + } + ] + } + result_obj = processor.get_combined_tool_content([obj_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"}' From e5e3ccf09f3c32e6b30944fa963d71558fbf030f Mon Sep 17 00:00:00 2001 From: "Md. Fazlul Karim" Date: Sun, 25 Oct 2026 11:34:09 +0600 Subject: [PATCH 3/4] fix(stream_chunk_builder): relocate tests to unit shard, fix basedpyright typing and delta coercion --- .../streaming_chunk_builder_utils.py | 44 ++- litellm/types/utils.py | 2 - tests/test_stream_chunk_builder_unit.py | 364 ------------------ .../test_streaming_chunk_builder_utils.py | 345 +++++++++++++++++ 4 files changed, 379 insertions(+), 376 deletions(-) delete mode 100644 tests/test_stream_chunk_builder_unit.py diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index 0f1f36b5136..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,14 +490,23 @@ class ChunkProcessor: if not delta: continue choice_index = choice.get("index", 0) - tool_calls = delta.get("tool_calls") or () - if tool_calls and not isinstance(tool_calls, (list, tuple)): - tool_calls = [tool_calls] + 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"): @@ -503,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 @@ -534,10 +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 () - if tool_calls and not isinstance(tool_calls, (list, tuple)): - tool_calls = [tool_calls] + 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 1c27f493278..ef1f460de8d 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -1650,8 +1650,6 @@ class Delta(SafeAttributeModel, OpenAIObject): coerced_tool_calls.append(ChatCompletionDeltaToolCall(**tool_call)) elif isinstance(tool_call, (ChatCompletionDeltaToolCall, ChatCompletionDeltaCustomToolCall)): coerced_tool_calls.append(tool_call) - else: - coerced_tool_calls.append(tool_call) tool_calls = coerced_tool_calls # Build the per-chunk state directly instead of round-tripping every diff --git a/tests/test_stream_chunk_builder_unit.py b/tests/test_stream_chunk_builder_unit.py deleted file mode 100644 index 6755eb4cef2..00000000000 --- a/tests/test_stream_chunk_builder_unit.py +++ /dev/null @@ -1,364 +0,0 @@ -"""Unit tests for stream_chunk_builder and Delta tool call handling.""" - -from litellm import stream_chunk_builder -from litellm.litellm_core_utils.streaming_chunk_builder_utils import ChunkProcessor -from litellm.types.utils import ( - ChatCompletionDeltaToolCall, - Delta, - Function, - ModelResponse, -) - - -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 = { - "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 = 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 = 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 = ChatCompletionDeltaToolCall( - id="call_2", - function=Function(name="test_obj", arguments="{}"), - type="function", - index=0, - ) - delta_from_obj = 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" - - class CustomToolCall: - pass - - custom_tc = CustomToolCall() - delta_from_custom = Delta(tool_calls=custom_tc) - assert delta_from_custom.tool_calls is not None - assert isinstance(delta_from_custom.tool_calls, list) - assert len(delta_from_custom.tool_calls) == 1 - assert delta_from_custom.tool_calls[0] is custom_tc - - -def test_stream_chunk_builder_list_tool_call_coercion() -> None: - """Verify existing list-based tool call chunks continue to build correctly.""" - chunk = { - "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 = 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 = [ - { - "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 = 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 = ChatCompletionDeltaToolCall( - index=0, - id="call_obj_1", - type="function", - function=Function(name="get_weather", arguments='{"location": "Dhaka"}'), - ) - chunk = { - "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 = 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 = [ - { - "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 = 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.""" - # Single dict tool_call - dict_chunk = { - "choices": [ - { - "index": 0, - "delta": { - "tool_calls": { - "index": 0, - "id": "call_direct_dict", - "type": "function", - "function": { - "name": "lookup", - "arguments": '{"query": "litellm"}', - }, - } - }, - } - ] - } - processor = ChunkProcessor(chunks=[dict_chunk]) - result_dict = processor.get_combined_tool_content([dict_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 = { - "choices": [ - { - "index": 0, - "delta": { - "tool_calls": ChatCompletionDeltaToolCall( - index=0, - id="call_direct_obj", - type="function", - function=Function(name="search", arguments='{"q": "test"}'), - ) - }, - } - ] - } - result_obj = processor.get_combined_tool_content([obj_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"}' 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..c8642d33ac1 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,347 @@ 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.""" + # 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]) + result_dict: Final = processor.get_combined_tool_content([dict_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]) + 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"}' From 416f6f0ee92cc2e2423e996054f3e630bd050384 Mon Sep 17 00:00:00 2001 From: "Md. Fazlul Karim" Date: Sun, 25 Oct 2026 12:45:47 +0600 Subject: [PATCH 4/4] test(stream_chunk_builder): add test coverage for empty and none delta choices --- .../test_streaming_chunk_builder_utils.py | 79 ++++++++++++++++++- 1 file changed, 76 insertions(+), 3 deletions(-) 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 c8642d33ac1..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 @@ -2815,6 +2815,32 @@ def test_stream_chunk_builder_fragmented_single_object_tool_call() -> None: 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": [ @@ -2834,8 +2860,8 @@ def test_get_combined_tool_content_direct_single_dict_and_object() -> None: } ] } - processor: Final = ChunkProcessor(chunks=[dict_chunk]) - result_dict: Final = processor.get_combined_tool_content([dict_chunk]) + 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" @@ -2857,8 +2883,55 @@ def test_get_combined_tool_content_direct_single_dict_and_object() -> None: } ] } - result_obj: Final = processor.get_combined_tool_content([obj_chunk]) + 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"}' +