mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(stream_chunk_builder): relocate tests to unit shard, fix basedpyright typing and delta coercion
This commit is contained in:
parent
1356ce9430
commit
e5e3ccf09f
4 changed files with 379 additions and 376 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"}'
|
||||
|
|
@ -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"}'
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue