mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(stream_chunk_builder): address coverage and lint checks
This commit is contained in:
parent
6aca828f72
commit
1356ce9430
2 changed files with 176 additions and 5 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"}'
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue