Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

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

Kerry Luce63f48ed2ee
diff --git a/litellm/decisions/call.py b/litellm/decisions/call.py
index b31cb1f69db..b94b3380ad5 100644
--- a/litellm/decisions/call.py
+++ b/litellm/decisions/call.py
@@ -427,7 +427,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, [], {}))
diff --git a/tests/unit/decisions/test_call.py b/tests/unit/decisions/test_call.py
index 03c135369b8..36a16dd5ad1 100644
--- a/tests/unit/decisions/test_call.py
+++ b/tests/unit/decisions/test_call.py
@@ -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