fix(stream_chunk_builder): coerce single object tool_calls in delta to list

This commit is contained in:
Md. Fazlul Karim 2026-10-24 17:23:50 +06:00
parent 72f1ac4a27
commit 6aca828f72
3 changed files with 202 additions and 1 deletions

View file

@ -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:

View file

@ -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

View file

@ -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"}'