From dfcb5517e52d07eb239b09fd745ef2f72d2a6453 Mon Sep 17 00:00:00 2001 From: agustin18 Date: Thu, 24 Sep 2026 03:39:11 +0000 Subject: [PATCH] fix(responses): fix basedpyright loop typing and achieve 100% test coverage --- .../transformation.py | 6 +++--- ...llm_responses_transformation_transformation.py | 15 +++++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index 9be66f088e6..a3422881539 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -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", diff --git a/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py b/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py index a8ea05f9704..a232a4ebcfc 100644 --- a/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py +++ b/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py @@ -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, )