fix(decisions): refuse a blank typed text part or a count drop that leaves no input

A `type` value now counts as text only when its object has no `text` key, so a blank
text part with every image dropped answers 400 again while {"type": "refund"} keeps
main's 200. After remote images past the count are dropped, a downloaded image dropped
for its type no longer keeps the lenient no-input check, so a blank request keeps
main's 400 instead of a billed 200 with nothing in it.
This commit is contained in:
mateo-berri 2026-10-31 01:44:26 -07:00
parent b21b8ac5ab
commit 2faf7e70f3
3 changed files with 59 additions and 8 deletions

View file

@ -845,7 +845,11 @@ def _kept_images(call: DecisionsCall) -> tuple[bool, ...]:
return tuple(budget.kept for budget in islice(budgets, 1, None))
def _with_images_within_limits(call: DecisionsCall, kept: Sequence[bool]) -> DecisionsCall:
def _drops_images(before: DecisionsCall, after: DecisionsCall) -> bool:
return len(input_images(after.ir_request.input)) < len(input_images(before.ir_request.input))
def _with_images_within_limits(call: DecisionsCall, kept: Sequence[bool], dropped_past_count: bool) -> DecisionsCall:
if all(kept):
return call
kept_input: Final = _with_images_kept(call.ir_request.input, kept)
@ -853,7 +857,7 @@ def _with_images_within_limits(call: DecisionsCall, kept: Sequence[bool]) -> Dec
if error is None:
return replace(call, ir_request=replace(call.ir_request, input=kept_input))
type_checked: Final = _checked_image_types(call)
if input_images(type_checked.ir_request.input) == input_images(kept_input):
if not dropped_past_count and input_images(type_checked.ir_request.input) == input_images(kept_input):
return type_checked
return _dropped_or_refused(call, kept_input, error)
@ -1035,10 +1039,11 @@ def _posted_without_refused_images(
async def asend(call: DecisionsCall) -> DecisionsResponse | OpenAIDecisionResponse:
supported: Final = _refused_over_count(_checked_image_types(call))
type_checked: Final = _checked_image_types(call)
supported: Final = _refused_over_count(type_checked)
fetched: Final = await _async_with_remote_images_inlined(supported)
kept: Final = _kept_images(fetched)
inlined: Final = _with_images_within_limits(fetched, kept)
inlined: Final = _with_images_within_limits(fetched, kept, _drops_images(type_checked, supported))
body: Final = _request_body(inlined)
try:
response: Final = await _aposted_without_refused_images(
@ -1050,10 +1055,11 @@ async def asend(call: DecisionsCall) -> DecisionsResponse | OpenAIDecisionRespon
def send(call: DecisionsCall) -> DecisionsResponse | OpenAIDecisionResponse:
supported: Final = _refused_over_count(_checked_image_types(call))
type_checked: Final = _checked_image_types(call)
supported: Final = _refused_over_count(type_checked)
fetched: Final = _with_remote_images_inlined(supported)
kept: Final = _kept_images(fetched)
inlined: Final = _with_images_within_limits(fetched, kept)
inlined: Final = _with_images_within_limits(fetched, kept, _drops_images(type_checked, supported))
body: Final = _request_body(inlined)
try:
response: Final = _posted_without_refused_images(inlined, body, _logged_body(supported, fetched, kept, body))

View file

@ -264,7 +264,9 @@ def _holds_no_text(value: object, depth: int = 0) -> bool:
if depth >= DEFAULT_MAX_RECURSE_DEPTH:
return True
if _is_json_object(value):
return all(_holds_no_text(item, depth + 1) for item in value.values())
return all(
_holds_no_text(item, depth + 1) for key, item in value.items() if key != "type" or "text" not in value
)
if _is_json_array(value):
return all(_holds_no_text(item, depth + 1) for item in value)
return False

View file

@ -4808,7 +4808,19 @@ _NOT_AN_IMAGE_REQUEST: Final[Mapping[str, object]] = MappingProxyType(
@pytest.mark.usefixtures("cloudflare_account")
@pytest.mark.parametrize(
"state",
("", [], {}, " ", [""], {"a": ""}, {"a": [" "]}, [None], {"a": None}),
(
"",
[],
{},
" ",
[""],
{"a": ""},
{"a": [" "]},
[None],
{"a": None},
[{"type": "input_text", "text": ""}],
[{"type": "text", "text": " "}],
),
ids=(
"string",
"list",
@ -4819,6 +4831,8 @@ _NOT_AN_IMAGE_REQUEST: Final[Mapping[str, object]] = MappingProxyType(
"nested-blank",
"list-of-null",
"object-of-null",
"input-text-part-with-empty-text",
"text-part-with-blank-text",
),
)
async def test_an_empty_state_with_only_a_dropped_image_is_refused_when_the_model_always_took_images(
@ -4890,6 +4904,35 @@ def test_a_blank_state_still_reaches_the_model_when_its_only_download_is_a_type_
assert sent.get("images", []) == []
@pytest.mark.asyncio
@pytest.mark.usefixtures("cloudflare_account")
@pytest.mark.parametrize("sync", (False, True), ids=("async", "sync"))
async def test_a_blank_state_is_refused_when_its_downloads_drop_for_their_type_after_the_count_drop(
sync: bool, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(litellm, "user_url_validation", False)
respx_mock.get(url__regex=r"^https://images\.example/screen-\d\.gif$").respond(
content=b"GIF89a", headers={"content-type": "image/gif"}
)
route: Final = respx_mock.post(f"{_CLOUDFLARE_RUN_URL}/clef").respond(json=_RESPONSE)
request_kwargs: Final = {
"model": "cloudflare/clef",
"state": "",
"questions": _SYSTEMONE_PREDICATE,
"images": [f"https://images.example/screen-{index}.gif" for index in range(5)],
"drop_params": True,
}
if sync:
with pytest.raises(litellm.BadRequestError):
litellm.systemone(**request_kwargs)
else:
with pytest.raises(litellm.BadRequestError):
await litellm.asystemone(**request_kwargs)
assert not route.called
@pytest.mark.usefixtures("cloudflare_account")
def test_a_blank_state_is_refused_when_a_download_type_drop_comes_with_a_download_size_drop(
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch