From d27929b7742a3b74b31c58246fe7b6fc8997b1fa Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Thu, 27 Feb 2025 22:54:49 -0800 Subject: [PATCH] fix(http_parsing_utils.py): orjson can throw errors on some emoji's in text, default to json.loads --- .../proxy/common_utils/http_parsing_utils.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/common_utils/http_parsing_utils.py b/litellm/proxy/common_utils/http_parsing_utils.py index ce8f1661c1d..5736ee21527 100644 --- a/litellm/proxy/common_utils/http_parsing_utils.py +++ b/litellm/proxy/common_utils/http_parsing_utils.py @@ -42,7 +42,26 @@ async def _read_request_body(request: Optional[Request]) -> Dict: if not body: parsed_body = {} else: - parsed_body = orjson.loads(body) + try: + parsed_body = orjson.loads(body) + except orjson.JSONDecodeError: + # Fall back to the standard json module which is more forgiving + # First decode bytes to string if needed + body_str = body.decode("utf-8") if isinstance(body, bytes) else body + + # Replace invalid surrogate pairs + import re + + # This regex finds incomplete surrogate pairs + body_str = re.sub( + r"[\uD800-\uDBFF](?![\uDC00-\uDFFF])", "", body_str + ) + # This regex finds low surrogates without high surrogates + body_str = re.sub( + r"(?