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 = {