diff --git a/python/packages/hosting-responses/agent_framework_hosting_responses/_parsing.py b/python/packages/hosting-responses/agent_framework_hosting_responses/_parsing.py index 313c699d47e..59ae64523aa 100644 --- a/python/packages/hosting-responses/agent_framework_hosting_responses/_parsing.py +++ b/python/packages/hosting-responses/agent_framework_hosting_responses/_parsing.py @@ -109,7 +109,7 @@ def _content_from_input_item(item: Mapping[str, Any]) -> Content: image_url = cast("Mapping[str, Any]", image_url).get("url") if not isinstance(image_url, str) or not image_url: raise ValueError("input_image requires a non-empty string `image_url`") - return Content.from_uri(uri=image_url, media_type="image/*") + return Content.from_uri(uri=image_url, media_type=None if image_url.startswith("data:image/") else "image/*") if item_type == "input_file": file_url = item.get("file_url") if file_url is not None: diff --git a/python/packages/hosting-responses/tests/hosting_responses/test_parsing.py b/python/packages/hosting-responses/tests/hosting_responses/test_parsing.py index e607708e919..6ece3594ac6 100644 --- a/python/packages/hosting-responses/tests/hosting_responses/test_parsing.py +++ b/python/packages/hosting-responses/tests/hosting_responses/test_parsing.py @@ -173,6 +173,20 @@ def test_image_url_via_object(self) -> None: ]) assert any(getattr(c, "uri", None) == "https://example.com/cat.png" for c in msgs[0].contents) + @pytest.mark.parametrize("as_object", [False, True]) + def test_image_data_url_keeps_its_media_type(self, as_object: bool) -> None: + data_url = "data:image/png;base64,iVBORw0KGgo=" + image_url: Any = {"url": data_url} if as_object else data_url + msgs = messages_from_responses_input([{"type": "input_image", "image_url": image_url}]) + image = msgs[0].contents[0] + assert image.type == "data" + assert image.uri == data_url + assert image.media_type == "image/png" + + def test_image_https_url_keeps_generic_image_media_type(self) -> None: + msgs = messages_from_responses_input([{"type": "input_image", "image_url": "https://example.com/cat.png"}]) + assert msgs[0].contents[0].media_type == "image/*" + def test_unknown_input_type_raises(self) -> None: with pytest.raises(ValueError, match="Unsupported"): messages_from_responses_input([{"type": "weird"}])