litellm/tests/local_testing/test_openai_moderations_hook.py
yuneng-jiang 6a0d03914c
test: drop the cwd-relative sys.path.insert calls from the test suite (#37802)
* test: drop the cwd-relative sys.path.insert calls from the test suite

TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.

Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.

Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.

* test: drop the duplicate imports the sys.path sweep exposed to F811

* test(pre-call-utils): restore the os import the new bedrock tests need
2026-08-22 09:25:58 -07:00

179 lines
6.1 KiB
Python

# What is this?
## This tests the llm guard integration
# What is this?
## Unit test for presidio pii masking
import sys, os, asyncio, time, random
from datetime import datetime
import traceback
from dotenv import load_dotenv
load_dotenv()
import pytest
import litellm
from litellm.proxy.enterprise.enterprise_hooks.openai_moderation import (
_ENTERPRISE_OpenAI_Moderation,
)
from litellm import Router, mock_completion
from litellm.proxy.utils import ProxyLogging, hash_token
from litellm.proxy._types import UserAPIKeyAuth
from litellm.caching.caching import DualCache
### UNIT TESTS FOR OpenAI Moderation ###
@pytest.mark.asyncio
async def test_openai_moderation_error_raising(monkeypatch):
"""
Tests to see OpenAI Moderation raises an error for a flagged response
"""
from unittest.mock import AsyncMock, MagicMock
from litellm.types.llms.openai import OpenAIModerationResponse
litellm.openai_moderations_model_name = "text-moderation-latest"
openai_mod = _ENTERPRISE_OpenAI_Moderation()
_api_key = "sk-12345"
_api_key = hash_token("sk-12345")
user_api_key_dict = UserAPIKeyAuth(api_key=_api_key)
local_cache = DualCache()
llm_router = litellm.Router(
model_list=[
{
"model_name": "text-moderation-latest",
"litellm_params": {
"model": "text-moderation-latest",
"api_key": os.environ.get("OPENAI_API_KEY", "fake-key"),
},
}
]
)
# Mock the amoderation call to return a flagged response
mock_response = MagicMock(spec=OpenAIModerationResponse)
mock_response.results = [MagicMock(flagged=True)]
async def mock_amoderation(*args, **kwargs):
return mock_response
llm_router.amoderation = mock_amoderation
import litellm.proxy.proxy_server as proxy_server
monkeypatch.setattr(proxy_server, "llm_router", llm_router)
with pytest.raises(Exception, match="Violated content safety policy") as exc_info:
await openai_mod.async_moderation_hook(
data={
"messages": [
{
"role": "user",
"content": "fuck off you're the worst",
}
]
},
user_api_key_dict=user_api_key_dict,
call_type="completion",
)
e = exc_info.value
print("Got exception: ", e)
assert "Violated content safety policy" in str(e)
@pytest.mark.asyncio
async def test_openai_moderation_responses_api_input_field():
"""
Tests that OpenAI Moderation works with Responses API input field via apply_guardrail.
This test verifies that the unified guardrail interface (apply_guardrail) correctly
handles different input types: plain text strings, structured messages, and lists.
"""
from unittest.mock import patch
from litellm.types.llms.openai import (
OpenAIModerationResponse,
OpenAIModerationResult,
)
from litellm.proxy.guardrails.guardrail_hooks.openai.moderations import (
OpenAIModerationGuardrail,
)
from litellm.types.utils import GenericGuardrailAPIInputs
# Initialize the open-source OpenAI Moderation guardrail
openai_mod = OpenAIModerationGuardrail(
guardrail_name="openai-moderation-test",
api_key="fake-key-for-testing",
model="omni-moderation-latest",
)
# Mock the async_make_request to return a flagged response
mock_moderation_response = OpenAIModerationResponse(
id="modr-123",
model="omni-moderation-latest",
results=[
OpenAIModerationResult(
flagged=True,
categories={"violence": True, "hate": False},
category_scores={"violence": 0.95, "hate": 0.1},
category_applied_input_types=None,
)
],
)
with patch.object(
openai_mod, "async_make_request", return_value=mock_moderation_response
):
# Test 1: Responses API / Embeddings with texts (string input)
inputs = GenericGuardrailAPIInputs(texts=["I want to hurt people"])
with pytest.raises(Exception, match="Violated OpenAI moderation policy") as exc_info:
await openai_mod.apply_guardrail(
inputs=inputs,
request_data={"model": "gpt-4o", "input": "I want to hurt people"},
input_type="request",
)
e = exc_info.value
print("Got exception for texts input: ", e)
assert "Violated OpenAI moderation policy" in str(e)
# Test 2: Responses API with structured_messages (list of message objects)
inputs = GenericGuardrailAPIInputs(
structured_messages=[
{"role": "user", "content": "I want to hurt people"}
]
)
with pytest.raises(Exception, match="Violated OpenAI moderation policy") as exc_info:
await openai_mod.apply_guardrail(
inputs=inputs,
request_data={
"model": "gpt-4o",
"input": [{"role": "user", "content": "I want to hurt people"}],
},
input_type="request",
)
e = exc_info.value
print("Got exception for structured_messages input: ", e)
assert "Violated OpenAI moderation policy" in str(e)
# Test 3: Chat Completions with structured_messages
inputs = GenericGuardrailAPIInputs(
structured_messages=[
{"role": "user", "content": "I want to hurt people"}
]
)
with pytest.raises(Exception, match="Violated OpenAI moderation policy") as exc_info:
await openai_mod.apply_guardrail(
inputs=inputs,
request_data={
"model": "gpt-4o",
"messages": [{"role": "user", "content": "I want to hurt people"}],
},
input_type="request",
)
e = exc_info.value
print("Got exception for chat completions input: ", e)
assert "Violated OpenAI moderation policy" in str(e)
print("✓ All Responses API moderation tests passed!")