diff --git a/litellm/router.py b/litellm/router.py index c8153b596d6..9e12d9117ba 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -613,6 +613,7 @@ _SESSION_ADAPTER: Final = TypeAdapter(Mapping[str, object]) _MODEL_INFO_ADAPTER: Final = TypeAdapter(Mapping[str, object]) _LITELLM_PARAMS_ADAPTER: Final = TypeAdapter(Mapping[str, object]) _SILENT_MODEL_ADAPTER: Final = TypeAdapter(str | list[str]) +_TEXT_INPUT_BATCH_ADAPTER: Final = TypeAdapter(list[str]) _RESOLVED_RETRY_POLICY_ADAPTER: Final = TypeAdapter(RetryPolicy | None) _ROUTING_KWARGS_ADAPTER: Final[TypeAdapter[Mapping[str, object] | None]] = TypeAdapter(Mapping[str, object] | None) _FALLBACK_HOP_ADAPTER: Final = TypeAdapter(Mapping[str, object]) @@ -14415,6 +14416,15 @@ 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: + try: + _TEXT_INPUT_BATCH_ADAPTER.validate_python(input_data, strict=True) + except ValidationError: + pass + else: + 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 e2ee80b7874..af1c7279992 100644 --- a/tests/unit/test_router/test_router.py +++ b/tests/unit/test_router/test_router.py @@ -30459,6 +30459,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)