diff --git a/litellm/decisions/call.py b/litellm/decisions/call.py index cbf086281d5..74f654f0034 100644 --- a/litellm/decisions/call.py +++ b/litellm/decisions/call.py @@ -29,6 +29,7 @@ from litellm.llms.base_llm.decisions.transformation import ( DecisionsResponseValidationError, image_media_type, image_param, + input_has_no_text, input_images, ir_to_systemone_response, remote_image_urls, @@ -607,9 +608,8 @@ def _leaves_nothing_to_decide_on(call: DecisionsCall, kept_input: DecisionsIRSta not call.provider_config.images_in_state and not call.ir_request.audio and not call.ir_request.videos - and isinstance(kept_input, DecisionsIRState) - and not kept_input.images - and not kept_input.state + and not input_images(kept_input) + and input_has_no_text(kept_input) ) diff --git a/litellm/llms/base_llm/decisions/transformation.py b/litellm/llms/base_llm/decisions/transformation.py index ce82aafb3ee..d8c613d3917 100644 --- a/litellm/llms/base_llm/decisions/transformation.py +++ b/litellm/llms/base_llm/decisions/transformation.py @@ -9,7 +9,7 @@ from typing import Final, TypeAlias import httpx from pydantic import TypeAdapter, ValidationError from pydantic_core import ErrorDetails -from typing_extensions import assert_never +from typing_extensions import TypeIs, assert_never # noqa: TID251 # TypeIs reaches typing only on 3.13 import litellm from litellm.litellm_core_utils.url_utils import join_api_base @@ -247,6 +247,28 @@ def _systemone_state(decision_input: DecisionsIRState | DecisionsIRMessages) -> assert_never(decision_input) +def _is_json_object(value: object) -> TypeIs[Mapping[str, object]]: # guard-ok: JSON object keys are strings + return isinstance(value, Mapping) + + +def _is_json_array(value: object) -> TypeIs[Sequence[object]]: # guard-ok: only the items are read + return isinstance(value, Sequence) + + +def _holds_no_text(value: object) -> bool: + if isinstance(value, str): + return not value.strip() + if _is_json_object(value): + return all(map(_holds_no_text, value.values())) + if _is_json_array(value): + return all(map(_holds_no_text, value)) + return False + + +def input_has_no_text(decision_input: DecisionsIRState | DecisionsIRMessages) -> bool: + return _holds_no_text(_systemone_state(decision_input)) + + def _media_type(content_type: str) -> str: return content_type.split(";", 1)[0].strip().lower() diff --git a/tests/unit/decisions/test_main.py b/tests/unit/decisions/test_main.py index 6fe5fc9f690..18201be3e84 100644 --- a/tests/unit/decisions/test_main.py +++ b/tests/unit/decisions/test_main.py @@ -4805,7 +4805,11 @@ _NOT_AN_IMAGE_REQUEST: Final[Mapping[str, object]] = MappingProxyType( @pytest.mark.asyncio @pytest.mark.usefixtures("cloudflare_account") -@pytest.mark.parametrize("state", ("", [], {}), ids=("string", "list", "object")) +@pytest.mark.parametrize( + "state", + ("", [], {}, " ", [""], {"a": ""}, {"a": [" "]}), + ids=("string", "list", "object", "blank", "list-of-empty", "object-of-empty", "nested-blank"), +) async def test_an_empty_state_with_only_a_dropped_image_is_refused_when_the_model_always_took_images( state: object, respx_mock: respx.MockRouter ) -> None: @@ -4817,6 +4821,75 @@ async def test_an_empty_state_with_only_a_dropped_image_is_refused_when_the_mode assert route.call_count == 0 +@pytest.mark.asyncio +@pytest.mark.usefixtures("cloudflare_account") +@pytest.mark.parametrize("state", ("a", ["a"], {"a": "b"}, {"a": 0}, [False]), ids=str) +async def test_a_state_with_something_in_it_still_reaches_the_model_after_its_only_image_is_dropped( + state: object, respx_mock: respx.MockRouter +) -> None: + route: Final = respx_mock.post(f"{_CLOUDFLARE_RUN_URL}/clef").respond(json=_RESPONSE) + + await litellm.asystemone(model="cloudflare/clef", state=state, **_NOT_AN_IMAGE_REQUEST) + + sent: Final = json.loads(route.calls.last.request.content) + assert sent["state"] == state + assert sent.get("images", []) == [] + + +_NOT_AN_IMAGE_DATA_URL: Final = f"data:image/png;base64,{_NOT_AN_IMAGE_BASE64}" +_DECISIONS_PREDICATE: Final[list[dict[str, str]]] = [{"type": "predicate", "name": "red", "instructions": "Is it red?"}] + + +def _decisions_input_with_text_and_dropped_image(text: str) -> list[dict[str, object]]: + return [ + { + "role": "user", + "content": [ + {"type": "input_text", "text": text}, + {"type": "input_image", "image_url": _NOT_AN_IMAGE_DATA_URL}, + ], + } + ] + + +@pytest.mark.asyncio +@pytest.mark.usefixtures("cloudflare_account") +@pytest.mark.parametrize("text", ("", " "), ids=("empty", "blank")) +async def test_decisions_input_with_no_text_and_only_a_dropped_image_is_refused( + text: str, respx_mock: respx.MockRouter +) -> None: + route: Final = respx_mock.post(f"{_CLOUDFLARE_RUN_URL}/clef").respond(json=_RESPONSE) + + with pytest.raises(litellm.BadRequestError): + await litellm.adecisions( + model="cloudflare/clef", + input=_decisions_input_with_text_and_dropped_image(text), + questions=_DECISIONS_PREDICATE, + drop_params=True, + ) + + assert route.call_count == 0 + + +@pytest.mark.asyncio +@pytest.mark.usefixtures("cloudflare_account") +async def test_decisions_input_with_text_still_reaches_the_model_after_its_only_image_is_dropped( + respx_mock: respx.MockRouter, +) -> None: + route: Final = respx_mock.post(f"{_CLOUDFLARE_RUN_URL}/clef").respond(json=_RESPONSE) + + await litellm.adecisions( + model="cloudflare/clef", + input=_decisions_input_with_text_and_dropped_image("Product photo"), + questions=_DECISIONS_PREDICATE, + drop_params=True, + ) + + sent: Final = json.loads(route.calls.last.request.content) + assert sent["state"] == "Product photo" + assert sent.get("images", []) == [] + + @pytest.mark.asyncio @pytest.mark.usefixtures("cloudflare_account") @pytest.mark.parametrize(