test(llm_response_utils): cover the malformed payload guard where unit coverage runs

codecov/patch reported 0% of the diff hit: the new except-clause lines are in
llm_response_utils, but this test lived under tests/llm_translation, which the
unit coverage job does not run. Move it next to the other llm_response_utils
tests and add a positive case so the guard is proven not to swallow valid
payloads.
This commit is contained in:
sclfcz 2026-09-26 09:07:25 +08:00
parent e2f83aadcf
commit 523c30dcf3

View file

@ -0,0 +1,59 @@
"""The hallucinated multi_tool_use.parallel expansion must not fail the response."""
import json
import pytest
from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import (
_handle_invalid_parallel_tool_calls,
)
from litellm.types.utils import ChatCompletionMessageToolCall, Function
def _parallel_instance(arguments: str):
return [
ChatCompletionMessageToolCall(
id="call_1",
type="function",
function=Function(name="multi_tool_use.parallel", arguments=arguments),
)
]
@pytest.mark.parametrize(
"arguments",
[
'{"tool_uses": [{"recipient_name": "functions.get_weather"}]}',
'{"tool_uses": "nope"}',
'{"tool_uses": [42]}',
"{}",
],
)
def test_malformed_tool_uses_returns_original_calls(arguments):
"""A hallucinated payload we cannot expand must come back untouched."""
tool_calls = _parallel_instance(arguments)
result = _handle_invalid_parallel_tool_calls(tool_calls)
assert len(result) == 1
assert result[0].function.name == "multi_tool_use.parallel"
assert result[0].id == "call_1"
assert result[0].function.arguments == arguments
def test_valid_parallel_payload_still_expands():
"""The guard must not swallow well-formed payloads."""
tool_calls = _parallel_instance(
json.dumps(
{
"tool_uses": [
{"recipient_name": "functions.get_weather", "parameters": {"city": "NYC"}},
{"recipient_name": "functions.get_time", "parameters": {"tz": "EST"}},
]
}
)
)
result = _handle_invalid_parallel_tool_calls(tool_calls)
assert [c.function.name for c in result] == ["get_weather", "get_time"]