diff --git a/litellm/proxy/common_utils/http_parsing_utils.py b/litellm/proxy/common_utils/http_parsing_utils.py index 2c6b756c702..4eba8ca52aa 100644 --- a/litellm/proxy/common_utils/http_parsing_utils.py +++ b/litellm/proxy/common_utils/http_parsing_utils.py @@ -7,7 +7,7 @@ from typing import Annotated, Any, Final, Literal, Union, get_args, get_origin import orjson from fastapi import Request, UploadFile, status from pydantic import TypeAdapter -from pydantic_core import from_json +from pydantic_core import PydanticSerializationError, from_json from starlette._utils import get_route_path from starlette.requests import HTTPConnection from typing_extensions import NotRequired, ReadOnly, Required, assert_never @@ -173,6 +173,13 @@ def _decode_json_body(body: bytes) -> object: return _DECODED_JSON.validate_python(json.loads(body)) +def _encode_json_body(body: Mapping[str, object]) -> bytes: + try: + return _JSON_REQUEST_BODY.dump_json(body) + except PydanticSerializationError: + return json.dumps(body, ensure_ascii=False, allow_nan=False, separators=(",", ":")).encode("utf-8") + + def _parse_binary_body(body: bytes) -> dict: """JSON sent under a binary content type still parses; real binary (protobuf) carries no params -> {}.""" try: @@ -432,7 +439,7 @@ def rewrite_request_model( ) safe_set_request_parsed_body(request=request, parsed_body=body) request._json = body # pyright: ignore[reportPrivateUsage] # Starlette JSON cache - request._body = _JSON_REQUEST_BODY.dump_json(body) # pyright: ignore[reportPrivateUsage] # Starlette body cache + request._body = _encode_json_body(body) # pyright: ignore[reportPrivateUsage] # Starlette body cache def safe_get_request_headers(request: Request | None) -> dict: diff --git a/tests/unit/proxy/common_utils/test_http_parsing_utils.py b/tests/unit/proxy/common_utils/test_http_parsing_utils.py index 06386070fe5..89cdefe21e3 100644 --- a/tests/unit/proxy/common_utils/test_http_parsing_utils.py +++ b/tests/unit/proxy/common_utils/test_http_parsing_utils.py @@ -205,9 +205,17 @@ async def test_escape_heavy_json_uses_memory_proportional_to_body_size(): @pytest.mark.asyncio @pytest.mark.parametrize("cached", (False, True)) -async def test_model_alias_rewrite_preserves_large_integers_in_every_body_reader(cached: bool): - payload: Final = {"model": "alias", "schema": {"maximum": 12345678901234567890123}} - request: Final = _starlette_request(json.dumps(payload).encode(), "application/json") +@pytest.mark.parametrize("depth", (0, 250, 800)) +async def test_model_alias_rewrite_preserves_large_integers_in_every_body_reader(cached: bool, depth: int): + raw: Final = ( + b'{"model":"alias","schema":' + + b"[" * depth + + b'{"maximum":12345678901234567890123}' + + b"]" * depth + + b"}" + ) + payload: Final = json.loads(raw) + request: Final = _starlette_request(raw, "application/json") request_data: Final = await read_request_body(request) if cached else dict(payload) rewrite_request_model(request_data, request, "integer-test") expected: Final = {**payload, "model": "integer-test"}