From 8f86959c3298d8be87710d2270ff5e28722c41ca Mon Sep 17 00:00:00 2001 From: Krish Dholakia Date: Fri, 28 Feb 2025 14:34:17 -0800 Subject: [PATCH] Litellm dev 02 27 2025 p6 (#8891) * fix(http_parsing_utils.py): orjson can throw errors on some emoji's in text, default to json.loads * fix(sagemaker/handler.py): support passing model id on async streaming * fix(litellm_pre_call_utils.py): Fixes https://github.com/BerriAI/litellm/issues/7237 --- litellm/llms/sagemaker/completion/handler.py | 4 ++++ .../proxy/common_utils/http_parsing_utils.py | 21 ++++++++++++++++++- litellm/proxy/litellm_pre_call_utils.py | 6 +++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/litellm/llms/sagemaker/completion/handler.py b/litellm/llms/sagemaker/completion/handler.py index ae40dd26d9a..4aff5f5d71b 100644 --- a/litellm/llms/sagemaker/completion/handler.py +++ b/litellm/llms/sagemaker/completion/handler.py @@ -433,6 +433,10 @@ class SagemakerLLM(BaseAWSLLM): "messages": messages, } prepared_request = await asyncified_prepare_request(**prepared_request_args) + if model_id is not None: # Fixes https://github.com/BerriAI/litellm/issues/8889 + prepared_request.headers.update( + {"X-Amzn-SageMaker-Inference-Component": model_id} + ) completion_stream = await self.make_async_call( api_base=prepared_request.url, headers=prepared_request.headers, # type: ignore 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"(?