fix(guardrail_endpoints): auto-detect post_call guardrails and use input_type=response

This commit is contained in:
Ishaan Jaffer 2026-04-23 18:48:55 -07:00
parent 09246b8305
commit f36f9c919e
No known key found for this signature in database

View file

@ -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]},