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 proxy perf) use `_read_request_body` instead of ast.literal_eval to get better performance (#7545)

Ishaan Jaff716efd5fad9b
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 0e614b0ae3d..df074fd5404 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -1,4 +1,3 @@
-import ast
import asyncio
import copy
import inspect
@@ -3339,13 +3338,7 @@ async def chat_completion( # noqa: PLR0915
data = {}
try:
- body = await request.body()
- body_str = body.decode()
- try:
- data = ast.literal_eval(body_str)
- except Exception:
- data = json.loads(body_str)
-
+ data = await _read_request_body(request=request)
verbose_proxy_logger.debug(
"Request received by LiteLLM:\n{}".format(json.dumps(data, indent=4)),
)
@@ -3612,12 +3605,7 @@ async def completion( # noqa: PLR0915
global user_temperature, user_request_timeout, user_max_tokens, user_api_base
data = {}
try:
- body = await request.body()
- body_str = body.decode()
- try:
- data = ast.literal_eval(body_str)
- except Exception:
- data = json.loads(body_str)
+ data = await _read_request_body(request=request)
data["model"] = (
general_settings.get("completion_model", None) # server default
@@ -5350,12 +5338,7 @@ async def anthropic_response( # noqa: PLR0915
litellm.adapters = [{"id": "anthropic", "adapter": anthropic_adapter}]
global user_temperature, user_request_timeout, user_max_tokens, user_api_base
- body = await request.body()
- body_str = body.decode()
- try:
- request_data: dict = ast.literal_eval(body_str)
- except Exception:
- request_data = json.loads(body_str)
+ request_data = await _read_request_body(request=request)
data: dict = {**request_data, "adapter_id": "anthropic"}
try:
data["model"] = (
diff --git a/tests/local_testing/test_completion.py b/tests/local_testing/test_completion.py
index 4b7c2aabd1d..32dcd2627da 100644
--- a/tests/local_testing/test_completion.py
+++ b/tests/local_testing/test_completion.py
@@ -24,7 +24,7 @@ from litellm import RateLimitError, Timeout, completion, completion_cost, embedd
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
from litellm.litellm_core_utils.prompt_templates.factory import anthropic_messages_pt
-# litellm.num_retries = 3
+# litellm.num_retries=3
litellm.cache = None
litellm.success_callback = []