fix: raise HTTPException(400) instead of CustomCodeExecutionError in apply_guardrail

Fixes #29436. When a custom code guardrail's execution fails (e.g. due to
a policy violation raised inside user-provided code), the except clause
wrapped the exception in CustomCodeExecutionError which is a plain Python
Exception with no HTTP status mapping. The proxy's error handler therefore
returned 500 instead of 400.

Raising HTTPException(status_code=400) directly keeps the same informative
error detail and is immediately handled by FastAPI, so the client receives
the correct 400 response code.
This commit is contained in:
PRABHU KIRAN VANDRANKI 2026-06-03 16:37:59 -04:00
parent a72414a061
commit 48c5596d83

View file

@ -257,11 +257,11 @@ class CustomCodeGuardrail(CustomGuardrail):
verbose_proxy_logger.error(
f"Custom code guardrail '{self.guardrail_name}' execution error: {e}"
)
raise CustomCodeExecutionError(
f"Custom code guardrail execution failed: {e}",
details={
"guardrail_name": self.guardrail_name,
"input_type": input_type,
raise HTTPException(
status_code=400,
detail={
"error": f"Custom code guardrail execution failed: {e}",
"guardrail": self.guardrail_name,
},
) from e