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): exclude images from HiddenLayer v1 scans (#29210)
Ashton Sidhu9f9236e8d5f2
diff --git a/litellm/proxy/guardrails/guardrail_hooks/hiddenlayer/hiddenlayer.py b/litellm/proxy/guardrails/guardrail_hooks/hiddenlayer/hiddenlayer.pyindex a6ea2e09583..68914a1989e 100644--- a/litellm/proxy/guardrails/guardrail_hooks/hiddenlayer/hiddenlayer.py+++ b/litellm/proxy/guardrails/guardrail_hooks/hiddenlayer/hiddenlayer.py@@ -156,6 +156,31 @@ def _header_value(headers: Mapping[str, str], key: str, default: str) -> str:return headers.get(key, default)+def _is_image_part(item: object) -> bool:+ """Whether a structured-message content part carries an image rather than text."""++ if not isinstance(item, Mapping):+ return False++ part: Final[Mapping[object, object]] = item+ return part.get("type") == "image_url"+++def _scannable_text(content: object) -> str:+ """Flatten a structured message's content into the single string the v1 detection endpoint takes.++ Image parts are dropped: the endpoint accepts one string, so an image would only reach it as+ its stringified source (a base64 blob or a URL), which is not text the scanner can evaluate.+ """++ if not isinstance(content, list):+ return str(content or "")++ parts: Final[Sequence[object]] = content+ text_parts: Final = [item for item in parts if not _is_image_part(item)] # mutable-ok: sent as a list repr+ return str(text_parts or "")++def is_saas(host: str) -> bool:"""Checks whether the connection is to the SaaS platform"""@@ -270,7 +295,7 @@ class HiddenlayerGuardrail(CustomGuardrail):"messages": [{"role": last_msg.get("role", "user"),- "content": str(last_msg.get("content", "")),+ "content": _scannable_text(last_msg.get("content")),}]},diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_hiddenlayer.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_hiddenlayer.pyindex b140082a3bf..f5d51a601d7 100644--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_hiddenlayer.py+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_hiddenlayer.py@@ -432,7 +432,7 @@ class TestHiddenlayerGuardrail:@pytest.mark.asyncioasync def test_apply_guardrail_request_with_image(self, monkeypatch: pytest.MonkeyPatch):- """Test apply_guardrail sends multimodal content (image) to HiddenLayer v1."""+ """Test apply_guardrail strips images from multimodal content before sending to HiddenLayer v1."""monkeypatch.setenv("HIDDENLAYER_API_BASE", "https://my.hiddenlayer")guardrail = HiddenlayerGuardrail(@@ -485,12 +485,13 @@ class TestHiddenlayerGuardrail:logging_obj=logging_obj,)- # v1 API requires string content — multimodal list is stringified+ # v1 API requires string content — image_url items are stripped and the+ # remaining (text-only) content is stringified before being sent.mock_post.assert_called_once()call_kwargs = mock_post.call_args.kwargssent_content = call_kwargs["json"]["input"]["messages"][0]["content"]assert isinstance(sent_content, str)- assert sent_content == str(multimodal_content)+ assert sent_content == str([{"type": "text", "text": "how much is on this receipt?"}])# Result should be returned without errorassert result is not None