From 6aca828f7240d7e825a6781d2840d2c8a5660967 Mon Sep 17 00:00:00 2001 From: "Md. Fazlul Karim" Date: Sat, 24 Oct 2026 17:23:50 +0600 Subject: [PATCH] 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"}'