fix(decisions): log non-UTF-8 bytes as base64 so the request reaches validation and gets a 400 (#47564)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>

Backport of #47564 to stable/1.105.x.
Cherry-picked from ce63f48ed2 (main).
This commit is contained in:
devin-ai-integration[bot] 2026-10-21 00:05:33 +00:00
parent 7312d216cc
commit 627b667688
2 changed files with 19 additions and 1 deletions

View file

@ -160,7 +160,7 @@ def _jsonable_fallback(value: object) -> object:
def _logged_request_message(caller_request: object) -> ChatCompletionUserMessage:
caller_fields: Final = _LOGGED_FIELDS_ADAPTER.validate_python(
to_jsonable_python(caller_request, fallback=_jsonable_fallback)
to_jsonable_python(caller_request, fallback=_jsonable_fallback, bytes_mode="base64")
)
sent_fields: Final = json.dumps(
dict((name, value) for name, value in caller_fields.items() if value not in (None, [], {}))

View file

@ -1,3 +1,4 @@
import base64
import json
from collections.abc import Mapping
from datetime import datetime, timezone
@ -100,3 +101,20 @@ def test_decisions_request_is_logged_even_when_its_questions_fail_validation():
logged: Final = _logged_request(logging_obj)
assert logged["questions"][0]["instructions"] == QUESTION_TEXT
def test_non_utf8_bytes_in_the_request_are_logged_and_rejected_with_a_400():
logging_obj: Final = _logging_obj("adecisions")
raw_input: Final = b"\xff\xfe not utf-8"
with pytest.raises(litellm.BadRequestError):
_prepare_decisions_call(
**CONNECTION,
decision_input=raw_input,
questions=[{"type": "predicate", "instructions": QUESTION_TEXT}],
kwargs={"litellm_logging_obj": logging_obj},
)
logged: Final = _logged_request(logging_obj)
assert base64.urlsafe_b64decode(logged["input"]) == raw_input
assert logged["questions"][0]["instructions"] == QUESTION_TEXT