From 033ba88d7ef78e1a19c1f3a4fcf49cdcc6d3d486 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 17 Jul 2026 14:13:58 -0400 Subject: [PATCH] fix(meta): use .get for type and role lookups in image mime normalization --- .../prompt_templates/common_utils.py | 4 +-- .../llms/openai_like/test_meta_provider.py | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py index aabd1d86d81..89892d3e5b2 100644 --- a/litellm/litellm_core_utils/prompt_templates/common_utils.py +++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py @@ -1250,7 +1250,7 @@ def normalize_image_data_url_mime_type(url: str) -> str: def _normalize_image_mime_type_in_content_item( content_item: OpenAIMessageContentListBlock, ) -> OpenAIMessageContentListBlock: - if content_item["type"] != "image_url": + if content_item.get("type") != "image_url": return content_item image_url = content_item.get("image_url") if isinstance(image_url, str): @@ -1270,7 +1270,7 @@ def _normalize_image_mime_type_in_content_item( def _normalize_image_mime_types_in_message(message: AllMessageValues) -> AllMessageValues: - if message["role"] != "user": + if message.get("role") != "user": return message content = message.get("content") if not isinstance(content, list): diff --git a/tests/test_litellm/llms/openai_like/test_meta_provider.py b/tests/test_litellm/llms/openai_like/test_meta_provider.py index 7769a5d8cc4..50a2a728bac 100644 --- a/tests/test_litellm/llms/openai_like/test_meta_provider.py +++ b/tests/test_litellm/llms/openai_like/test_meta_provider.py @@ -275,6 +275,38 @@ class TestMetaImageMimeNormalization: out = asyncio.run(cfg._transform_messages(messages=messages, model="muse-spark-1.1", is_async=True)) assert out[0]["content"][0]["image_url"]["url"] == f"data:image/png;base64,{PNG_BASE64}" + def test_content_item_without_type_passthrough(self): + cfg = litellm.ProviderConfigManager.get_provider_chat_config( + model="muse-spark-1.1", provider=litellm.LlmProviders.META + ) + assert cfg is not None + out = cfg.transform_request( + model="muse-spark-1.1", + messages=[ + { + "role": "user", + "content": [ + {"text": "hi"}, + {"type": "image_url", "image_url": {"url": f"data:binary/octet-stream;base64,{PNG_BASE64}"}}, + ], + } + ], + optional_params={}, + litellm_params={}, + headers={}, + ) + content = out["messages"][0]["content"] + assert content[0] == {"text": "hi"} + assert content[1]["image_url"]["url"] == f"data:image/png;base64,{PNG_BASE64}" + + def test_message_without_role_passthrough(self): + from litellm.litellm_core_utils.prompt_templates.common_utils import ( + normalize_image_data_url_mime_types_in_messages, + ) + + messages = [{"content": [{"type": "text", "text": "hi"}]}] + assert normalize_image_data_url_mime_types_in_messages(messages) == messages + def test_string_content_passthrough(self): cfg = litellm.ProviderConfigManager.get_provider_chat_config( model="muse-spark-1.1", provider=litellm.LlmProviders.META