From f778865836cabff8eb0379fe44f83e2d871b912d Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sun, 12 Jan 2025 16:45:26 -0800 Subject: [PATCH] Revert "fix _read_request_body to re-use parsed body already (#7722)" (#7724) This reverts commit 95183f210362d27824b2239a983387e62d9616ae. --- .../proxy/common_utils/http_parsing_utils.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/litellm/proxy/common_utils/http_parsing_utils.py b/litellm/proxy/common_utils/http_parsing_utils.py index 033ebdb71e..63b904602a 100644 --- a/litellm/proxy/common_utils/http_parsing_utils.py +++ b/litellm/proxy/common_utils/http_parsing_utils.py @@ -21,32 +21,26 @@ async def _read_request_body(request: Optional[Request]) -> Dict: try: if request is None: return {} - - # Check if we already read and parsed the body - if hasattr(request.state, "parsed_body"): - return request.state.parsed_body - _request_headers: dict = _safe_get_request_headers(request=request) content_type = _request_headers.get("content-type", "") - if "form" in content_type: - parsed_body = dict(await request.form()) + return dict(await request.form()) else: # Read the request body body = await request.body() # Return empty dict if body is empty or None if not body: - parsed_body = {} - parsed_body = orjson.loads(body) + return {} - # Cache the parsed result - request.state.parsed_body = parsed_body - return parsed_body + # Attempt JSON parsing (safe for untrusted input) + return orjson.loads(body) except (json.JSONDecodeError, orjson.JSONDecodeError): + # Log detailed information for debugging verbose_proxy_logger.exception("Invalid JSON payload received.") return {} + except Exception as e: # Catch unexpected errors to avoid crashes verbose_proxy_logger.exception(