Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,12 @@ async def apply_guardrail(
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,
Comment thread
Ashton-Sidhu marked this conversation as resolved.
)
Comment on lines 155 to 169

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage for new behavior

The existing test test_apply_guardrail_request_with_violations mocks the HiddenLayer response as {"evaluation": {"action": "Block"}} without an analysis key, so it doesn't verify the new block_reasons or threat_level fields in the error detail. Similarly, no test covers the change from scanning all messages to scanning only the last message.

Consider adding a test that:

  1. Mocks a response with analysis entries (e.g., {"analysis": [{"name": "prompt_injection", "detected": True}], "evaluation": {"action": "Block", "threat_level": "high"}})
  2. Verifies that exc_info.value.detail["block_reasons"] contains ["prompt_injection"] and threat_level is "high"
  3. Uses structured_messages with multiple messages and asserts only the last one is sent to the API

elif text := inputs.get("texts"):
result = await self._call_hiddenlayer(
Expand All @@ -171,11 +169,19 @@ async def apply_guardrail(
result = {}

if result.get("evaluation", {}).get("action") == HiddenlayerAction.BLOCK:
detected_reasons = [
entry["name"]
for entry in result.get("analysis", [])
if entry.get("detected")
]
Comment thread
Ashton-Sidhu marked this conversation as resolved.
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,
},
)

Expand Down
Loading