mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-29 06:33:28 +00:00
fix(guardrails): skip the empty moderation call when only tool or system text is present
This commit is contained in:
parent
be08189c2f
commit
6a4248cb15
2 changed files with 32 additions and 1 deletions
|
|
@ -40,7 +40,7 @@ class ENTERPRISE_OpenAI_Moderation(CustomLogger):
|
|||
# Covers multimodal list content + Responses-API input. System, instructions
|
||||
# and tool text go in a second call after the message text.
|
||||
message_text, added_text = split_message_text(data)
|
||||
texts = ("".join(message_text), "".join(added_text)) if added_text else ("".join(message_text),)
|
||||
texts = tuple(text for text in ("".join(message_text), "".join(added_text)) if text) or ("",)
|
||||
|
||||
from litellm.proxy.proxy_server import llm_router
|
||||
|
||||
|
|
|
|||
|
|
@ -907,6 +907,37 @@ async def test_openai_moderation_sends_tool_and_system_text_in_its_own_call(
|
|||
assert seen == ["say hi", added]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("data", "inputs"),
|
||||
[
|
||||
({"messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": "x"}}]}]}, [""]),
|
||||
(
|
||||
{
|
||||
"system": "be brief",
|
||||
"messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": "x"}}]}],
|
||||
},
|
||||
["be brief"],
|
||||
),
|
||||
],
|
||||
ids=("no-text", "system-only"),
|
||||
)
|
||||
async def test_openai_moderation_makes_one_call_when_one_side_has_no_text(
|
||||
monkeypatch: pytest.MonkeyPatch, user_api_key: UserAPIKeyAuth, data: dict[str, JsonValue], inputs: list[str]
|
||||
) -> None:
|
||||
from enterprise.enterprise_hooks.openai_moderation import ENTERPRISE_OpenAI_Moderation
|
||||
|
||||
seen: Final[list[str]] = []
|
||||
|
||||
async def amoderation(model: str, input: str) -> SimpleNamespace:
|
||||
seen.append(input)
|
||||
return SimpleNamespace(results=[SimpleNamespace(flagged=False)])
|
||||
|
||||
monkeypatch.setattr("litellm.proxy.proxy_server.llm_router", SimpleNamespace(amoderation=amoderation))
|
||||
assert await ENTERPRISE_OpenAI_Moderation().async_moderation_hook(data, user_api_key, "acompletion") is None
|
||||
assert seen == inputs
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("configured_after_init", "expected_model"),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue