diff --git a/litellm/router.py b/litellm/router.py index 7993b5b921c..ec4705e6f14 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -14347,6 +14347,14 @@ class Router: from litellm.litellm_core_utils.prompt_templates.common_utils import request_contains_image_content from litellm.litellm_core_utils.prompt_templates.factory import resolve_structured_messages + input_data: Final = request_kwargs.get("input") + if ( + not messages + and isinstance(input_data, list) + and all(isinstance(item, str) for item in cast(list[object], input_data)) + ): + return False + structured: Final = resolve_structured_messages( messages=cast(list[dict[str, object]] | None, messages), # cast-ok: chat messages are plain dicts request_kwargs=cast(dict[str, object], request_kwargs), # cast-ok: the resolver only reads the mapping diff --git a/tests/unit/test_router/test_router.py b/tests/unit/test_router/test_router.py index 568a05e8ff4..caeec3c135c 100644 --- a/tests/unit/test_router/test_router.py +++ b/tests/unit/test_router/test_router.py @@ -30099,6 +30099,20 @@ def test_image_routing_drop_declared_text_only_legs_for_responses_input_image(): assert _candidate_ids(router, request_kwargs={"input": _IMAGE_RESPONSES_INPUT}) == ["vision"] +@pytest.mark.parametrize( + ("messages", "expected"), + [(None, ("vision", "declared-text")), (_IMAGE_CHAT, ("vision",))], + ids=["text-batch", "image-messages-take-precedence"], +) +def test_image_routing_handles_text_batch_input_without_overriding_messages( + messages: list[dict[str, object]] | None, expected: tuple[str, ...] +) -> None: + router: Final = _mixed_router(_VISION_LEG, _DECLARED_TEXT_LEG) + assert tuple( + _candidate_ids(router, messages=messages, request_kwargs={"input": ["safe text", "more safe text"]}) + ) == expected + + def test_image_routing_send_image_to_the_group_when_every_leg_is_declared_text_only(): other_text_leg = {**_DECLARED_TEXT_LEG, "model_info": {"id": "declared-text-2", "supports_vision": False}} router: Final = _mixed_router(_DECLARED_TEXT_LEG, other_text_leg)