From f3d2887add2fa298c2900744baf62fc270e02d48 Mon Sep 17 00:00:00 2001 From: David Schneidhoffer Date: Fri, 13 Mar 2026 14:39:36 +0100 Subject: [PATCH] fix(auto_router): skip empty text blocks when joining Anthropic content Filter out dict blocks with an empty or missing "text" value so that trailing spaces are not introduced in the joined message content. Updates the corresponding test assertion to match. --- litellm/router_strategy/auto_router/auto_router.py | 2 +- tests/test_litellm/router_strategy/test_auto_router.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/router_strategy/auto_router/auto_router.py b/litellm/router_strategy/auto_router/auto_router.py index 922ffe90239..a980f7f8164 100644 --- a/litellm/router_strategy/auto_router/auto_router.py +++ b/litellm/router_strategy/auto_router/auto_router.py @@ -123,7 +123,7 @@ class AutoRouter(CustomLogger): raw_content = user_message.get("content", "") # Handle Anthropic-style content blocks: [{"type": "text", "text": "..."}] if isinstance(raw_content, list): - message_content = " ".join(block.get("text", "") for block in raw_content if isinstance(block, dict)) + message_content = " ".join(block.get("text", "") for block in raw_content if isinstance(block, dict) and block.get("text", "")) else: message_content = str(raw_content) route_choice: Optional[Union[RouteChoice, List[RouteChoice]]] = self.routelayer(text=message_content) diff --git a/tests/test_litellm/router_strategy/test_auto_router.py b/tests/test_litellm/router_strategy/test_auto_router.py index c0d7bc0ab46..43786cbd53e 100644 --- a/tests/test_litellm/router_strategy/test_auto_router.py +++ b/tests/test_litellm/router_strategy/test_auto_router.py @@ -232,6 +232,6 @@ class TestAutoRouter: ) # Assert: only dict blocks contribute; "text" defaults to "" when missing - mock_routelayer.assert_called_once_with(text="What is the weather today? In Paris. ") + mock_routelayer.assert_called_once_with(text="What is the weather today? In Paris.") assert result is not None assert result.messages == messages