Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(proxy): fix zguard httpcode when block input (#31948)

jwang-gifbb58f019a0f9
diff --git a/litellm/proxy/guardrails/guardrail_hooks/zscaler_ai_guard/zscaler_ai_guard.py b/litellm/proxy/guardrails/guardrail_hooks/zscaler_ai_guard/zscaler_ai_guard.py
index d00f9aec67f..c5c66988cb4 100644
--- a/litellm/proxy/guardrails/guardrail_hooks/zscaler_ai_guard/zscaler_ai_guard.py
+++ b/litellm/proxy/guardrails/guardrail_hooks/zscaler_ai_guard/zscaler_ai_guard.py
@@ -200,7 +200,9 @@ class ZscalerAIGuard(CustomGuardrail):
if zscaler_ai_guard_result and zscaler_ai_guard_result.get("action") == "BLOCK":
blocking_info: Final = zscaler_ai_guard_result.get("zscaler_ai_guard_response")
error_message = f"Content blocked by Zscaler AI Guard: {self.extract_blocking_info(blocking_info)}"
- raise Exception(error_message)
+ raise HTTPException(status_code=400, detail={"error": error_message})
+ except HTTPException:
+ raise
except Exception as e:
verbose_proxy_logger.error("ZscalerAIGuard: Failed to apply guardrail: %s", str(e))
raise e
@@ -350,6 +352,8 @@ class ZscalerAIGuard(CustomGuardrail):
try:
response: Final = await self._send_request(zscaler_ai_guard_url, extra_headers, data)
return self._handle_response(response, direction)
+ except HTTPException:
+ raise
except Exception as e:
verbose_proxy_logger.error("%s. Blocking request.", e)
user_facing_error: Final = self._create_user_facing_error(f"{e}")
diff --git a/tests/guardrails_tests/test_zscaler_ai_guard.py b/tests/guardrails_tests/test_zscaler_ai_guard.py
index c28f516cff0..51c86c15dcb 100644
--- a/tests/guardrails_tests/test_zscaler_ai_guard.py
+++ b/tests/guardrails_tests/test_zscaler_ai_guard.py
@@ -337,3 +337,62 @@ async def test_should_omit_policy_id_when_zero_or_negative():
call_args = mock_send.call_args
data = call_args[0][2] # Third positional arg is data
assert "policyId" not in data
+
+@patch(
+ "litellm.proxy.guardrails.guardrail_hooks.zscaler_ai_guard.ZscalerAIGuard.make_zscaler_ai_guard_api_call",
+ new_callable=AsyncMock,
+)
+async def test_apply_guardrail_block_raises_400(mock_api_call):
+ """
+ When the guardrail returns BLOCK, apply_guardrail must raise HTTPException
+ with status_code=400 (not 500).
+ """
+ mock_api_call.return_value = {
+ "action": "BLOCK",
+ "zscaler_ai_guard_response": {
+ "transactionId": "tx-123",
+ "detectorResponses": {"detector1": {"action": "BLOCK"}},
+ },
+ }
+ guardrail = ZscalerAIGuard(api_key="test_key", policy_id=1)
+ inputs = {"texts": ["inject malicious content"]}
+ request_data = {}
+
+ with pytest.raises(HTTPException) as exc_info:
+ await guardrail.apply_guardrail(inputs, request_data, "request")
+
+ assert exc_info.value.status_code == 400
+ assert "blocked" in exc_info.value.detail["error"].lower()
+
+
+@patch(
+ "litellm.proxy.guardrails.guardrail_hooks.zscaler_ai_guard.ZscalerAIGuard.make_zscaler_ai_guard_api_call",
+ new_callable=AsyncMock,
+)
+async def test_apply_guardrail_block_does_not_log_error(mock_api_call):
+ """
+ Regression: a BLOCK is intentional guardrail behavior, not a failure.
+ apply_guardrail must NOT call verbose_proxy_logger.error when content is blocked.
+ """
+ mock_api_call.return_value = {
+ "action": "BLOCK",
+ "zscaler_ai_guard_response": {
+ "transactionId": "tx-456",
+ "detectorResponses": {"detector1": {"action": "BLOCK"}},
+ },
+ }
+ guardrail = ZscalerAIGuard(api_key="test_key", policy_id=1)
+ inputs = {"texts": ["blocked content"]}
+ request_data = {}
+
+ with patch(
+ "litellm.proxy.guardrails.guardrail_hooks.zscaler_ai_guard.zscaler_ai_guard.verbose_proxy_logger"
+ ) as mock_logger:
+ with pytest.raises(HTTPException) as exc_info:
+ await guardrail.apply_guardrail(inputs, request_data, "request")
+
+ mock_logger.error.assert_not_called()
+
+ assert exc_info.value.status_code == 400