From 1eff79a2bd83c378579ad592c84e2897a8ec2cac Mon Sep 17 00:00:00 2001 From: Ishaan Jaffer Date: Thu, 5 Feb 2026 13:24:15 -0800 Subject: [PATCH] test_openai_moderation_guardrail_apply_guardrail --- .../openai/test_moderations.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/openai/test_moderations.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/openai/test_moderations.py index 8957b534ea8..b707805cfcb 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/openai/test_moderations.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/openai/test_moderations.py @@ -141,6 +141,64 @@ async def test_openai_moderation_guardrail_safe_content(): assert result == data +@pytest.mark.asyncio +async def test_openai_moderation_guardrail_apply_guardrail(): + """Test OpenAI moderation guardrail apply_guardrail method (unified guardrail interface)""" + from litellm.types.utils import GenericGuardrailAPIInputs + + with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}): + guardrail = OpenAIModerationGuardrail( + guardrail_name="test-openai-moderation", + ) + + # Mock safe moderation response + mock_response = OpenAIModerationResponse( + id="modr-123", + model="omni-moderation-latest", + results=[ + OpenAIModerationResult( + flagged=False, + categories={ + "sexual": False, + "hate": False, + "harassment": False, + "self-harm": False, + "violence": False, + }, + category_scores={ + "sexual": 0.001, + "hate": 0.001, + "harassment": 0.001, + "self-harm": 0.001, + "violence": 0.001, + }, + category_applied_input_types={ + "sexual": [], + "hate": [], + "harassment": [], + "self-harm": [], + "violence": [], + } + ) + ] + ) + + with patch.object(guardrail, 'async_make_request', return_value=mock_response): + # Test apply_guardrail with texts (embeddings-style input) + inputs = GenericGuardrailAPIInputs( + texts=["Hello, how are you?", "What is the weather?"] + ) + + result = await guardrail.apply_guardrail( + inputs=inputs, + request_data={}, + input_type="request", + ) + + # Should return inputs unchanged (moderation doesn't modify, only blocks) + assert result == inputs + + @pytest.mark.asyncio async def test_openai_moderation_guardrail_harmful_content(): """Test OpenAI moderation guardrail with harmful content"""