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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(enterprise): resolve openai_moderations model at call time and default to omni-moderation-latest
yucheng1704aeebb49e
diff --git a/enterprise/enterprise_hooks/openai_moderation.py b/enterprise/enterprise_hooks/openai_moderation.pyindex 2162370804a..017f51bfabd 100644--- a/enterprise/enterprise_hooks/openai_moderation.py+++ b/enterprise/enterprise_hooks/openai_moderation.py@@ -17,6 +17,7 @@ from fastapi import HTTPExceptionimport litellmfrom litellm._logging import verbose_proxy_logger+from litellm.constants import DEFAULT_OPENAI_MODERATIONS_MODELfrom litellm.integrations.custom_logger import CustomLoggerfrom litellm.proxy._types import UserAPIKeyAuthfrom litellm.proxy.guardrails._content_utils import iter_message_text@@ -24,11 +25,9 @@ from litellm.types.utils import CallTypesLiteralclass _ENTERPRISE_OpenAI_Moderation(CustomLogger):- def __init__(self):- self.model_name = (- litellm.openai_moderations_model_name or "text-moderation-latest"- ) # pass the model_name you initialized on litellm.Router()- pass+ @property+ def model_name(self) -> str:+ return litellm.openai_moderations_model_name or DEFAULT_OPENAI_MODERATIONS_MODEL#### CALL HOOKS - proxy only ####diff --git a/litellm/constants.py b/litellm/constants.pyindex e7cb21a3a7d..a7d4eba0f15 100644--- a/litellm/constants.py+++ b/litellm/constants.py@@ -158,6 +158,8 @@ DEFAULT_SEMANTIC_GUARD_EMBEDDING_MODEL: Final = str()DEFAULT_SEMANTIC_GUARD_SIMILARITY_THRESHOLD = float(os.getenv("DEFAULT_SEMANTIC_GUARD_SIMILARITY_THRESHOLD", 0.75))+DEFAULT_OPENAI_MODERATIONS_MODEL: Final = "omni-moderation-latest"+# MCP OAuth2 Client Credentials DefaultsMCP_OAUTH2_TOKEN_EXPIRY_BUFFER_SECONDS: Final = int(os.getenv("MCP_OAUTH2_TOKEN_EXPIRY_BUFFER_SECONDS", "60"))MCP_OAUTH2_TOKEN_CACHE_MAX_SIZE: Final = int(os.getenv("MCP_OAUTH2_TOKEN_CACHE_MAX_SIZE", "200"))diff --git a/tests/test_litellm/proxy/guardrails/test_guardrail_coverage.py b/tests/test_litellm/proxy/guardrails/test_guardrail_coverage.pyindex f25e83b1672..548677c70bc 100644--- a/tests/test_litellm/proxy/guardrails/test_guardrail_coverage.py+++ b/tests/test_litellm/proxy/guardrails/test_guardrail_coverage.py@@ -18,7 +18,9 @@ from unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom httpx import Request, Response+import litellmfrom litellm import DualCache+from litellm.constants import DEFAULT_OPENAI_MODERATIONS_MODELfrom litellm.proxy._types import UserAPIKeyAuthfrom litellm.types.utils import Choices, Message, ModelResponse@@ -764,6 +766,40 @@ async def test_openai_moderation_inspects_multimodal_content(monkeypatch, user_aassert seen_inputs == ["alpha beta"]+ ("configured_after_init", "expected_model"),+ [("omni-moderation-2024-09-26", "omni-moderation-2024-09-26"), (None, DEFAULT_OPENAI_MODERATIONS_MODEL)],+)+async def test_openai_moderation_reads_model_name_at_call_time(+ monkeypatch, user_api_key, configured_after_init, expected_model+):+ """``litellm_settings`` applies ``callbacks`` and ``openai_moderations_model_name`` in YAML+ order, so the hook must resolve the model when it runs, not when it is constructed."""+ from enterprise.enterprise_hooks.openai_moderation import (+ _ENTERPRISE_OpenAI_Moderation,+ )++ monkeypatch.setattr(litellm, "openai_moderations_model_name", None)+ guard = _ENTERPRISE_OpenAI_Moderation()+ monkeypatch.setattr(litellm, "openai_moderations_model_name", configured_after_init)++ class FakeModeration:+ results = [type("R", (), {"flagged": False})()]++ fake_router = MagicMock()+ fake_router.amoderation = AsyncMock(return_value=FakeModeration())+ monkeypatch.setattr("litellm.proxy.proxy_server.llm_router", fake_router, raising=False)++ await guard.async_moderation_hook(+ data={"messages": [{"role": "user", "content": "hello"}]},+ user_api_key_dict=user_api_key,+ call_type="acompletion",+ )++ fake_router.amoderation.assert_awaited_once_with(model=expected_model, input="hello")++# ── Google Text Moderation ────────────────────────────────────────────────────