fix(proxy): keep queued moderation running past a V1 pre_call guardrail
Some checks failed
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Modules / fmt, validate, test (aws) (push) Has been cancelled
Terraform Modules / fmt, validate, test (gcp) (push) Has been cancelled

A V1 CustomGuardrail with moderation_check pre_call returned out of
during_call_hook before asyncio.gather, abandoning already-queued
CustomLogger moderation coroutines and skipping every later callback.
Skip only that guardrail instead.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
yucheng 2026-09-18 23:33:29 +00:00
parent e5d02fecf7
commit c7028761aa
2 changed files with 24 additions and 1 deletions

View file

@ -2767,7 +2767,7 @@ class ProxyLogging:
# V1 implementation - backwards compatibility
if callback.event_hook is None and hasattr(callback, "moderation_check"):
if callback.moderation_check == "pre_call":
return
continue
else:
# Main - V2 Guardrails implementation
from litellm.types.guardrails import GuardrailEventHooks

View file

@ -656,6 +656,29 @@ class _InheritsModerationOverride(_RejectsInModeration):
pass
class _V1PreCallGuardrail(CustomGuardrail):
def __init__(self) -> None:
super().__init__(guardrail_name="v1-pre-call")
self.moderation_check = "pre_call"
@pytest.mark.asyncio
@pytest.mark.filterwarnings("error::RuntimeWarning")
async def test_during_call_hook_runs_moderation_override_after_v1_pre_call_guardrail(monkeypatch):
moderator = _RejectsInModeration()
monkeypatch.setattr(litellm, "callbacks", [_V1PreCallGuardrail(), moderator])
with pytest.raises(HTTPException) as exc_info:
await ProxyLogging(user_api_key_cache=DualCache()).during_call_hook(
data={"messages": [{"role": "user", "content": "hi"}]},
user_api_key_dict=UserAPIKeyAuth(api_key="sk-1234"),
call_type="acompletion",
)
assert exc_info.value.status_code == 400
assert moderator.moderated == ["acompletion"]
@pytest.mark.asyncio
async def test_during_call_hook_runs_moderation_override_inherited_from_parent(monkeypatch):
moderator = _InheritsModerationOverride()