From f7bb8c836ae1cb174b7798b366d60fd49d6a84ab Mon Sep 17 00:00:00 2001 From: Aloys Jehwin Date: Wed, 23 Sep 2026 00:31:26 +0530 Subject: [PATCH] fix(headroom): drop stream_options when converting a stream to non-streaming MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the Headroom CCR guardrail converts a streaming /chat/completions call to non-streaming (async_pre_call_deployment_hook sets stream=False), it left the client's stream_options in the request. Strict providers reject stream_options without stream=true — DeepSeek returns HTTP 400 'stream_options should be set along with stream = true' — so streaming requests failed whenever Headroom actually compressed and triggered CCR. Pop stream_options from the converted request alongside stream=False. Fixes #40068 --- .../guardrail_hooks/headroom/headroom.py | 7 ++++++- .../guardrail_hooks/test_headroom.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/headroom/headroom.py b/litellm/proxy/guardrails/guardrail_hooks/headroom/headroom.py index eb62b896784..591b19548da 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/headroom/headroom.py +++ b/litellm/proxy/guardrails/guardrail_hooks/headroom/headroom.py @@ -890,11 +890,16 @@ class HeadroomGuardrail(CustomGuardrail): return base_result if not has_headroom_retrieve_tool(effective.get("tools")): return base_result - return { # mutable-ok: the hook contract is a plain dict the router merges into the request kwargs + converted: Final = { # mutable-ok: the hook contract is a plain dict the router merges into the request kwargs **effective, "stream": False, HEADROOM_CONVERTED_STREAM_KEY: True, } + # stream_options is only valid alongside stream=true; leaving it after the + # internal conversion makes strict providers (e.g. DeepSeek) reject the + # request with "stream_options should be set along with stream = true". + converted.pop("stream_options", None) + return converted async def async_should_run_agentic_loop( self, diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_headroom.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_headroom.py index bcecb5b27db..94b135abc95 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_headroom.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_headroom.py @@ -2171,6 +2171,25 @@ async def test_pre_call_deployment_hook_converts_stream_only_for_ccr_chat_comple assert kwargs["stream"] is True +@pytest.mark.asyncio +async def test_pre_call_deployment_hook_drops_stream_options_when_converting(guardrail: HeadroomGuardrail): + """stream_options is only valid with stream=true. When the CCR conversion flips stream to + False it must also drop stream_options, or strict providers (e.g. DeepSeek) 400 with + 'stream_options should be set along with stream = true'.""" + kwargs = { + "model": "gpt-4o", + "stream": True, + "stream_options": {"include_usage": True}, + "tools": [_retrieve_tool_definition()], + } + + result = await guardrail.async_pre_call_deployment_hook(kwargs=kwargs, call_type=CallTypes.acompletion) + + assert result is not None + assert result["stream"] is False + assert "stream_options" not in result + + @pytest.mark.asyncio async def test_pre_call_deployment_hook_leaves_background_streams_alone(guardrail: HeadroomGuardrail): kwargs = {