From 181313bb28a4241f05c6db6ea65d034c70497190 Mon Sep 17 00:00:00 2001 From: Ashton Sidhu Date: Tue, 3 Mar 2026 16:28:33 -0500 Subject: [PATCH] Serialize error message to a string; only scan last message --- .../hiddenlayer/hiddenlayer.py | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/hiddenlayer/hiddenlayer.py b/litellm/proxy/guardrails/guardrail_hooks/hiddenlayer/hiddenlayer.py index b907fbbcbda..076af0e7bde 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/hiddenlayer/hiddenlayer.py +++ b/litellm/proxy/guardrails/guardrail_hooks/hiddenlayer/hiddenlayer.py @@ -151,14 +151,12 @@ class HiddenlayerGuardrail(CustomGuardrail): project_id = headers.get("hl-project-id") if scan_params := inputs.get("structured_messages"): - # Convert AllMessageValues to simple dict format for HiddenLayer API - messages = [ - {"role": msg.get("role", "user"), "content": msg.get("content", "")} - for msg in scan_params - if isinstance(msg, dict) - ] + last_msg = scan_params[-1] result = await self._call_hiddenlayer( - project_id, hl_request_metadata, {"messages": messages}, input_type + project_id, + hl_request_metadata, + {"messages": [{"role": last_msg.get("role", "user"), "content": last_msg.get("content", "")}]}, + input_type, ) elif text := inputs.get("texts"): result = await self._call_hiddenlayer( @@ -171,11 +169,19 @@ class HiddenlayerGuardrail(CustomGuardrail): result = {} if result.get("evaluation", {}).get("action") == HiddenlayerAction.BLOCK: + detected_reasons = [ + entry["name"] + for entry in result.get("analysis", []) + if entry.get("detected") + ] + threat_level = result.get("evaluation", {}).get("threat_level") raise HTTPException( status_code=400, detail={ "error": "Violated guardrail policy", - "hiddenlayer_guardrail_response": HiddenlayerMessages.BLOCK_MESSAGE, + "hiddenlayer_guardrail_response": HiddenlayerMessages.BLOCK_MESSAGE.value, + "block_reasons": detected_reasons, + "threat_level": threat_level, }, )