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): strip stream_options without mutating the cached request body

Tin Chi Loaf1b7f1347e5
diff --git a/litellm/proxy/response_api_endpoints/endpoints.py b/litellm/proxy/response_api_endpoints/endpoints.py
index dcd7ba18e7f..9601e2d4fde 100644
--- a/litellm/proxy/response_api_endpoints/endpoints.py
+++ b/litellm/proxy/response_api_endpoints/endpoints.py
@@ -349,8 +349,11 @@ async def cursor_chat_completions(
)
# OpenAI's Responses API rejects chat-completions-only stream_options
- # (Cursor sends include_usage); usage arrives via response.completed anyway
- data.pop("stream_options", None)
+ # (Cursor sends include_usage); usage arrives via response.completed anyway.
+ # Rebuild rather than pop: _read_request_body can return the request-scope
+ # cached parsed-body dict itself, and removing keys from it corrupts the
+ # cache's key snapshot so later readers get an empty body
+ data = {key: value for key, value in data.items() if key != "stream_options"}
processor = ProxyBaseLLMRequestProcessing(data=data)
diff --git a/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py b/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py
index 9af1ef4766d..0cc79658b6a 100644
--- a/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py
+++ b/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py
@@ -803,14 +803,30 @@ def test_cursor_chat_completions_input_body_uses_responses_pipeline_and_strips_s
"""A Responses-shaped body (``input``, no ``messages``; what Cursor agent mode
sends) must run through the Responses pipeline with chat-completions output, and
``stream_options`` (chat-completions-only; Cursor sends include_usage) must be
- stripped before the Responses call since OpenAI's Responses API rejects it."""
+ stripped before the Responses call since OpenAI's Responses API rejects it.
+ Stripping must not mutate the dict _read_request_body returned: that can be the
+ request-scope cached parsed body itself, and removing a key from it corrupts the
+ cache's key snapshot so any later _read_request_body caller (spend tracking,
+ logging hooks) silently gets an empty body; a follow-up read must still see the
+ full original body."""
+ import asyncio
+
from openai.types.responses import ResponseOutputMessage, ResponseOutputText
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
+ from litellm.proxy.common_utils.http_parsing_utils import (
+ _read_request_body as real_read_request_body,
+ )
from litellm.types.llms.openai import ResponsesAPIResponse
import litellm.proxy.proxy_server as ps
+ captured_requests = []
+
+ async def capturing_read_request_body(request):
+ captured_requests.append(request)
+ return await real_read_request_body(request=request)
+
mock_router = MagicMock()
mock_router.aresponses = AsyncMock(
return_value=ResponsesAPIResponse(
@@ -835,7 +851,9 @@ def test_cursor_chat_completions_input_body_uses_responses_pipeline_and_strips_s
app.dependency_overrides[user_api_key_auth] = _auth_override
try:
- with patch.object(ps, "llm_router", mock_router):
+ with patch.object(ps, "llm_router", mock_router), patch.object(
+ ps, "_read_request_body", side_effect=capturing_read_request_body
+ ):
client = TestClient(app)
response = client.post(
"/cursor/chat/completions",
@@ -858,3 +876,8 @@ def test_cursor_chat_completions_input_body_uses_responses_pipeline_and_strips_s
called_kwargs = mock_router.aresponses.call_args.kwargs
assert "stream_options" not in called_kwargs
mock_router.acompletion.assert_not_called()
+
+ assert captured_requests
+ followup_body = asyncio.run(real_read_request_body(request=captured_requests[0]))
+ assert followup_body.get("stream_options") == {"include_usage": True}
+ assert followup_body.get("input") == [{"role": "user", "content": "hello"}]