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]

test(proxy): cover inherited moderation overrides through during_call_hook

yucheng719d7a198393
diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py
index 40630a6a840..1021b2208ab 100644
--- a/litellm/proxy/utils.py
+++ b/litellm/proxy/utils.py
@@ -2645,10 +2645,6 @@ class ProxyLogging:
user_api_key_dict: UserAPIKeyAuth | None,
call_type: CallTypesLiteral,
):
- """
- Runs the async_moderation_hook() of every CustomGuardrail, and of every
- CustomLogger that overrides it, in parallel
- """
caps: Final = ProxyLogging._callback_capabilities()
if not caps.has_guardrail and not caps.has_moderation_override:
return data
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 58ee8ff656c..fd832439c0f 100644
--- a/tests/test_litellm/proxy/test_proxy_logging_hook_detection.py
+++ b/tests/test_litellm/proxy/test_proxy_logging_hook_detection.py
@@ -652,13 +652,24 @@ async def test_during_call_hook_skips_custom_logger_moderation_without_auth(monk
assert moderator.moderated == []
-def test_callback_capabilities_detects_custom_logger_moderation_override(monkeypatch):
- ProxyLogging._callback_capabilities_cache.clear()
- monkeypatch.setattr(litellm, "callbacks", [CustomLogger(), CustomGuardrail()])
- assert ProxyLogging._callback_capabilities().has_moderation_override is False
+class _InheritsModerationOverride(_RejectsInModeration):
+ pass
+
+
+async def test_during_call_hook_runs_moderation_override_inherited_from_parent(monkeypatch):
+ moderator = _InheritsModerationOverride()
+ monkeypatch.setattr(litellm, "callbacks", [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",
+ )
- monkeypatch.setattr(litellm, "callbacks", [_RejectsInModeration()])
- assert ProxyLogging._callback_capabilities().has_moderation_override is True
+ assert exc_info.value.status_code == 400
+ assert moderator.moderated == ["acompletion"]
@pytest.mark.asyncio