From 4932047b2e23de50d632e7cdd261ba84cd14ed88 Mon Sep 17 00:00:00 2001 From: tbartik <22518863+tbartik@users.noreply.github.com> Date: Mon, 27 Jul 2026 22:57:19 +0200 Subject: [PATCH] test(anthropic): cover malformed content blocks in the survivor predicate _survives_assistant_conversion had a separate non-dict guard that no test reached, since _is_empty_text_block already rejects non-dict blocks before the predicate runs on them in practice. Folding the check into the type lookup keeps the same behaviour for bare strings and blocks without a type key, with no unreachable branch. Extends the sibling-block test with a block missing its type key. --- litellm/litellm_core_utils/prompt_templates/factory.py | 4 +--- .../test_litellm/llms/anthropic/test_message_sanitization.py | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) 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): """