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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(guardrails): honor configured timeout in Zscaler AI Guard (#36110)
yucheng-berrif4f59ec4c35f
diff --git a/litellm/proxy/guardrails/guardrail_hooks/zscaler_ai_guard/__init__.py b/litellm/proxy/guardrails/guardrail_hooks/zscaler_ai_guard/__init__.pyindex 408260d8483..270c28b094e 100644--- a/litellm/proxy/guardrails/guardrail_hooks/zscaler_ai_guard/__init__.py+++ b/litellm/proxy/guardrails/guardrail_hooks/zscaler_ai_guard/__init__.py@@ -18,6 +18,7 @@ def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"send_user_api_key_alias=litellm_params.send_user_api_key_alias,send_user_api_key_user_id=litellm_params.send_user_api_key_user_id,send_user_api_key_team_id=litellm_params.send_user_api_key_team_id,+ timeout=litellm_params.timeout,guardrail_name=guardrail.get("guardrail_name", ""),event_hook=litellm_params.mode,default_on=litellm_params.default_on,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.pyindex c5c66988cb4..1aefa38ecf8 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@@ -22,9 +22,10 @@ from litellm.types.utils import GenericGuardrailAPIInputsif TYPE_CHECKING:from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+ from litellm.types.guardrails import LitellmParamsfrom litellm.types.proxy.guardrails.guardrail_hooks.base import GuardrailConfigModel-GUARDRAIL_TIMEOUT: Final = 5+DEFAULT_GUARDRAIL_TIMEOUT: Final = 5.0class ZscalerAIGuard(CustomGuardrail):@@ -43,6 +44,7 @@ class ZscalerAIGuard(CustomGuardrail):send_user_api_key_alias: bool | None = None,send_user_api_key_user_id: bool | None = None,send_user_api_key_team_id: bool | None = None,+ timeout: float | None = None,**kwargs,):kwargs.setdefault("supported_event_hooks", list(self.get_supported_event_hooks()))@@ -68,6 +70,7 @@ class ZscalerAIGuard(CustomGuardrail):if send_user_api_key_team_id is not Noneelse os.getenv("SEND_USER_API_KEY_TEAM_ID", "False").lower() in ("true", "1"))+ self.timeout = self._resolve_timeout(timeout)verbose_proxy_logger.debug("send_user_api_key_alias: %s, \n send_user_api_key_user_id:%s, \n send_user_api_key_team_id:%s",@@ -80,6 +83,29 @@ class ZscalerAIGuard(CustomGuardrail):verbose_proxy_logger.debug("ZscalerAIGuard Initializing ...")+ @staticmethod+ def _resolve_timeout(timeout: float | None) -> float:+ """+ Resolve the effective per-request timeout, falling back to the default+ when it is unset or non-positive.+ """+ if timeout is None:+ return DEFAULT_GUARDRAIL_TIMEOUT++ if timeout <= 0:+ verbose_proxy_logger.warning(+ "Ignoring non-positive Zscaler AI Guard timeout %s, using %s seconds",+ timeout,+ DEFAULT_GUARDRAIL_TIMEOUT,+ )+ return DEFAULT_GUARDRAIL_TIMEOUT++ return timeout++ def update_in_memory_litellm_params(self, litellm_params: "LitellmParams") -> None:+ super().update_in_memory_litellm_params(litellm_params)+ self.timeout = self._resolve_timeout(litellm_params.timeout)+@staticmethoddef _resolve_metadata_value(request_data: dict | None, key: str) -> str | None:"""@@ -267,7 +293,7 @@ class ZscalerAIGuard(CustomGuardrail):f"{url}",headers=headers,json=data,- timeout=GUARDRAIL_TIMEOUT,+ timeout=self.timeout,)response.raise_for_status()return responsediff --git a/litellm/types/proxy/guardrails/guardrail_hooks/zscaler_ai_guard.py b/litellm/types/proxy/guardrails/guardrail_hooks/zscaler_ai_guard.pyindex 3991cee8548..37125c4d583 100644--- a/litellm/types/proxy/guardrails/guardrail_hooks/zscaler_ai_guard.py+++ b/litellm/types/proxy/guardrails/guardrail_hooks/zscaler_ai_guard.py@@ -79,6 +79,15 @@ class ZscalerAIGuardConfigModel(GuardrailConfigModel):json_schema_extra={"ui_type": GuardrailParamUITypes.BOOL},)+ timeout: float | None = Field(+ default=None,+ description=(+ "Timeout for each Zscaler AI Guard API call, in seconds. Must be positive. "+ "Raise it if scans fail under load with 'Connection timed out'. "+ "Defaults to 5 seconds."+ ),+ )+@model_validator(mode="after")def validate_endpoint_configuration(self) -> "ZscalerAIGuardConfigModel":"""diff --git a/tests/guardrails_tests/test_zscaler_ai_guard.py b/tests/guardrails_tests/test_zscaler_ai_guard.pyindex 51c86c15dcb..76e498673b0 100644--- a/tests/guardrails_tests/test_zscaler_ai_guard.py+++ b/tests/guardrails_tests/test_zscaler_ai_guard.py@@ -396,3 +396,122 @@ async def test_apply_guardrail_block_does_not_log_error(mock_api_call):mock_logger.error.assert_not_called()assert exc_info.value.status_code == 400+++async def test_send_request_uses_default_timeout_when_unconfigured():+ """+ Regression: unconfigured guardrails must keep the historical 5s timeout.+ """+ guardrail = ZscalerAIGuard(api_key="test_key", policy_id=1)++ assert guardrail.timeout == 5.0++ with patch(+ "litellm.proxy.guardrails.guardrail_hooks.zscaler_ai_guard.zscaler_ai_guard.get_async_httpx_client"+ ) as mock_get_client:+ mock_client = Mock()+ mock_client.post = AsyncMock(return_value=Mock(status_code=200))+ mock_get_client.return_value = mock_client++ await guardrail._send_request("http://example.com", {}, {})++ assert mock_client.post.call_args.kwargs["timeout"] == 5.0+++async def test_send_request_uses_configured_timeout():+ """+ Regression for LIT-5222: a configured timeout must reach the HTTP call.++ Before the fix _send_request passed a module-level constant, so a slow+ upstream failed at 5s with `Timeout passed=5` no matter what was configured.+ """+ guardrail = ZscalerAIGuard(api_key="test_key", policy_id=1, timeout=30)++ assert guardrail.timeout == 30++ with patch(+ "litellm.proxy.guardrails.guardrail_hooks.zscaler_ai_guard.zscaler_ai_guard.get_async_httpx_client"+ ) as mock_get_client:+ mock_client = Mock()+ mock_client.post = AsyncMock(return_value=Mock(status_code=200))+ mock_get_client.return_value = mock_client++ await guardrail._send_request("http://example.com", {}, {})++ assert mock_client.post.call_args.kwargs["timeout"] == 30+++def test_initialize_guardrail_forwards_configured_timeout():+ """+ Regression for LIT-5222: the `timeout` key from config.yaml must survive+ initialization. It reaches LitellmParams already, but the initializer used+ to drop it before it could reach the guardrail instance.+ """+ from litellm.proxy.guardrails.guardrail_hooks.zscaler_ai_guard import (+ initialize_guardrail,+ )+ from litellm.types.guardrails import LitellmParams++ litellm_params = LitellmParams(+ guardrail="zscaler_ai_guard",+ mode="pre_call",+ api_key="test_key",+ api_base="http://example.com",+ policy_id=1,+ timeout="30",+ )++ guardrail = initialize_guardrail(+ litellm_params, {"guardrail_name": "zscaler-configured-timeout"}+ )++ assert guardrail.timeout == 30.0+++def test_config_model_exposes_timeout_to_dashboard():+ """+ The dashboard guardrail form is built from get_config_model(), so the field+ has to be declared there for the setting to be reachable outside config.yaml.+ """+ config_model = ZscalerAIGuard.get_config_model()++ assert config_model is not None+ assert "timeout" in config_model.model_fields++[email protected]("bad_timeout", [0, -1])+def test_non_positive_timeout_falls_back_to_default(bad_timeout):+ """+ Regression: httpx rejects a negative timeout and treats 0 as "fail+ immediately", so a non-positive value would break every scan instead of+ relaxing the limit the operator was trying to raise.+ """+ guardrail = ZscalerAIGuard(api_key="test_key", policy_id=1, timeout=bad_timeout)++ assert guardrail.timeout == 5.0+++def test_update_in_memory_litellm_params_keeps_timeout_resolved():+ """+ Regression: the base implementation copies every LitellmParams attribute+ onto the guardrail, so an unset timeout would overwrite the resolved value+ with None and silently fall back to the shared client's 600s default.+ """+ from litellm.types.guardrails import LitellmParams++ guardrail = ZscalerAIGuard(api_key="test_key", policy_id=1, timeout=30)+ assert guardrail.timeout == 30++ guardrail.update_in_memory_litellm_params(+ LitellmParams(guardrail="zscaler_ai_guard", mode="pre_call", api_key="test_key")+ )+ assert guardrail.timeout == 5.0++ guardrail.update_in_memory_litellm_params(+ LitellmParams(+ guardrail="zscaler_ai_guard", mode="pre_call", api_key="test_key", timeout=45+ )+ )+ assert guardrail.timeout == 45.0