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): keep queued moderation running past a V1 pre_call guardrail

yuchengc7028761aa63
diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py
index dec5b9af2a9..b078a65759e 100644
--- a/litellm/proxy/utils.py
+++ b/litellm/proxy/utils.py
@@ -2767,7 +2767,7 @@ class ProxyLogging:
# V1 implementation - backwards compatibility
if callback.event_hook is None and hasattr(callback, "moderation_check"):
if callback.moderation_check == "pre_call":
- return
+ continue
else:
# Main - V2 Guardrails implementation
from litellm.types.guardrails import GuardrailEventHooks
diff --git a/tests/test_litellm/proxy/test_proxy_logging_hook_detection.py b/tests/test_litellm/proxy/test_proxy_logging_hook_detection.py
index fd832439c0f..dd330d32ce6 100644
--- a/tests/test_litellm/proxy/test_proxy_logging_hook_detection.py
+++ b/tests/test_litellm/proxy/test_proxy_logging_hook_detection.py
@@ -656,6 +656,29 @@ class _InheritsModerationOverride(_RejectsInModeration):
pass
+class _V1PreCallGuardrail(CustomGuardrail):
+ def __init__(self) -> None:
+ super().__init__(guardrail_name="v1-pre-call")
+ self.moderation_check = "pre_call"
+
+
[email protected]("error::RuntimeWarning")
+async def test_during_call_hook_runs_moderation_override_after_v1_pre_call_guardrail(monkeypatch):
+ moderator = _RejectsInModeration()
+ monkeypatch.setattr(litellm, "callbacks", [_V1PreCallGuardrail(), moderator])
+
+ with pytest.raises(HTTPException) as exc_info:
+ await ProxyLogging(user_api_key_cache=DualCache()).during_call_hook(
+ data={"messages": [{"role": "user", "content": "hi"}]},
+ user_api_key_dict=UserAPIKeyAuth(api_key="sk-1234"),
+ call_type="acompletion",
+ )
+
+ assert exc_info.value.status_code == 400
+ assert moderator.moderated == ["acompletion"]
+
+
@pytest.mark.asyncio
async def test_during_call_hook_runs_moderation_override_inherited_from_parent(monkeypatch):
moderator = _InheritsModerationOverride()