diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 697c0d16938..feb3c857641 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -1033,9 +1033,7 @@ def normalize_system_role_in_anthropic_messages( elif content is not None and not isinstance(content, str): # Best-effort: stringify unknown content shapes so callers # don't silently lose information. - promoted_blocks.append( - {"type": "text", "text": str(content)} - ) + promoted_blocks.append({"type": "text", "text": str(content)}) else: kept.append(m) diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py index 08be5118dfa..8d9b4c63ec6 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py @@ -411,9 +411,7 @@ def anthropic_messages_handler( # role." OpenAI-style clients routinely send the system prompt as # messages[0]; lift those up to the top-level `system` parameter so # sync callers (litellm.messages.create) stay valid too. See #30705. - messages, system = normalize_system_role_in_anthropic_messages( - messages, system - ) + messages, system = normalize_system_role_in_anthropic_messages(messages, system) metadata = validate_anthropic_api_metadata(metadata) diff --git a/tests/test_litellm/llms/anthropic/test_normalize_system_role.py b/tests/test_litellm/llms/anthropic/test_normalize_system_role.py index 0d1e63e9c92..fa9f6612472 100644 --- a/tests/test_litellm/llms/anthropic/test_normalize_system_role.py +++ b/tests/test_litellm/llms/anthropic/test_normalize_system_role.py @@ -40,9 +40,7 @@ class TestNormalizeSystemRole: new_messages, new_system = normalize_system_role_in_anthropic_messages( messages, system=None ) - assert new_system == [ - {"type": "text", "text": "you are a helpful assistant"} - ] + assert new_system == [{"type": "text", "text": "you are a helpful assistant"}] assert new_messages == [{"role": "user", "content": "hi"}] def test_promotes_content_block_system_message(self): @@ -221,6 +219,7 @@ class TestSyncPathNormalization: from litellm.llms.anthropic.common_utils import ( normalize_system_role_in_anthropic_messages as real_normalize, ) + return real_normalize(messages, system) # Patch the symbol the handler imported. @@ -242,9 +241,9 @@ class TestSyncPathNormalization: finally: handler_module.normalize_system_role_in_anthropic_messages = original - assert len(calls) >= 1, ( - "sync handler did not invoke normalize_system_role_in_anthropic_messages" - ) + assert ( + len(calls) >= 1 + ), "sync handler did not invoke normalize_system_role_in_anthropic_messages" def test_sync_handler_skips_normalizer_when_presanitized_flag_set(self): """ @@ -260,11 +259,14 @@ class TestSyncPathNormalization: calls = [] - def spy_normalize(messages, system=None): # pragma: no cover - patched at runtime + def spy_normalize( + messages, system=None + ): # pragma: no cover - patched at runtime calls.append((list(messages), system)) from litellm.llms.anthropic.common_utils import ( normalize_system_role_in_anthropic_messages as real_normalize, ) + return real_normalize(messages, system) original = handler_module.normalize_system_role_in_anthropic_messages @@ -288,4 +290,4 @@ class TestSyncPathNormalization: if __name__ == "__main__": - sys.exit(pytest.main([__file__, "-v"])) \ No newline at end of file + sys.exit(pytest.main([__file__, "-v"]))