diff --git a/litellm/interactions/litellm_responses_transformation/transformation.py b/litellm/interactions/litellm_responses_transformation/transformation.py index 48cc798f631..06fc39bd4ef 100644 --- a/litellm/interactions/litellm_responses_transformation/transformation.py +++ b/litellm/interactions/litellm_responses_transformation/transformation.py @@ -201,7 +201,7 @@ class LiteLLMResponsesInteractionsConfig: def _transform_image_content_item(item: Mapping[str, object]) -> Mapping[str, object]: uri: Final = item.get("uri") if isinstance(uri, str) and uri: - return {"type": "input_image", "image_url": uri} + return MappingProxyType({"type": "input_image", "image_url": uri}) data: Final = item.get("data") if isinstance(data, str) and data: @@ -210,7 +210,7 @@ class LiteLLMResponsesInteractionsConfig: or LiteLLMResponsesInteractionsConfig._sniff_image_mime_type(data) or "application/octet-stream" ) - return {"type": "input_image", "image_url": f"data:{mime_type};base64,{data}"} + return MappingProxyType({"type": "input_image", "image_url": f"data:{mime_type};base64,{data}"}) return item diff --git a/tests/test_litellm/interactions/test_litellm_responses_bridge.py b/tests/test_litellm/interactions/test_litellm_responses_bridge.py index 6244c0b1f09..cf68ca3d636 100644 --- a/tests/test_litellm/interactions/test_litellm_responses_bridge.py +++ b/tests/test_litellm/interactions/test_litellm_responses_bridge.py @@ -137,6 +137,45 @@ class TestBridgeInputTransformation: ) assert transformed[0]["content"][0]["image_url"].startswith("data:application/octet-stream;base64,") + def test_image_content_webp_signature_is_sniffed_from_data(self): + webp_signature_b64 = base64.b64encode(b"RIFF\x00\x00\x00\x00WEBPrest-of-file").decode() + image_part = {"type": "image", "data": webp_signature_b64} + transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input( + [{"type": "user_input", "content": [image_part]}] + ) + assert transformed == [ + { + "role": "user", + "content": [{"type": "input_image", "image_url": f"data:image/webp;base64,{webp_signature_b64}"}], + } + ] + + def test_image_content_with_undecodable_data_defaults_to_octet_stream(self): + image_part = {"type": "image", "data": "a" + "!" * 23} + transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input( + [{"type": "user_input", "content": [image_part]}] + ) + assert transformed[0]["content"][0]["image_url"].startswith("data:application/octet-stream;base64,") + + def test_image_content_with_too_short_data_defaults_to_octet_stream(self): + image_part = {"type": "image", "data": "ab"} + transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input( + [{"type": "user_input", "content": [image_part]}] + ) + assert transformed == [ + { + "role": "user", + "content": [{"type": "input_image", "image_url": "data:application/octet-stream;base64,ab"}], + } + ] + + def test_image_content_without_data_or_uri_passes_through_unchanged(self): + image_part = {"type": "image", "mime_type": "image/png"} + transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input( + [{"type": "user_input", "content": [image_part]}] + ) + assert transformed == [{"role": "user", "content": [image_part]}] + def test_unrecognized_content_type_passes_through_unchanged(self): other_part = {"type": "document", "data": "base64data", "mime_type": "application/pdf"} transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(