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(headroom): delegate to the parent deployment hook so deployment-level configs still compress

mateo-berri4d4cf4033470
diff --git a/litellm/proxy/guardrails/guardrail_hooks/headroom/headroom.py b/litellm/proxy/guardrails/guardrail_hooks/headroom/headroom.py
index 6ae16281983..e2d2fffb2df 100644
--- a/litellm/proxy/guardrails/guardrail_hooks/headroom/headroom.py
+++ b/litellm/proxy/guardrails/guardrail_hooks/headroom/headroom.py
@@ -719,17 +719,19 @@ class HeadroomGuardrail(CustomGuardrail):
async def async_pre_call_deployment_hook(
self,
- kwargs: Mapping[str, Any],
+ kwargs: dict[str, Any],
call_type: CallTypes | None,
) -> dict[str, Any] | None: # mutable-ok: overrides CustomLogger hook whose contract is a plain dict
+ base_result: Final = await super().async_pre_call_deployment_hook(kwargs, call_type)
+ effective: Final = base_result if base_result is not None else kwargs
if call_type not in (CallTypes.completion, CallTypes.acompletion):
- return None
- if not kwargs.get("stream"):
- return None
- if not has_headroom_retrieve_tool(kwargs.get("tools")):
- return None
+ return base_result
+ if not effective.get("stream"):
+ 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
- **kwargs,
+ **effective,
"stream": False,
HEADROOM_CONVERTED_STREAM_KEY: True,
}
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 52df875e714..22031abad8a 100644
--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_headroom.py
+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_headroom.py
@@ -1975,7 +1975,8 @@ async def test_pre_call_deployment_hook_converts_stream_only_for_ccr_chat_comple
result = await guardrail.async_pre_call_deployment_hook(kwargs=kwargs, call_type=call_type)
if not expect_conversion:
- assert result is None
+ assert result is kwargs
+ assert HEADROOM_CONVERTED_STREAM_KEY not in kwargs
assert kwargs["stream"] is stream
return
@@ -1985,6 +1986,34 @@ async def test_pre_call_deployment_hook_converts_stream_only_for_ccr_chat_comple
assert kwargs["stream"] is True
+async def test_pre_call_deployment_hook_still_compresses_for_deployment_level_configs(
+ guardrail: HeadroomGuardrail,
+):
+ """Regression for the stream-conversion override swallowing the parent hook:
+ when the guardrail is attached at the deployment level and proxy pre_call never
+ ran, the deployment hook is the only place compression executes, so the
+ override must delegate to CustomGuardrail.async_pre_call_deployment_hook."""
+ kwargs = {
+ "model": "gpt-4o",
+ "messages": [dict(m) for m in ORIGINAL_MESSAGES],
+ "stream": False,
+ "guardrails": ["headroom"],
+ "metadata": {},
+ }
+
+ with patch.object(
+ guardrail.async_handler,
+ "post",
+ new_callable=AsyncMock,
+ return_value=_make_compress_response(COMPRESSED_MESSAGES),
+ ):
+ result = await guardrail.async_pre_call_deployment_hook(kwargs=kwargs, call_type=CallTypes.acompletion)
+
+ assert result is not None
+ assert result["messages"] == EXPECTED_MESSAGES
+
+
@pytest.mark.asyncio
async def test_streaming_chat_completion_resolves_ccr_retrieval_end_to_end(
guardrail: HeadroomGuardrail,