From 48c5596d83a02da6697d5d2f038bb5aa1ef2df54 Mon Sep 17 00:00:00 2001 From: PRABHU KIRAN VANDRANKI <72809214+VANDRANKI@users.noreply.github.com> Date: Wed, 3 Jun 2026 16:37:59 -0400 Subject: [PATCH] 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. --- .../custom_code/custom_code_guardrail.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/custom_code/custom_code_guardrail.py b/litellm/proxy/guardrails/guardrail_hooks/custom_code/custom_code_guardrail.py index 58502e309ef..afdd3630a55 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/custom_code/custom_code_guardrail.py +++ b/litellm/proxy/guardrails/guardrail_hooks/custom_code/custom_code_guardrail.py @@ -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