mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-29 06:33:28 +00:00
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.106.x
Cherry-picked from ce63f48ed2 (main)
This commit is contained in:
parent
00b54f2c83
commit
79a61ade5f
2 changed files with 21 additions and 1 deletions
|
|
@ -418,7 +418,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, [], {}))
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import base64
|
||||
import json
|
||||
from collections.abc import Mapping
|
||||
from datetime import datetime, timezone
|
||||
|
|
@ -112,3 +113,22 @@ 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}],
|
||||
audio=None,
|
||||
videos=None,
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue