From f36f9c919ecd24a208e7b08fc3eeb9d62683446f Mon Sep 17 00:00:00 2001 From: Ishaan Jaffer Date: Thu, 23 Apr 2026 18:48:55 -0700 Subject: [PATCH] fix(guardrail_endpoints): auto-detect post_call guardrails and use input_type=response --- litellm/proxy/guardrails/guardrail_endpoints.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/guardrails/guardrail_endpoints.py b/litellm/proxy/guardrails/guardrail_endpoints.py index b370f6479ed..d8c89456d18 100644 --- a/litellm/proxy/guardrails/guardrail_endpoints.py +++ b/litellm/proxy/guardrails/guardrail_endpoints.py @@ -2170,8 +2170,20 @@ async def apply_guardrail( request_data: dict = {} if request.messages: request_data["messages"] = request.messages + + # Auto-detect input_type: if the caller didn't specify "response" but the + # guardrail only runs post_call (e.g. LLM-as-a-judge), use "response" so + # the test actually exercises the guardrail logic. + from litellm.types.guardrails import GuardrailEventHooks + + resolved_input_type = request.input_type + if resolved_input_type == "request": + hook = getattr(active_guardrail, "event_hook", None) + if hook == GuardrailEventHooks.post_call or hook == "post_call": + resolved_input_type = "response" + _input_type: Literal["request", "response"] = ( - "response" if request.input_type == "response" else "request" + "response" if resolved_input_type == "response" else "request" ) guardrailed_inputs = await active_guardrail.apply_guardrail( inputs={"texts": [request.text]},