diff --git a/litellm/utils.py b/litellm/utils.py index 1aed15ba83e..2aead4d2852 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -5045,6 +5045,20 @@ def get_optional_params( model=model, provider_config=provider_config, ) + # include_usage in stream_options is only meaningful for streaming requests; + # OpenAI-compatible backends (e.g. vLLM) reject it with a 400 when stream is + # not True. Strip just that key for non-streaming requests (other keys such as + # include_obfuscation stay). Fixes #29431. + if stream is not True: + _params: Final = cast(dict[str, object], non_default_params) # cast-ok: bare dict from the preprocessor + _stream_options: Final = _params.get("stream_options") + if isinstance(_stream_options, dict) and "include_usage" in _stream_options: + _options: Final = cast(dict[str, object], _stream_options) # cast-ok: caller-supplied stream_options + _without_usage: Final = {k: v for k, v in _options.items() if k != "include_usage"} + if _without_usage: + _params["stream_options"] = _without_usage + else: + _params.pop("stream_options", None) optional_params = pre_process_optional_params( passed_params=passed_params, non_default_params=non_default_params, diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 38287b25169..0a7b4f60b50 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -10880,3 +10880,30 @@ def test_get_optional_params_forwards_a_tool_config_cache_point_to_a_downstream_ ) assert optional_params["extra_body"] == {"kept": True, "cache_control_injection_points": [point]} + + +@pytest.mark.parametrize( + "stream,passed,expected", + [ + (False, {"include_usage": True}, None), + (None, {"include_usage": True}, None), + (True, {"include_usage": True}, {"include_usage": True}), + (False, {"include_usage": True, "include_obfuscation": True}, {"include_obfuscation": True}), + (False, {"include_obfuscation": True}, {"include_obfuscation": True}), + ], +) +def test_get_optional_params_strips_include_usage_when_not_streaming(stream, passed, expected): + """`stream_options.include_usage` only means anything for a streaming request. + OpenAI ignores it otherwise, but OpenAI-compatible backends such as vLLM reject + the call with a 400 (#29431). Only that key is stripped; the rest are kept. + """ + from litellm.utils import get_optional_params + + optional_params = get_optional_params( + model="gpt-4o", + custom_llm_provider="openai", + stream=stream, + stream_options=dict(passed), + ) + + assert optional_params.get("stream_options") == expected