fix(responses): fix basedpyright loop typing and achieve 100% test coverage

This commit is contained in:
agustin18 2026-09-24 03:39:11 +00:00
parent 0f6d0b62bf
commit dfcb5517e5
2 changed files with 12 additions and 9 deletions

View file

@ -434,7 +434,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
else:
# Fallback: convert unexpected types to input_text
tool_output = [{"type": "input_text", "text": str(content)}]
normalized_tool_call_id: Final = self._normalize_tool_call_id(tool_call_id)
normalized_tool_call_id = self._normalize_tool_call_id(tool_call_id)
if tool_call_id in custom_tool_call_ids:
input_items.append(
ResponseCustomToolCallOutputParam(
@ -464,8 +464,8 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
for tool_call in tool_calls:
function = tool_call.get("function")
custom = tool_call.get("custom")
raw_id: Final = tool_call.get("id")
normalized_call_id: Final = self._normalize_tool_call_id(raw_id)
raw_id = tool_call.get("id")
normalized_call_id = self._normalize_tool_call_id(raw_id)
if function:
input_tool_call: dict[str, object] = {
"type": "function_call",

View file

@ -4357,6 +4357,7 @@ def test_map_optional_params_verbosity_merges_into_text():
@pytest.mark.parametrize(
"tool_call_id,is_custom",
[
(None, False),
("call_short_123", False),
("call_" + "a" * 59, False),
("call_" + "a" * 60, False),
@ -4371,17 +4372,19 @@ def test_map_optional_params_verbosity_merges_into_text():
],
)
def test_convert_chat_completion_messages_to_responses_api_normalizes_overlong_tool_call_ids(
tool_call_id: str,
tool_call_id: str | None,
is_custom: bool,
):
"""Overlong tool call IDs (> 64 chars) must be deterministically normalized to <= 64 characters."""
import hashlib
from litellm.completion_extras.litellm_responses_transformation.transformation import (
LiteLLMResponsesTransformationHandler,
)
expected_id: Final = (
tool_call_id
None
if tool_call_id is None
else tool_call_id
if len(tool_call_id) <= 64
else f"{tool_call_id[:31]}_{hashlib.sha256(tool_call_id.encode('utf-8')).hexdigest()[:32]}"
)
@ -4428,7 +4431,8 @@ def test_convert_chat_completion_messages_to_responses_api_normalizes_overlong_t
call_id = func_tool_call_item.get("call_id")
assert call_id == expected_id
assert len(str(call_id)) <= 64
if call_id is not None:
assert len(str(call_id)) <= 64
if is_custom:
custom_output_item: Final = next(
@ -4446,7 +4450,6 @@ def test_convert_chat_completion_messages_to_responses_api_normalizes_overlong_t
def test_convert_chat_completion_messages_to_responses_api_overlong_collision_resistance():
"""Two distinct overlong IDs with the same prefix must not produce collision."""
from litellm.completion_extras.litellm_responses_transformation.transformation import (
LiteLLMResponsesTransformationHandler,
)
@ -4483,8 +4486,8 @@ def test_convert_chat_completion_messages_to_responses_api_overlong_collision_re
def test_convert_chat_completion_messages_to_responses_api_mixed_custom_and_function_output_types():
"""Mixed custom and function tool call outputs must maintain respective types without collision."""
import hashlib
from litellm.completion_extras.litellm_responses_transformation.transformation import (
LiteLLMResponsesTransformationHandler,
)