diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index 9ce138725fb..d7a6b4180be 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -2024,9 +2024,7 @@ def _is_empty_text_block(block: object) -> bool: def _survives_assistant_conversion(block: object) -> bool: - if not isinstance(block, dict): - return False - block_type = block.get("type") + block_type = block.get("type") if isinstance(block, dict) else None if not isinstance(block_type, str) or block_type == "text": return False if block_type == "thinking": diff --git a/tests/test_litellm/llms/anthropic/test_message_sanitization.py b/tests/test_litellm/llms/anthropic/test_message_sanitization.py index 9009f2d0799..c3c2a9ecd3e 100644 --- a/tests/test_litellm/llms/anthropic/test_message_sanitization.py +++ b/tests/test_litellm/llms/anthropic/test_message_sanitization.py @@ -508,8 +508,9 @@ class TestMessageSanitization: {"type": "image_url", "image_url": {"url": "data:image/png;base64,aGk="}}, {"type": "thinking", "thinking": ""}, {"type": "unknown_future_block"}, + {"no_type_key": "malformed"}, ], - ids=["image_url", "unsignable_thinking", "unknown_type"], + ids=["image_url", "unsignable_thinking", "unknown_type", "missing_type"], ) def test_empty_text_kept_when_sibling_block_would_not_survive(self, sibling_block): """