fix(decisions): refuse a request with no text left once every image is dropped, on /v1/decisions and blank state

This commit is contained in:
mateo-berri 2026-10-30 12:41:22 -07:00
parent 2e3b4a5326
commit e92efed078
3 changed files with 100 additions and 5 deletions

View file

@ -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)
)

View file

@ -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()

View file

@ -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(