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: reorganize unified guardrail tests into nested classes

Yuta Saito82ed6283fa85
diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/unified_guardrails/test_unified_guardrail.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/unified_guardrails/test_unified_guardrail.py
index cdb23916bd5..2a33c56b56a 100644
--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/unified_guardrails/test_unified_guardrail.py
+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/unified_guardrails/test_unified_guardrail.py
@@ -58,71 +58,76 @@ def _inject_mcp_handler_mapping():
unified_module.endpoint_guardrail_translation_mappings = None
-async def test_pre_call_hook_uses_mcp_event_type():
- """pre_call hook should swap to GuardrailEventHooks.pre_mcp_call for MCP calls."""
- handler = UnifiedLLMGuardrails()
- guardrail = RecordingGuardrail()
- cache = DualCache()
-
- data = {
- "guardrail_to_apply": guardrail,
- "messages": [{"role": "user", "content": "Tool: test\nArguments: {}"}],
- "model": "mcp-tool-call",
- }
-
- await handler.async_pre_call_hook(
- user_api_key_dict=None,
- cache=cache,
- data=data,
- call_type=CallTypes.call_mcp_tool.value,
- )
-
- assert guardrail.event_history == [GuardrailEventHooks.pre_mcp_call]
-
-
-async def test_moderation_hook_uses_mcp_event_type():
- """moderation hook should request GuardrailEventHooks.during_mcp_call for MCP calls."""
- handler = UnifiedLLMGuardrails()
- guardrail = RecordingGuardrail()
-
- data = {
- "guardrail_to_apply": guardrail,
- "messages": [{"role": "user", "content": "Tool: test\nArguments: {}"}],
- "model": "mcp-tool-call",
- }
-
- await handler.async_moderation_hook(
- data=data,
- user_api_key_dict=None,
- call_type=CallTypes.call_mcp_tool.value,
- )
-
- assert guardrail.event_history == [GuardrailEventHooks.during_mcp_call]
-
+class TestUnifiedLLMGuardrails:
+ class TestAsyncPreCallHook:
+ @pytest.mark.asyncio
+ async def test_uses_mcp_event_type(self):
+ """pre_call hook should swap to GuardrailEventHooks.pre_mcp_call for MCP calls."""
+ handler = UnifiedLLMGuardrails()
+ guardrail = RecordingGuardrail()
+ cache = DualCache()
+
+ data = {
+ "guardrail_to_apply": guardrail,
+ "messages": [
+ {"role": "user", "content": "Tool: test\nArguments: {}"}
+ ],
+ "model": "mcp-tool-call",
+ }
-async def test_moderation_hook_runs_for_anthropic_messages():
- """Ensure anthropic_messages requests still trigger guardrail moderation."""
- handler = UnifiedLLMGuardrails()
- guardrail = RecordingGuardrail()
+ await handler.async_pre_call_hook(
+ user_api_key_dict=None,
+ cache=cache,
+ data=data,
+ call_type=CallTypes.call_mcp_tool.value,
+ )
+
+ assert guardrail.event_history == [GuardrailEventHooks.pre_mcp_call]
+
+ class TestAsyncModerationHook:
+ @pytest.mark.asyncio
+ async def test_uses_mcp_event_type(self):
+ """moderation hook should request GuardrailEventHooks.during_mcp_call for MCP calls."""
+ handler = UnifiedLLMGuardrails()
+ guardrail = RecordingGuardrail()
+
+ data = {
+ "guardrail_to_apply": guardrail,
+ "messages": [
+ {"role": "user", "content": "Tool: test\nArguments: {}"}
+ ],
+ "model": "mcp-tool-call",
+ }
- data = {
- "guardrail_to_apply": guardrail,
- "messages": [
- {
- "role": "user",
- "content": "Hello Anthropics",
+ await handler.async_moderation_hook(
+ data=data,
+ user_api_key_dict=None,
+ call_type=CallTypes.call_mcp_tool.value,
+ )
+
+ assert guardrail.event_history == [GuardrailEventHooks.during_mcp_call]
+
+ @pytest.mark.asyncio
+ async def test_runs_for_anthropic_messages(self):
+ """Ensure anthropic_messages requests still trigger guardrail moderation."""
+ handler = UnifiedLLMGuardrails()
+ guardrail = RecordingGuardrail()
+
+ data = {
+ "guardrail_to_apply": guardrail,
+ "messages": [
+ {
+ "role": "user",
+ "content": "Hello Anthropics",
+ }
+ ],
+ "model": "anthropic.claude-3",
}
- ],
- "model": "anthropic.claude-3",
- }
- await handler.async_moderation_hook(
- data=data,
- user_api_key_dict=None,
- call_type=CallTypes.anthropic_messages.value,
- )
+ await handler.async_moderation_hook(
+ data=data,
+ user_api_key_dict=None,
+ call_type=CallTypes.anthropic_messages.value,
+ )
- assert guardrail.event_history == [GuardrailEventHooks.during_call]
+ assert guardrail.event_history == [GuardrailEventHooks.during_call]