Repository navigation
fix(decisions): log non-UTF-8 bytes as base64 so the request reaches validation and gets a 400 - #47564
Conversation
…validation and gets a 400
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
Merging this PR will not alter performance
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@litellm-stampbot |
litellm-stampbot: risk lowApproved at Rules (
Cursor Bugbot: Low Risk at Pushes do not re-run this check. Comment |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 05a1ba3. Configure here.
kerry-berri
left a comment
There was a problem hiding this comment.
Low risk by rule and by judgement at 05a1ba3
…validation and gets a 400 (BerriAI#47564) Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> Backport of BerriAI#47564 to stable/1.106.x Cherry-picked from ce63f48 (main)
…validation and gets a 400 (BerriAI#47564) Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> Backport of BerriAI#47564 to stable/1.105.x. Cherry-picked from ce63f48 (main).
TLDR
Problem this solves:
litellm.decisions(input=b"\xff\xfe", ...)raised a rawUnicodeDecodeError, not a 400to_jsonable_pythonrejects non-UTF-8 bytesdefault-message-value, so the log lost the request tooHow it solves it:
_logged_request_messagecallsto_jsonable_python(..., bytes_mode="base64"), so bytes serialize losslesslyBadRequestError(400)pydantic_core.to_jsonable_pythonoption does itUser Flow
Before: an SDK caller who passes raw bytes in a decisions field gets a Python crash instead of the 400 they got before v1.107.0-dev.3
litellm.decisions(model="openai/gpt-6-luna", input=b"\xff\xfe not utf-8", questions=[{"type": "predicate", "instructions": "Is this about a billing problem?"}])UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0with nostatus_codemessagesofdefault-message-value, so nothing about the request is loggedAfter: the same call fails with a 400 that names the invalid field, and the request is logged
litellm.decisions(...)with the same bytes inputlitellm.BadRequestError(status_code=400) sayingInvalid Decisions request: ... input.str Input should be a valid stringmessagescarrying the full request, with the bytes as base64 ("input": "__4gbm90IHV0Zi04")Relevant issues
Fixes a regression from #47183, which added the pre-validation request logging that raised on non-UTF-8 bytes
Affected release
regression since v1.107.0-dev.3 (the only tag that carries #47183, no rc or stable has it)
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreScreenshots / Proof of Fix
Raw bytes cannot reach
/v1/decisionsor/v1/systemonethrough the proxy: the route parses the body withvalidate_json, which 400s invalid UTF-8 before any litellm code runs. The proxy run below shows that on both sides, and the SDK call is the surface where the finding reproduces, so the before/after is an SDK call with a failure callback printing the loggedmessages. Validation rejects bytes before any provider call, so neither leg reaches OpenAI. The valid request on the same rig did, to show the proxy and model are real.Shared setup:
python -m litellm.proxy.proxy_cli --config config.yaml --port <port> --num_workers 1 --detailed_debugwith local Postgres, main at port 4000 and this branch at 4001Before (5145250)
SDK call with non-UTF-8 bytes in
inputpython sdk_bytes.pyProxy: raw 0xFF 0xFE bytes inside the JSON body (unchanged by this PR)
printf '{"model":"luna","input":"\xff\xfe not utf-8","questions":[{"type":"predicate","instructions":"Is this about a billing problem?"}]}' > bad_utf8_body.bincurl -s -w '\nHTTP %{http_code}\n' localhost:4000/v1/decisions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' --data-binary @bad_utf8_body.binAfter (05a1ba3)
SDK call with non-UTF-8 bytes in
inputpython sdk_bytes.pyProxy: raw 0xFF 0xFE bytes inside the JSON body (unchanged by this PR)
curlagainstlocalhost:4001/v1/decisionsProxy: valid request on the same rig (real OpenAI call)
curl -s -w '\nHTTP %{http_code}\n' localhost:4001/v1/decisions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"luna","input":"My card was charged twice.","questions":[{"type":"predicate","instructions":"Is this about a billing problem?"}]}'Regression test
tests/unit/decisions/test_call.py::test_non_utf8_bytes_in_the_request_are_logged_and_rejected_with_a_400on main (5145250) with only the test applied:Type
🐛 Bug Fix
Caveats (if any)
Low
bytes_mode="base64"is pydantic's URL-safe alphabet (-_), not the+/thedata_base64fields takeFinal Attestation
Link to Devin session: https://app.devin.ai/sessions/8458846cc699483291f33927c4e4f383
Open in Devin Desktop: https://app.devin.ai/desktop/session/8458846cc699483291f33927c4e4f383?variant=devin
Requested by: @kerrylu-berri
Note
Low Risk
Narrow change to decisions pre-validation logging serialization; behavior for invalid byte input improves (400 instead of crash) with a small logging format shift for all byte fields.
Overview
Fixes a decisions SDK regression where pre-validation request logging crashed on
bytesfields that are not valid UTF-8, instead of letting validation return a 400._logged_request_messagenow serializes caller fields withto_jsonable_python(..., bytes_mode="base64"), so non-UTF-8input(and other byte values) are logged losslessly as URL-safe base64 and the call still fails withlitellm.BadRequestErroras before. A unit test asserts the logged payload round-trips the raw bytes and that validation still rejects the request.Reviewed by Cursor Bugbot for commit 05a1ba3. Bugbot is set up for automated code reviews on this repo. Configure here.