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
"""
Unit tests for Bedrock Guardrails
"""
import json
import asyncio
import logging
from typing import Final
from datetime import datetime, timezone
import sys
from unittest.mock import AsyncMock, MagicMock, patch
import httpx
import pytest
import respx
from fastapi import HTTPException
import litellm
from litellm._logging import verbose_proxy_logger
from litellm.caching.caching import DualCache
from litellm.exceptions import ModifyResponseException
from litellm.proxy._types import UserAPIKeyAuth
from litellm.constants import BEDROCK_APPLY_GUARDRAIL_CHUNK_BUDGET_CHARS
from litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
BedrockContentChunkResult,
BedrockGuardrail,
_redact_pii_matches,
)
from litellm.proxy.utils import ProxyLogging
from litellm.types.guardrails import GuardrailEventHooks
from litellm.types.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
BedrockContentItem,
BedrockTextContent,
)
from litellm.types.utils import CallTypes, ModelResponse
from tests.unit.llms.bedrock.event_loop_probe import EventLoopProbe
from tests.unit.llms.bedrock.event_loop_probe import host_in_loop_probing_vault
@pytest.fixture
def aws_test_credentials(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("AWS_ACCESS_KEY_ID", "test-access-key")
monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "test-secret-key")
@pytest.mark.asyncio
async def test__redact_pii_matches_function():
"""Test the _redact_pii_matches function directly"""
# Test case 1: Response with PII entities
response_with_pii = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{"type": "NAME", "match": "John Smith", "action": "BLOCKED"},
{
"type": "US_SOCIAL_SECURITY_NUMBER",
"match": "324-12-3212",
"action": "BLOCKED",
},
{"type": "PHONE", "match": "607-456-7890", "action": "BLOCKED"},
]
}
}
],
"outputs": [{"text": "Input blocked by PII policy"}],
}
# Call the redaction function
redacted_response = _redact_pii_matches(response_with_pii)
# Verify that PII matches are redacted
pii_entities = redacted_response["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"]
assert pii_entities[0]["match"] == "[REDACTED]", "Name should be redacted"
assert pii_entities[1]["match"] == "[REDACTED]", "SSN should be redacted"
assert pii_entities[2]["match"] == "[REDACTED]", "Phone should be redacted"
# Verify other fields remain unchanged
assert pii_entities[0]["type"] == "NAME"
assert pii_entities[1]["type"] == "US_SOCIAL_SECURITY_NUMBER"
assert pii_entities[2]["type"] == "PHONE"
assert redacted_response["action"] == "GUARDRAIL_INTERVENED"
assert redacted_response["outputs"][0]["text"] == "Input blocked by PII policy"
print("PII redaction function test passed")
@pytest.mark.asyncio
async def test__redact_pii_matches_no_pii():
"""Test _redact_pii_matches with response that has no PII"""
response_no_pii = {"action": "NONE", "assessments": [], "outputs": []}
# Call the redaction function
redacted_response = _redact_pii_matches(response_no_pii)
# Should return the same response unchanged
assert redacted_response == response_no_pii
print("No PII redaction test passed")
@pytest.mark.asyncio
async def test__redact_pii_matches_empty_assessments():
"""Test _redact_pii_matches with empty assessments"""
response_empty_assessments = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [{"sensitiveInformationPolicy": {"piiEntities": []}}],
"outputs": [{"text": "Some output"}],
}
# Call the redaction function
redacted_response = _redact_pii_matches(response_empty_assessments)
# Should return the same response unchanged
assert redacted_response == response_empty_assessments
print("Empty assessments redaction test passed")
@pytest.mark.asyncio
async def test__redact_pii_matches_malformed_response():
"""Test _redact_pii_matches with malformed response (should not crash)"""
# Test with completely malformed response
malformed_response = {
"action": "GUARDRAIL_INTERVENED",
# Wrong type for assessments; redact_nested_match_and_regex_keys walks dict
# values and skips non-dict/list nodes, so this must not raise.
"assessments": "not_a_list",
}
# Should not crash (deep copy + walk skips the string value under assessments)
redacted_response = _redact_pii_matches(malformed_response)
assert redacted_response == malformed_response
# Test with missing keys
missing_keys_response = {
"action": "GUARDRAIL_INTERVENED"
# Missing assessments key
}
redacted_response = _redact_pii_matches(missing_keys_response)
assert redacted_response == missing_keys_response
print("Malformed response redaction test passed")
@pytest.mark.asyncio
async def test__redact_pii_matches_multiple_assessments():
"""Test _redact_pii_matches with multiple assessments containing PII"""
response_multiple_assessments = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "EMAIL",
"match": "[email protected]",
"action": "ANONYMIZED",
}
]
}
},
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "CREDIT_DEBIT_CARD_NUMBER",
"match": "1234-5678-9012-3456",
"action": "BLOCKED",
},
{
"type": "ADDRESS",
"match": "123 Main St, Anytown USA",
"action": "ANONYMIZED",
},
]
}
},
],
"outputs": [{"text": "Multiple PII detected"}],
}
# Call the redaction function
redacted_response = _redact_pii_matches(response_multiple_assessments)
# Verify all PII in all assessments are redacted
assessment1_pii = redacted_response["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"]
assessment2_pii = redacted_response["assessments"][1]["sensitiveInformationPolicy"]["piiEntities"]
assert assessment1_pii[0]["match"] == "[REDACTED]", "Email should be redacted"
assert assessment2_pii[0]["match"] == "[REDACTED]", "Credit card should be redacted"
assert assessment2_pii[1]["match"] == "[REDACTED]", "Address should be redacted"
# Verify types remain unchanged
assert assessment1_pii[0]["type"] == "EMAIL"
assert assessment2_pii[0]["type"] == "CREDIT_DEBIT_CARD_NUMBER"
assert assessment2_pii[1]["type"] == "ADDRESS"
print("Multiple assessments redaction test passed")
@pytest.mark.asyncio
async def test_bedrock_guardrail_logging_uses_redacted_response():
"""Debug logs and standard_logging payloads must not include raw match values."""
# Create proper mock objects
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
# Mock the Bedrock API response with PII
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "Hello, my phone number is {PHONE}"}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "PHONE",
"match": "+1 412 555 1212", # This should be redacted in logs
"action": "ANONYMIZED",
}
]
}
}
],
}
request_data = {
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "Hello, my phone number is +1 412 555 1212"},
],
}
# Mock AWS credentials to avoid credential loading issues in CI
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
# Mock AWS-related methods to ensure test runs without external dependencies
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch("litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails.verbose_proxy_logger.debug") as mock_debug,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")) as mock_load_creds,
patch.object(guardrail, "_prepare_request", return_value=MagicMock()) as mock_prepare_request,
):
mock_post.return_value = mock_bedrock_response
# Call the method that should log the redacted response
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=request_data.get("messages"),
request_data=request_data,
)
# Verify that debug logging was called
mock_debug.assert_called()
# Get the logged response (second argument to debug call)
logged_calls = mock_debug.call_args_list
bedrock_response_log_call = None
for call in logged_calls:
args, kwargs = call
if len(args) >= 2 and "Bedrock AI response" in str(args[0]):
bedrock_response_log_call = call
break
assert bedrock_response_log_call is not None, "Should have logged Bedrock AI response"
# Extract the logged response data
logged_response = bedrock_response_log_call[0][1] # Second argument to debug call
# Verify that the logged response has redacted PII
assert (
logged_response["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"][0]["match"] == "[REDACTED]"
)
# Verify other fields are preserved
assert logged_response["action"] == "GUARDRAIL_INTERVENED"
assert logged_response["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"][0]["type"] == "PHONE"
slg_list = request_data["metadata"]["standard_logging_guardrail_information"]
assert (
slg_list[0]["guardrail_response"]["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"][0]["match"]
== "[REDACTED]"
)
print("Bedrock guardrail logging redaction test passed")
@pytest.mark.asyncio
async def test_bedrock_guardrail_original_response_not_modified():
"""Test that the original response is not modified by redaction, only the logged version"""
# Create proper mock objects
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
# Mock the Bedrock API response with PII
original_response_data = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "Hello, my phone number is {PHONE}"}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "PHONE",
"match": "+1 412 555 1212", # This should NOT be modified in original
"action": "ANONYMIZED",
}
]
}
}
],
}
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = original_response_data
request_data = {
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "Hello, my phone number is +1 412 555 1212"},
],
}
# Mock AWS credentials to avoid credential loading issues in CI
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
# Mock AWS-related methods to ensure test runs without external dependencies
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")) as mock_load_creds,
patch.object(guardrail, "_prepare_request", return_value=MagicMock()) as mock_prepare_request,
):
mock_post.return_value = mock_bedrock_response
# Call the method
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=request_data.get("messages"),
request_data=request_data,
)
# Verify that the original response data was not modified
# (The json() method should return the original data)
original_data = mock_bedrock_response.json()
assert (
original_data["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"][0]["match"]
== "+1 412 555 1212"
)
# Verify that the returned BedrockGuardrailResponse contains original data
assert result["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"][0]["match"] == "+1 412 555 1212"
print("Original response not modified test passed")
@pytest.mark.asyncio
async def test__redact_pii_matches_preserves_non_pii_entities():
"""Test that _redact_pii_matches only affects PII-related entities and preserves other assessment data"""
response_with_mixed_data = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "EMAIL",
"match": "[email protected]",
"action": "ANONYMIZED",
"confidence": "HIGH",
}
],
"regexes": [
{
"name": "custom_pattern",
"match": "some_pattern_match",
"action": "BLOCKED",
}
],
},
"contentPolicy": {
"filters": [
{
"type": "VIOLENCE",
"confidence": "MEDIUM",
"action": "BLOCKED",
}
]
},
"topicPolicy": {
"topics": [
{
"name": "Restricted Topic",
"type": "DENY",
"action": "BLOCKED",
}
]
},
}
],
"outputs": [{"text": "Content blocked"}],
}
# Call the redaction function
redacted_response = _redact_pii_matches(response_with_mixed_data)
# Verify that PII entity matches are redacted
pii_entities = redacted_response["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"]
assert pii_entities[0]["match"] == "[REDACTED]", "PII match should be redacted"
assert pii_entities[0]["type"] == "EMAIL", "PII type should be preserved"
assert pii_entities[0]["action"] == "ANONYMIZED", "PII action should be preserved"
assert pii_entities[0]["confidence"] == "HIGH", "PII confidence should be preserved"
# Verify that regex matches are also redacted (updated behavior)
regexes = redacted_response["assessments"][0]["sensitiveInformationPolicy"]["regexes"]
assert regexes[0]["match"] == "[REDACTED]", "Regex match should be redacted"
assert regexes[0]["name"] == "custom_pattern", "Regex name should be preserved"
assert regexes[0]["action"] == "BLOCKED", "Regex action should be preserved"
# Verify that other policies are completely unchanged
content_policy = redacted_response["assessments"][0]["contentPolicy"]
assert content_policy["filters"][0]["type"] == "VIOLENCE"
assert content_policy["filters"][0]["confidence"] == "MEDIUM"
topic_policy = redacted_response["assessments"][0]["topicPolicy"]
assert topic_policy["topics"][0]["name"] == "Restricted Topic"
# Verify top-level fields are unchanged
assert redacted_response["action"] == "GUARDRAIL_INTERVENED"
assert redacted_response["outputs"][0]["text"] == "Content blocked"
print("Preserves non-PII entities test passed")
@pytest.mark.asyncio
async def test_pii_redaction_matches_debug_output_format():
"""Test that demonstrates the exact behavior shown in your debug output"""
# This matches the structure from your debug output
original_response = {
"action": "GUARDRAIL_INTERVENED",
"actionReason": "Guardrail blocked.",
"assessments": [
{
"invocationMetrics": {
"guardrailCoverage": {"textCharacters": {"guarded": 84, "total": 84}},
"guardrailProcessingLatency": 322,
"usage": {
"contentPolicyImageUnits": 0,
"contentPolicyUnits": 0,
"contextualGroundingPolicyUnits": 0,
"sensitiveInformationPolicyFreeUnits": 0,
"sensitiveInformationPolicyUnits": 1,
"topicPolicyUnits": 0,
"wordPolicyUnits": 0,
},
},
"sensitiveInformationPolicy": {
"piiEntities": [
{
"action": "BLOCKED",
"detected": True,
"match": "John Smith",
"type": "NAME",
},
{
"action": "BLOCKED",
"detected": True,
"match": "324-12-3212",
"type": "US_SOCIAL_SECURITY_NUMBER",
},
{
"action": "BLOCKED",
"detected": True,
"match": "607-456-7890",
"type": "PHONE",
},
]
},
}
],
"blockedResponse": "Input blocked by PII policy",
"guardrailCoverage": {"textCharacters": {"guarded": 84, "total": 84}},
"output": [{"text": "Input blocked by PII policy"}],
"outputs": [{"text": "Input blocked by PII policy"}],
"usage": {
"contentPolicyImageUnits": 0,
"contentPolicyUnits": 0,
"contextualGroundingPolicyUnits": 0,
"sensitiveInformationPolicyFreeUnits": 0,
"sensitiveInformationPolicyUnits": 1,
"topicPolicyUnits": 0,
"wordPolicyUnits": 0,
},
}
# Apply redaction
redacted_response = _redact_pii_matches(original_response)
# Verify the redacted response matches your expected debug output
pii_entities = redacted_response["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"]
# All PII matches should be redacted
assert pii_entities[0]["match"] == "[REDACTED]", "NAME should be redacted"
assert pii_entities[1]["match"] == "[REDACTED]", "SSN should be redacted"
assert pii_entities[2]["match"] == "[REDACTED]", "PHONE should be redacted"
# But all other fields should be preserved
assert pii_entities[0]["type"] == "NAME"
assert pii_entities[1]["type"] == "US_SOCIAL_SECURITY_NUMBER"
assert pii_entities[2]["type"] == "PHONE"
assert pii_entities[0]["action"] == "BLOCKED"
assert pii_entities[0]["detected"] == True
# Verify that the original response is unchanged
original_pii_entities = original_response["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"]
assert original_pii_entities[0]["match"] == "John Smith", "Original should be unchanged"
assert original_pii_entities[1]["match"] == "324-12-3212", "Original should be unchanged"
assert original_pii_entities[2]["match"] == "607-456-7890", "Original should be unchanged"
# Verify all other metadata is preserved in redacted response
assert redacted_response["action"] == "GUARDRAIL_INTERVENED"
assert redacted_response["actionReason"] == "Guardrail blocked."
assert redacted_response["blockedResponse"] == "Input blocked by PII policy"
assert redacted_response["assessments"][0]["invocationMetrics"]["guardrailProcessingLatency"] == 322
print("PII redaction matches debug output format test passed")
print(f"Original PII values preserved: {[e['match'] for e in original_pii_entities]}")
print(f"Redacted PII values: {[e['match'] for e in pii_entities]}")
@pytest.mark.asyncio
async def test__redact_pii_matches_with_regex_matches():
"""Test redaction of regex matches in sensitive information policy"""
response_with_regex = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"sensitiveInformationPolicy": {
"regexes": [
{
"name": "SSN_PATTERN",
"match": "123-45-6789",
"action": "BLOCKED",
},
{
"name": "CREDIT_CARD_PATTERN",
"match": "4111-1111-1111-1111",
"action": "ANONYMIZED",
},
]
}
}
],
"outputs": [{"text": "Regex patterns detected"}],
}
# Call the redaction function
redacted_response = _redact_pii_matches(response_with_regex)
# Verify that regex matches are redacted
regexes = redacted_response["assessments"][0]["sensitiveInformationPolicy"]["regexes"]
assert regexes[0]["match"] == "[REDACTED]", "SSN regex match should be redacted"
assert regexes[1]["match"] == "[REDACTED]", "Credit card regex match should be redacted"
# Verify other fields are preserved
assert regexes[0]["name"] == "SSN_PATTERN", "Regex name should be preserved"
assert regexes[0]["action"] == "BLOCKED", "Regex action should be preserved"
assert regexes[1]["name"] == "CREDIT_CARD_PATTERN", "Regex name should be preserved"
assert regexes[1]["action"] == "ANONYMIZED", "Regex action should be preserved"
# Verify original response is unchanged
original_regexes = response_with_regex["assessments"][0]["sensitiveInformationPolicy"]["regexes"]
assert original_regexes[0]["match"] == "123-45-6789", "Original should be unchanged"
assert original_regexes[1]["match"] == "4111-1111-1111-1111", "Original should be unchanged"
print("Regex matches redaction test passed")
@pytest.mark.asyncio
async def test__redact_pii_matches_with_custom_words():
"""Test redaction of custom word matches in word policy"""
response_with_custom_words = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"wordPolicy": {
"customWords": [
{
"match": "confidential_data",
"action": "BLOCKED",
},
{
"match": "secret_information",
"action": "ANONYMIZED",
},
]
}
}
],
"outputs": [{"text": "Custom words detected"}],
}
# Call the redaction function
redacted_response = _redact_pii_matches(response_with_custom_words)
# Verify that custom word matches are redacted
custom_words = redacted_response["assessments"][0]["wordPolicy"]["customWords"]
assert custom_words[0]["match"] == "[REDACTED]", "First custom word match should be redacted"
assert custom_words[1]["match"] == "[REDACTED]", "Second custom word match should be redacted"
# Verify other fields are preserved
assert custom_words[0]["action"] == "BLOCKED", "Custom word action should be preserved"
assert custom_words[1]["action"] == "ANONYMIZED", "Custom word action should be preserved"
# Verify original response is unchanged
original_custom_words = response_with_custom_words["assessments"][0]["wordPolicy"]["customWords"]
assert original_custom_words[0]["match"] == "confidential_data", "Original should be unchanged"
assert original_custom_words[1]["match"] == "secret_information", "Original should be unchanged"
print("Custom words redaction test passed")
@pytest.mark.asyncio
async def test__redact_pii_matches_with_managed_words():
"""Test redaction of managed word matches in word policy"""
response_with_managed_words = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"wordPolicy": {
"managedWordLists": [
{
"match": "inappropriate_word",
"action": "BLOCKED",
"type": "PROFANITY",
},
{
"match": "offensive_term",
"action": "ANONYMIZED",
"type": "HATE_SPEECH",
},
]
}
}
],
"outputs": [{"text": "Managed words detected"}],
}
# Call the redaction function
redacted_response = _redact_pii_matches(response_with_managed_words)
# Verify that managed word matches are redacted
managed_words = redacted_response["assessments"][0]["wordPolicy"]["managedWordLists"]
assert managed_words[0]["match"] == "[REDACTED]", "First managed word match should be redacted"
assert managed_words[1]["match"] == "[REDACTED]", "Second managed word match should be redacted"
# Verify other fields are preserved
assert managed_words[0]["action"] == "BLOCKED", "Managed word action should be preserved"
assert managed_words[0]["type"] == "PROFANITY", "Managed word type should be preserved"
assert managed_words[1]["action"] == "ANONYMIZED", "Managed word action should be preserved"
assert managed_words[1]["type"] == "HATE_SPEECH", "Managed word type should be preserved"
# Verify original response is unchanged
original_managed_words = response_with_managed_words["assessments"][0]["wordPolicy"]["managedWordLists"]
assert original_managed_words[0]["match"] == "inappropriate_word", "Original should be unchanged"
assert original_managed_words[1]["match"] == "offensive_term", "Original should be unchanged"
print("Managed words redaction test passed")
@pytest.mark.asyncio
async def test__redact_pii_matches_comprehensive_coverage():
"""Test redaction across all supported policy types in a single response"""
comprehensive_response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "EMAIL",
"match": "[email protected]",
"action": "ANONYMIZED",
}
],
"regexes": [
{
"name": "PHONE_PATTERN",
"match": "555-123-4567",
"action": "BLOCKED",
}
],
},
"wordPolicy": {
"customWords": [
{
"match": "confidential",
"action": "BLOCKED",
}
],
"managedWordLists": [
{
"match": "inappropriate",
"action": "ANONYMIZED",
"type": "PROFANITY",
}
],
},
}
],
"outputs": [{"text": "Multiple policy violations detected"}],
}
# Call the redaction function
redacted_response = _redact_pii_matches(comprehensive_response)
# Verify all match fields are redacted
assessment = redacted_response["assessments"][0]
# PII entities
pii_entities = assessment["sensitiveInformationPolicy"]["piiEntities"]
assert pii_entities[0]["match"] == "[REDACTED]", "PII entity match should be redacted"
# Regex matches
regexes = assessment["sensitiveInformationPolicy"]["regexes"]
assert regexes[0]["match"] == "[REDACTED]", "Regex match should be redacted"
# Custom words
custom_words = assessment["wordPolicy"]["customWords"]
assert custom_words[0]["match"] == "[REDACTED]", "Custom word match should be redacted"
# Managed words
managed_words = assessment["wordPolicy"]["managedWordLists"]
assert managed_words[0]["match"] == "[REDACTED]", "Managed word match should be redacted"
# Verify all other fields are preserved
assert pii_entities[0]["type"] == "EMAIL"
assert regexes[0]["name"] == "PHONE_PATTERN"
assert managed_words[0]["type"] == "PROFANITY"
# Verify original response is unchanged
original_assessment = comprehensive_response["assessments"][0]
assert original_assessment["sensitiveInformationPolicy"]["piiEntities"][0]["match"] == "[email protected]"
assert original_assessment["sensitiveInformationPolicy"]["regexes"][0]["match"] == "555-123-4567"
assert original_assessment["wordPolicy"]["customWords"][0]["match"] == "confidential"
assert original_assessment["wordPolicy"]["managedWordLists"][0]["match"] == "inappropriate"
print("Comprehensive coverage redaction test passed")
@pytest.mark.asyncio
async def test_bedrock_guardrail_respects_custom_runtime_endpoint(monkeypatch):
"""Test that BedrockGuardrail respects aws_bedrock_runtime_endpoint when set"""
# Clear any existing environment variable to ensure clean test
monkeypatch.delenv("AWS_BEDROCK_RUNTIME_ENDPOINT", raising=False)
# Create guardrail with custom runtime endpoint
custom_endpoint = "https://custom-bedrock.example.com"
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
aws_bedrock_runtime_endpoint=custom_endpoint,
)
# Mock credentials
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
# Test data
data = {"source": "INPUT", "content": [{"text": {"text": "test content"}}]}
optional_params = {}
aws_region_name = "us-east-1"
# Mock the _load_credentials method to avoid actual AWS credential loading
with patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, aws_region_name)):
# Call _prepare_request which internally calls get_runtime_endpoint
prepped_request = guardrail._prepare_request(
credentials=mock_credentials,
data=data,
optional_params=optional_params,
aws_region_name=aws_region_name,
)
# Verify that the custom endpoint is used in the URL
expected_url = (
f"{custom_endpoint}/guardrail/{guardrail.guardrailIdentifier}/version/{guardrail.guardrailVersion}/apply"
)
assert prepped_request.url == expected_url, (
f"Expected URL to contain custom endpoint. Got: {prepped_request.url}"
)
print(f"Custom runtime endpoint test passed. URL: {prepped_request.url}")
@pytest.mark.asyncio
async def test_bedrock_guardrail_respects_env_runtime_endpoint(monkeypatch):
"""Test that BedrockGuardrail respects AWS_BEDROCK_RUNTIME_ENDPOINT environment variable"""
custom_endpoint = "https://env-bedrock.example.com"
# Set the environment variable
monkeypatch.setenv("AWS_BEDROCK_RUNTIME_ENDPOINT", custom_endpoint)
# Create guardrail without explicit aws_bedrock_runtime_endpoint
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
# Mock credentials
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
# Test data
data = {"source": "INPUT", "content": [{"text": {"text": "test content"}}]}
optional_params = {}
aws_region_name = "us-east-1"
# Mock the _load_credentials method
with patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, aws_region_name)):
# Call _prepare_request which internally calls get_runtime_endpoint
prepped_request = guardrail._prepare_request(
credentials=mock_credentials,
data=data,
optional_params=optional_params,
aws_region_name=aws_region_name,
)
# Verify that the custom endpoint from environment is used in the URL
expected_url = (
f"{custom_endpoint}/guardrail/{guardrail.guardrailIdentifier}/version/{guardrail.guardrailVersion}/apply"
)
assert prepped_request.url == expected_url, f"Expected URL to contain env endpoint. Got: {prepped_request.url}"
print(f"Environment runtime endpoint test passed. URL: {prepped_request.url}")
@pytest.mark.asyncio
async def test_bedrock_guardrail_uses_default_endpoint_when_no_custom_set(monkeypatch):
"""Test that BedrockGuardrail uses default endpoint when no custom endpoint is set"""
# Ensure no environment variable is set
monkeypatch.delenv("AWS_BEDROCK_RUNTIME_ENDPOINT", raising=False)
# Create guardrail without any custom endpoint
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
# Mock credentials
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
# Test data
data = {"source": "INPUT", "content": [{"text": {"text": "test content"}}]}
optional_params = {}
aws_region_name = "us-west-2"
# Mock the _load_credentials method
with patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, aws_region_name)):
# Call _prepare_request which internally calls get_runtime_endpoint
prepped_request = guardrail._prepare_request(
credentials=mock_credentials,
data=data,
optional_params=optional_params,
aws_region_name=aws_region_name,
)
# Verify that the default endpoint is used
expected_url = f"https://bedrock-runtime.{aws_region_name}.amazonaws.com/guardrail/{guardrail.guardrailIdentifier}/version/{guardrail.guardrailVersion}/apply"
assert prepped_request.url == expected_url, f"Expected default URL. Got: {prepped_request.url}"
print(f"Default endpoint test passed. URL: {prepped_request.url}")
@pytest.mark.asyncio
async def test_bedrock_guardrail_parameter_takes_precedence_over_env(monkeypatch):
"""Test that aws_bedrock_runtime_endpoint parameter takes precedence over environment variable
This test verifies the corrected behavior where the parameter should take precedence
over the environment variable, consistent with the endpoint_url logic.
"""
param_endpoint = "https://param-bedrock.example.com"
env_endpoint = "https://env-bedrock.example.com"
# Set environment variable
monkeypatch.setenv("AWS_BEDROCK_RUNTIME_ENDPOINT", env_endpoint)
# Create guardrail with explicit aws_bedrock_runtime_endpoint
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
aws_bedrock_runtime_endpoint=param_endpoint,
)
# Mock credentials
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
# Test data
data = {"source": "INPUT", "content": [{"text": {"text": "test content"}}]}
optional_params = {}
aws_region_name = "us-east-1"
# Mock the _load_credentials method
with patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, aws_region_name)):
# Call _prepare_request which internally calls get_runtime_endpoint
prepped_request = guardrail._prepare_request(
credentials=mock_credentials,
data=data,
optional_params=optional_params,
aws_region_name=aws_region_name,
)
# Verify that the parameter takes precedence over environment variable
expected_url = (
f"{param_endpoint}/guardrail/{guardrail.guardrailIdentifier}/version/{guardrail.guardrailVersion}/apply"
)
assert prepped_request.url == expected_url, (
f"Expected parameter endpoint to take precedence. Got: {prepped_request.url}"
)
print(f"Parameter precedence test passed. URL: {prepped_request.url}")
@pytest.mark.asyncio
async def test_bedrock_apply_guardrail_with_only_tool_calls_response():
"""Test that apply_guardrail handles response with tool_calls (no text content) without calling Bedrock API"""
# Create a BedrockGuardrail instance
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
# Mock the make_bedrock_api_request method
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api_request:
# Test the apply_guardrail method with tool_calls in response
inputs = {
"texts": [],
"tool_calls": [
{
"id": "call_eFSCWFsyL7MclHYnzKrcQnMK",
"type": "function",
"function": {
"name": "get_weather",
"arguments": '{"location":"São Paulo"}',
},
}
],
}
guardrailed_inputs = await guardrail.apply_guardrail(
inputs=inputs,
request_data={},
input_type="response",
logging_obj=None,
)
# Verify the result - should succeed without errors
assert guardrailed_inputs is not None
assert "tool_calls" in guardrailed_inputs
assert len(guardrailed_inputs["tool_calls"]) == 1
assert guardrailed_inputs["tool_calls"][0]["id"] == "call_eFSCWFsyL7MclHYnzKrcQnMK"
assert guardrailed_inputs["tool_calls"][0]["function"]["name"] == "get_weather"
assert guardrailed_inputs["tool_calls"][0]["function"]["arguments"] == '{"location":"São Paulo"}'
# Verify that the Bedrock API was NOT called since there's no text to process
mock_api_request.assert_not_called()
print("✅ apply_guardrail with tool_calls test passed - no API call made")
def _anthropic_tool_result_conversation(
extra_blocks: tuple[dict[str, str], ...] = (),
) -> list[dict[str, object]]:
"""Anthropic /v1/messages history whose latest user turn is a tool_result follow-up."""
return [
{"role": "user", "content": "What is the weather in Paris?"},
{
"role": "assistant",
"content": [{"type": "tool_use", "id": "toolu_01A", "name": "get_weather", "input": {"city": "Paris"}}],
},
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": "toolu_01A", "content": "18C and sunny"},
*extra_blocks,
],
},
]
@pytest.mark.asyncio
async def test_during_call_hook_skips_bedrock_call_for_tool_result_only_turn():
"""A tool_result-only latest user turn must not post an empty content list to Bedrock.
Regression for `400: At least one GuardrailContentBlock must be provided` on
/v1/messages: with experimental_use_latest_role_message_only the scanned turn is the
Anthropic tool_result block, which carries no text, so ApplyGuardrail rejected the call.
"""
guardrail = BedrockGuardrail(
guardrail_name="bedrock-tool-result",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.during_call,
default_on=True,
experimental_use_latest_role_message_only=True,
)
data = {"model": "claude-sonnet-4-5", "messages": _anthropic_tool_result_conversation()}
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
await guardrail.async_moderation_hook(
data=data,
user_api_key_dict=UserAPIKeyAuth(),
call_type=CallTypes.anthropic_messages.value,
)
mock_post.assert_not_called()
assert data["messages"] == _anthropic_tool_result_conversation()
@pytest.mark.asyncio
async def test_during_call_hook_still_scans_tool_result_turn_carrying_text():
"""The skip must be limited to turns with nothing to scan, never to tool_result turns as such."""
guardrail = BedrockGuardrail(
guardrail_name="bedrock-tool-result-text",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.during_call,
default_on=True,
experimental_use_latest_role_message_only=True,
)
data = {
"model": "claude-sonnet-4-5",
"messages": _anthropic_tool_result_conversation(({"type": "text", "text": "now summarize that"},)),
}
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"action": "NONE", "assessments": []}
with (
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
):
mock_post.return_value = mock_response
await guardrail.async_moderation_hook(
data=data,
user_api_key_dict=UserAPIKeyAuth(),
call_type=CallTypes.anthropic_messages.value,
)
mock_post.assert_called_once()
sent = mock_post.call_args.kwargs["data"].decode()
assert "now summarize that" in sent
# tool_result text is not extracted by this path (https://github.com/BerriAI/litellm/issues/33086)
assert "18C and sunny" not in sent
@pytest.mark.asyncio
async def test_make_apply_guardrail_request_skips_output_scan_without_response_text():
"""A tool-calls-only assistant response yields no OUTPUT content, so it must not be posted."""
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
response = ModelResponse(
choices=[
litellm.Choices(
index=0,
message=litellm.Message(role="assistant", content=None, tool_calls=[]),
finish_reason="tool_calls",
)
]
)
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
bedrock_response = await guardrail.make_bedrock_api_request(source="OUTPUT", response=response)
mock_post.assert_not_called()
assert bedrock_response == {}
@pytest.mark.asyncio
async def test_make_apply_guardrail_request_skips_scan_without_credentials():
"""Skipping happens before credential resolution, so an empty scan costs no AWS work."""
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
with (
patch.object(guardrail, "_load_credentials", side_effect=AssertionError("credentials must not be loaded")),
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
):
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=[{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": "out"}]}],
)
mock_post.assert_not_called()
@pytest.mark.asyncio
async def test_bedrock_apply_guardrail_response_uses_OUTPUT_source():
"""input_type='response' must call Bedrock with source=OUTPUT and assistant content.
Regression: apply_guardrail used to always use source=INPUT. Output-only Bedrock
policies (e.g. PII on model output) then returned action=NONE for non-streaming
completions that go through unified_guardrail -> process_output_response.
"""
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
bedrock_none = {"action": "NONE", "output": [], "outputs": []}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = bedrock_none
await guardrail.apply_guardrail(
inputs={"texts": ["first line", "second line"]},
request_data={"model": "gpt-4o"},
input_type="response",
)
mock_api.assert_called_once()
kwargs = mock_api.call_args.kwargs
assert kwargs["source"] == "OUTPUT"
assert kwargs["request_data"]["model"] == "gpt-4o"
recorded = kwargs["request_data"]["metadata"]["standard_logging_guardrail_information"]
assert [(e["guardrail_name"], e["guardrail_status"]) for e in recorded] == [
(guardrail.guardrail_name, "success")
]
synthetic = kwargs["response"]
assert isinstance(synthetic, ModelResponse)
assert len(synthetic.choices) == 2
assert synthetic.choices[0].message.content == "first line"
assert synthetic.choices[0].message.role == "assistant"
assert synthetic.choices[1].message.content == "second line"
assert synthetic.choices[1].message.role == "assistant"
@pytest.mark.asyncio
async def test_bedrock_apply_guardrail_request_uses_INPUT_source():
"""input_type='request' must call Bedrock with source=INPUT and user messages."""
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
bedrock_none = {"action": "NONE", "output": [], "outputs": []}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = bedrock_none
await guardrail.apply_guardrail(
inputs={"texts": ["user prompt"]},
request_data={},
input_type="request",
)
mock_api.assert_called_once()
kwargs = mock_api.call_args.kwargs
assert kwargs["source"] == "INPUT"
assert kwargs["messages"] is not None
assert len(kwargs["messages"]) == 1
assert kwargs["messages"][0]["role"] == "user"
assert kwargs["messages"][0]["content"] == "user prompt"
assert kwargs.get("response") is None
@pytest.mark.asyncio
async def test_bedrock_guardrail_blocked_content_with_masking_enabled():
"""Test that BLOCKED content raises exception even when masking is enabled
This test verifies the bug fix where previously mask_request_content=True or
mask_response_content=True would bypass all BLOCKED content checks. Now it
properly distinguishes between BLOCKED (raise exception) and ANONYMIZED (apply masking).
"""
# Create guardrail with masking enabled
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
mask_request_content=True, # Masking enabled
mask_response_content=True, # Masking enabled
)
# Mock Bedrock response with BLOCKED content (hate speech)
blocked_response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"contentPolicy": {
"filters": [
{
"type": "HATE",
"confidence": "HIGH",
"action": "BLOCKED", # Should raise exception
}
]
},
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "NAME",
"match": "John Doe",
"action": "ANONYMIZED", # Should be masked
}
]
},
}
],
"outputs": [{"text": "Content blocked due to policy violation"}],
}
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = blocked_response
# Mock credentials
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
request_data = {
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "Test message with PII and hate speech"},
],
}
# Mock AWS-related methods
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = mock_bedrock_response
# Should raise HTTPException for BLOCKED content
with pytest.raises(HTTPException) as exc_info:
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=request_data.get("messages"),
request_data=request_data,
)
# Verify exception details
assert exc_info.value.status_code == 400
assert "Violated guardrail policy" in str(exc_info.value.detail)
print("✅ BLOCKED content with masking enabled raises exception correctly")
# ──────────────────────────────────────────────────────────────────────────────
# Null-safety tests for Bedrock guardrail responses
#
# The Bedrock ApplyGuardrail API can return explicit null/None for list fields
# such as "regexes", "piiEntities", "topics", "filters", "customWords", and
# "managedWordLists" when a particular policy category is present in the
# assessment but has no matches.
#
# Python's dict.get("key", []) returns None (NOT []) when the key exists with
# a None value. The `or []` fallback ensures we always iterate over a list.
#
# Without the fix, iterating over None raises:
# TypeError: 'NoneType' object is not iterable
# which surfaces to callers as:
# openai.InternalServerError: Error code: 500
# {'error': {'message': "Bedrock guardrail failed: 'NoneType' object is not iterable", ...}}
# ──────────────────────────────────────────────────────────────────────────────
class TestRedactPiiMatchesNullSafety:
"""Tests for _redact_pii_matches handling of null/None list fields from Bedrock API."""
@pytest.mark.asyncio
async def test_should_handle_null_regexes_in_sensitive_info_policy(self):
"""Bedrock can return regexes: null while piiEntities has data.
Real-world scenario: guardrail detects PII (e.g. EMAIL) but has no
custom regex patterns configured, so the API returns regexes: null.
"""
response = {
"action": "NONE",
"actionReason": "No action.",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"action": "NONE",
"detected": True,
"match": "[email protected]",
"type": "EMAIL",
}
],
"regexes": None, # Explicit null from Bedrock API
},
}
],
}
# Should not raise TypeError: 'NoneType' object is not iterable
redacted = _redact_pii_matches(response)
# PII match should be redacted
pii = redacted["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"]
assert pii[0]["match"] == "[REDACTED]"
assert pii[0]["type"] == "EMAIL"
@pytest.mark.asyncio
async def test_should_handle_null_pii_entities_in_sensitive_info_policy(self):
"""Bedrock can return piiEntities: null while regexes has data."""
response = {
"action": "NONE",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": None, # null from Bedrock API
"regexes": [
{
"name": "CUSTOM_PATTERN",
"match": "secret-abc-123",
"action": "BLOCKED",
}
],
},
}
],
}
redacted = _redact_pii_matches(response)
regexes = redacted["assessments"][0]["sensitiveInformationPolicy"]["regexes"]
assert regexes[0]["match"] == "[REDACTED]"
@pytest.mark.asyncio
async def test_should_handle_null_custom_words_and_managed_words(self):
"""Bedrock can return null for customWords and managedWordLists in wordPolicy."""
response = {
"action": "NONE",
"assessments": [
{
"wordPolicy": {
"customWords": None, # null from Bedrock API
"managedWordLists": None, # null from Bedrock API
},
}
],
}
# Should not raise TypeError
redacted = _redact_pii_matches(response)
# Values should remain None (no crash)
assert redacted["assessments"][0]["wordPolicy"]["customWords"] is None
assert redacted["assessments"][0]["wordPolicy"]["managedWordLists"] is None
@pytest.mark.asyncio
async def test_should_handle_null_assessments_list(self):
"""Bedrock can return assessments: null."""
response = {
"action": "NONE",
"assessments": None, # null from Bedrock API
}
# Should not raise TypeError
redacted = _redact_pii_matches(response)
assert redacted["assessments"] is None
@pytest.mark.asyncio
async def test_should_handle_all_null_policy_sub_lists_together(self):
"""All sub-list fields are null at the same time — worst-case scenario."""
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": None,
"regexes": None,
},
"wordPolicy": {
"customWords": None,
"managedWordLists": None,
},
"topicPolicy": None,
"contentPolicy": None,
"contextualGroundingPolicy": None,
}
],
}
# Should not raise any exception
redacted = _redact_pii_matches(response)
assert redacted is not None
class TestShouldRaiseGuardrailBlockedExceptionNullSafety:
"""Tests for _should_raise_guardrail_blocked_exception handling of null list fields."""
def _create_guardrail(self) -> BedrockGuardrail:
return BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
@pytest.mark.asyncio
async def test_should_handle_all_null_policy_sub_lists(self):
"""All policy sub-lists are null — should not crash, should return False."""
guardrail = self._create_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"topicPolicy": {
"topics": None, # null from Bedrock API
},
"contentPolicy": {
"filters": None, # null
},
"wordPolicy": {
"customWords": None, # null
"managedWordLists": None, # null
},
"sensitiveInformationPolicy": {
"piiEntities": None, # null
"regexes": None, # null
},
"contextualGroundingPolicy": {
"filters": None, # null
},
}
],
}
# No BLOCKED actions found (all lists null) → should return False
result = guardrail._should_raise_guardrail_blocked_exception(response)
assert result is False
@pytest.mark.asyncio
async def test_should_detect_blocked_despite_other_null_lists(self):
"""A mix of null lists and a real BLOCKED action — should still detect it."""
guardrail = self._create_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"topicPolicy": {
"topics": None, # null — should not crash
},
"contentPolicy": {
"filters": [
{
"type": "HATE",
"confidence": "HIGH",
"action": "BLOCKED",
}
],
},
"wordPolicy": {
"customWords": None, # null
"managedWordLists": None, # null
},
"sensitiveInformationPolicy": {
"piiEntities": None, # null
"regexes": None, # null
},
"contextualGroundingPolicy": None, # entire policy is null
}
],
}
# Should return True because contentPolicy has a BLOCKED filter
result = guardrail._should_raise_guardrail_blocked_exception(response)
assert result is True
@pytest.mark.asyncio
async def test_should_handle_null_assessments_list(self):
"""assessments itself is null — should return False."""
guardrail = self._create_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": None, # null from Bedrock API
}
result = guardrail._should_raise_guardrail_blocked_exception(response)
assert result is False
@pytest.mark.asyncio
async def test_should_handle_null_topics_with_blocked_word_policy(self):
"""topics is null but wordPolicy has a BLOCKED customWord."""
guardrail = self._create_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"topicPolicy": {
"topics": None,
},
"wordPolicy": {
"customWords": [{"match": "badword", "action": "BLOCKED"}],
"managedWordLists": None,
},
}
],
}
result = guardrail._should_raise_guardrail_blocked_exception(response)
assert result is True
@pytest.mark.asyncio
async def test_should_handle_null_pii_with_blocked_regex(self):
"""piiEntities is null but regexes has a BLOCKED match."""
guardrail = self._create_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": None,
"regexes": [{"name": "SSN", "match": "123-45-6789", "action": "BLOCKED"}],
},
}
],
}
result = guardrail._should_raise_guardrail_blocked_exception(response)
assert result is True
@pytest.mark.asyncio
async def test_should_handle_null_grounding_filters(self):
"""contextualGroundingPolicy.filters is null — should not crash."""
guardrail = self._create_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"contextualGroundingPolicy": {
"filters": None,
},
}
],
}
result = guardrail._should_raise_guardrail_blocked_exception(response)
assert result is False
@pytest.mark.asyncio
async def test_should_not_crash_when_action_is_not_intervened(self):
"""If action != GUARDRAIL_INTERVENED, null lists should never be reached."""
guardrail = self._create_guardrail()
response = {
"action": "NONE",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": None,
"regexes": None,
},
}
],
}
result = guardrail._should_raise_guardrail_blocked_exception(response)
assert result is False
class TestApplyGuardrailNullSafety:
"""Tests for apply_guardrail handling of null/None texts input."""
@pytest.mark.asyncio
async def test_should_handle_none_texts_in_inputs(self):
"""inputs[\"texts\"] is explicitly None — should not crash."""
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
inputs = {"texts": None} # Explicit None
mock_credentials = MagicMock()
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(
guardrail,
"_load_credentials",
return_value=(mock_credentials, "us-east-1"),
),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
# With empty texts (from None → []), no Bedrock API call should be made
result = await guardrail.apply_guardrail(
inputs=inputs,
request_data={},
input_type="request",
)
# Should return empty texts without crashing
assert result.get("texts") == []
# No Bedrock API call should be made for empty input
mock_post.assert_not_called()
@pytest.mark.asyncio
async def test_should_handle_missing_texts_key(self):
"""inputs has no \"texts\" key at all — should not crash."""
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
inputs = {} # No "texts" key
mock_credentials = MagicMock()
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(
guardrail,
"_load_credentials",
return_value=(mock_credentials, "us-east-1"),
),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
result = await guardrail.apply_guardrail(
inputs=inputs,
request_data={},
input_type="request",
)
assert result.get("texts") == []
mock_post.assert_not_called()
@pytest.mark.asyncio
async def test_bedrock_guardrail_blocked_vs_anonymized_actions():
"""Test that BLOCKED actions raise exceptions but ANONYMIZED actions do not"""
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
# Test 1: ANONYMIZED action should NOT raise exception
anonymized_response = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "Hello, my phone number is {PHONE}"}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "PHONE",
"match": "+1 412 555 1212",
"action": "ANONYMIZED",
}
]
}
}
],
}
should_raise = guardrail._should_raise_guardrail_blocked_exception(anonymized_response)
assert should_raise is False, "ANONYMIZED actions should not raise exceptions"
# Test 2: BLOCKED action should raise exception
blocked_response = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "I can't provide that information."}],
"assessments": [
{"topicPolicy": {"topics": [{"name": "Sensitive Topic", "type": "DENY", "action": "BLOCKED"}]}}
],
}
should_raise = guardrail._should_raise_guardrail_blocked_exception(blocked_response)
assert should_raise is True, "BLOCKED actions should raise exceptions"
# Test 3: Mixed actions - should raise if ANY action is BLOCKED
mixed_response = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "I can't provide that information."}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "PHONE",
"match": "+1 412 555 1212",
"action": "ANONYMIZED",
}
]
},
"topicPolicy": {"topics": [{"name": "Blocked Topic", "type": "DENY", "action": "BLOCKED"}]},
}
],
}
should_raise = guardrail._should_raise_guardrail_blocked_exception(mixed_response)
assert should_raise is True, "Mixed actions with any BLOCKED should raise exceptions"
# Test 4: NONE action should not raise exception
none_response = {
"action": "NONE",
"outputs": [],
"assessments": [],
}
should_raise = guardrail._should_raise_guardrail_blocked_exception(none_response)
assert should_raise is False, "NONE action should not raise exceptions"
print("\u2705 BLOCKED vs ANONYMIZED actions test passed")
# ---------------------------------------------------------------------------
# Spend logs: guardrail_mode (pre/during/post) vs Bedrock INPUT/OUTPUT
# ---------------------------------------------------------------------------
def test_bedrock_guardrail_uses_native_during_call_hook():
"""during_call must use async_moderation_hook, not unified apply_guardrail(input=request)."""
assert BedrockGuardrail.use_native_during_call_hook is True
@pytest.mark.asyncio
async def test_make_bedrock_api_request_logging_event_type_for_spend_logs():
"""
Spend/UI use event_type from the proxy hook, not Bedrock's INPUT/OUTPUT alone.
When logging_event_type is set, it must be forwarded to standard guardrail logging.
When omitted, INPUT maps to pre_call (legacy).
"""
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "NONE",
"assessments": [
{"sensitiveInformationPolicy": {"piiEntities": [{"type": "NAME", "match": "GG", "action": "BLOCKED"}]}}
],
}
request_data = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "hi"}],
}
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch.object(
guardrail,
"add_standard_logging_guardrail_information_to_request_data",
) as mock_log,
):
mock_post.return_value = mock_bedrock_response
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=request_data["messages"],
request_data=request_data,
logging_event_type=GuardrailEventHooks.during_call,
)
assert mock_log.call_args.kwargs["event_type"] == GuardrailEventHooks.during_call
# Raw Bedrock JSON is forwarded; redaction runs once in
# CustomGuardrail.add_standard_logging_guardrail_information_to_request_data.
assert (
mock_log.call_args.kwargs["guardrail_json_response"]["assessments"][0]["sensitiveInformationPolicy"][
"piiEntities"
][0]["match"]
== "GG"
)
mock_log.reset_mock()
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=request_data["messages"],
request_data=request_data,
)
assert mock_log.call_args.kwargs["event_type"] == GuardrailEventHooks.pre_call
@pytest.mark.asyncio
async def test_make_bedrock_api_request_filters_dynamic_evaluation_overrides():
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {"action": "NONE", "assessments": []}
prepared_request = MagicMock()
prepared_request.url = "https://bedrock.test/apply"
prepared_request.body = b"{}"
prepared_request.headers = {}
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=prepared_request) as mock_prepare_request,
patch.object(
guardrail,
"get_guardrail_dynamic_request_body_params",
return_value={
"content": [{"text": {"text": "benign replacement"}}],
"source": "OUTPUT",
"outputScope": "FULL",
},
),
):
mock_post.return_value = mock_bedrock_response
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=[{"role": "user", "content": "actual prompt"}],
request_data={"model": "gpt-4o"},
)
prepared_data = mock_prepare_request.call_args.kwargs["data"]
assert prepared_data["source"] == "INPUT"
assert "actual prompt" in json.dumps(prepared_data["content"])
assert "benign replacement" not in json.dumps(prepared_data["content"])
assert prepared_data["outputScope"] == "FULL"
@pytest.mark.asyncio
async def test_during_call_hook_invokes_bedrock_async_moderation_hook():
"""
Bedrock sets use_native_during_call_hook so ProxyLogging runs the real
async_moderation_hook (unified apply_guardrail would log INPUT as pre_call).
"""
cache = DualCache()
proxy_logging = ProxyLogging(user_api_key_cache=cache)
guardrail = BedrockGuardrail(
guardrail_name="bedrock-during-test",
guardrailIdentifier="gid",
guardrailVersion="1",
event_hook=GuardrailEventHooks.during_call,
default_on=True,
)
mock_mod = AsyncMock(return_value=None)
original_callbacks = litellm.callbacks.copy() if litellm.callbacks else []
try:
litellm.callbacks = [guardrail]
with patch.object(guardrail, "async_moderation_hook", new=mock_mod):
await proxy_logging.during_call_hook(
data={
"model": "gpt-4",
"messages": [{"role": "user", "content": "test"}],
},
user_api_key_dict=UserAPIKeyAuth(api_key="test_key", user_id="test_user"),
call_type="completion",
)
finally:
litellm.callbacks = original_callbacks
mock_mod.assert_awaited_once()
# ---------------------------------------------------------------------------
# L3: _extract_blocked_assessments + _get_http_exception_for_blocked_guardrail
# Regression coverage for case 2026-04-10-internal-bedrock-guardrail-streaming-error.
# ---------------------------------------------------------------------------
def _make_guardrail() -> BedrockGuardrail:
return BedrockGuardrail(
guardrail_name="bedrock-pii-guard",
guardrailIdentifier="amgllac6xf3r",
guardrailVersion="1",
)
def test_extract_blocked_assessments_pii_entity():
"""L3: PII entity match (BLOCKED) is surfaced with category, type, and match."""
g = _make_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{"type": "NAME", "action": "BLOCKED", "match": "Jack"},
{"type": "EMAIL", "action": "ANONYMIZED", "match": "[email protected]"},
]
}
}
],
}
blocked = g._extract_blocked_assessments(response)
assert len(blocked) == 1
assert blocked[0]["policy"] == "sensitiveInformationPolicy"
matches = blocked[0]["matches"]
assert len(matches) == 1 # only the BLOCKED one is surfaced
assert matches[0]["category"] == "piiEntities"
assert matches[0]["type"] == "NAME"
assert matches[0]["match"] == "Jack"
def test_extract_blocked_assessments_multiple_policies():
"""L3: multiple policies fired in one assessment must all be reported."""
g = _make_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"topicPolicy": {"topics": [{"name": "Investment", "type": "DENY", "action": "BLOCKED"}]},
"contentPolicy": {
"filters": [
{
"type": "VIOLENCE",
"confidence": "HIGH",
"filterStrength": "HIGH",
"action": "BLOCKED",
}
]
},
"wordPolicy": {"customWords": [{"match": "forbidden", "action": "BLOCKED"}]},
}
],
}
blocked = g._extract_blocked_assessments(response)
policies = {entry["policy"] for entry in blocked}
assert policies == {"topicPolicy", "contentPolicy", "wordPolicy"}
def test_extract_blocked_assessments_only_anonymized_returns_empty():
"""L3: if all matches are ANONYMIZED (not BLOCKED), the list is empty."""
g = _make_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{"sensitiveInformationPolicy": {"piiEntities": [{"type": "NAME", "action": "ANONYMIZED", "match": "Jack"}]}}
],
}
assert g._extract_blocked_assessments(response) == []
def test_extract_blocked_assessments_no_assessments():
"""L3: response with no assessments returns an empty list, not an error."""
g = _make_guardrail()
assert g._extract_blocked_assessments({"action": "NONE"}) == []
assert g._extract_blocked_assessments({"assessments": None}) == []
def test_get_http_exception_includes_assessments_and_identifier():
"""L3: end-to-end — _get_http_exception_for_blocked_guardrail emits the new fields."""
g = _make_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "Sorry, the model cannot answer this question."}],
"assessments": [
{"sensitiveInformationPolicy": {"piiEntities": [{"type": "NAME", "action": "BLOCKED", "match": "Jack"}]}}
],
}
exc = g._get_http_exception_for_blocked_guardrail(response)
assert isinstance(exc, HTTPException)
assert exc.status_code == 400
assert exc.detail["error"] == "Violated guardrail policy"
assert exc.detail["bedrock_guardrail_response"] == "Sorry, the model cannot answer this question."
assert exc.detail["guardrailIdentifier"] == "amgllac6xf3r"
assert exc.detail["guardrailVersion"] == "1"
assert exc.detail["assessments"][0]["policy"] == "sensitiveInformationPolicy"
assert exc.detail["assessments"][0]["matches"][0]["type"] == "NAME"
assert exc.detail["assessments"][0]["matches"][0]["match"] == "[REDACTED]"
def test_extract_violation_category_names_mixed_policies():
"""Topic names, content-filter types, PII types, and managed-word types
flatten into a single category-name list — using only the operator-
defined `name`/`type` labels."""
g = _make_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"topicPolicy": {
"topics": [
{"name": "Fiduciary Advice", "action": "BLOCKED"},
{"name": "Tax Advice", "action": "BLOCKED"},
]
},
"contentPolicy": {"filters": [{"type": "VIOLENCE", "action": "BLOCKED"}]},
"wordPolicy": {
"managedWordLists": [{"type": "PROFANITY", "action": "BLOCKED"}],
},
"sensitiveInformationPolicy": {"piiEntities": [{"type": "EMAIL", "action": "BLOCKED"}]},
}
],
}
names = g._extract_violation_category_names(response)
assert "Fiduciary Advice" in names
assert "Tax Advice" in names
assert "VIOLENCE" in names
assert "PROFANITY" in names
assert "EMAIL" in names
def test_extract_violation_category_names_does_not_leak_user_input():
"""SECURITY: customWords.match is the raw user-submitted word that
triggered the rule, and an unnamed regex match is the actual sensitive
value (e.g. a credit-card number). Neither must appear in
violation_categories — otherwise the content the guardrail blocked
leaks straight into telemetry backends."""
g = _make_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"wordPolicy": {
"customWords": [{"match": "secret-codeword-abc-123", "action": "BLOCKED"}],
},
"sensitiveInformationPolicy": {"regexes": [{"match": "4111-1111-1111-1111", "action": "BLOCKED"}]},
}
],
}
names = g._extract_violation_category_names(response)
assert "secret-codeword-abc-123" not in names
assert "4111-1111-1111-1111" not in names
assert names == []
def test_extract_violation_category_names_named_regex_uses_name():
"""A regex with a `name` field surfaces that operator-defined label
(safe to log), not the matched value."""
g = _make_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"sensitiveInformationPolicy": {
"regexes": [
{
"name": "credit-card-pattern",
"match": "4111-1111-1111-1111",
"action": "BLOCKED",
}
]
}
}
],
}
names = g._extract_violation_category_names(response)
assert names == ["credit-card-pattern"]
def test_extract_violation_category_names_skips_anonymized():
"""ANONYMIZED entries are not blocks — they must not contribute to the
violation_categories list."""
g = _make_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [{"sensitiveInformationPolicy": {"piiEntities": [{"type": "NAME", "action": "ANONYMIZED"}]}}],
}
assert g._extract_violation_category_names(response) == []
def test_extract_violation_category_names_no_assessments():
"""Empty / missing assessments → empty list, not an error."""
g = _make_guardrail()
assert g._extract_violation_category_names({"action": "NONE"}) == []
assert g._extract_violation_category_names({"assessments": None}) == []
@pytest.mark.asyncio
async def test_make_bedrock_api_request_forwards_guardrail_action():
"""Bedrock's top-level ``action`` string must be propagated through
``tracing_detail`` so downstream loggers (OTEL, ...) can surface the
raw provider verdict as a queryable attribute without re-parsing the
redacted guardrail_response blob."""
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [{"topicPolicy": {"topics": [{"name": "Fiduciary Advice", "action": "BLOCKED"}]}}],
}
request_data = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "hi"}],
}
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch.object(
guardrail,
"add_standard_logging_guardrail_information_to_request_data",
) as mock_log,
patch.object(
guardrail,
"_get_http_exception_for_blocked_guardrail",
return_value=Exception("blocked"),
),
):
mock_post.return_value = mock_bedrock_response
with pytest.raises(Exception, match="blocked"):
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=request_data["messages"],
request_data=request_data,
)
tracing_detail = mock_log.call_args.kwargs["tracing_detail"]
assert tracing_detail is not None
assert tracing_detail["guardrail_action"] == "GUARDRAIL_INTERVENED"
@pytest.mark.asyncio
async def test_make_bedrock_api_request_omits_guardrail_action_when_missing():
"""If the Bedrock response omits ``action`` (older / partial payloads),
the field must be left off ``tracing_detail`` rather than written as
``None`` — downstream code expects strings or absence, not nulls."""
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {"assessments": []}
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch.object(
guardrail,
"add_standard_logging_guardrail_information_to_request_data",
) as mock_log,
):
mock_post.return_value = mock_bedrock_response
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=[{"role": "user", "content": "hi"}],
request_data={"model": "gpt-4o", "messages": []},
)
tracing_detail = mock_log.call_args.kwargs["tracing_detail"]
# No violation categories and no action ⇒ tracing_detail stays None
# (the hook collapses an empty dict before forwarding).
if tracing_detail is not None:
assert "guardrail_action" not in tracing_detail
def test_get_http_exception_no_blocked_assessments_omits_field():
"""L3: when no assessments are blocked, the `assessments` key is omitted entirely."""
g = _make_guardrail()
response = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "blocked"}],
"assessments": [
{"sensitiveInformationPolicy": {"piiEntities": [{"type": "NAME", "action": "ANONYMIZED", "match": "Jack"}]}}
],
}
exc = g._get_http_exception_for_blocked_guardrail(response)
assert isinstance(exc, HTTPException)
assert "assessments" not in exc.detail
assert exc.detail["guardrailIdentifier"] == "amgllac6xf3r"
@pytest.mark.asyncio
async def test_streaming_post_call_only_runs_output_scan():
"""
async_post_call_streaming_iterator_hook must pass request_data into OUTPUT
make_bedrock_api_request so spend/standard_logging attaches to the real request
(Greptile: previously OUTPUT used request_data=None / ephemeral {}).
post_call only validates the response — no INPUT scan should run here.
"""
request_data = {
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "hi"}],
"metadata": {"stream_guardrail_logging": True},
}
guardrail = BedrockGuardrail(
guardrail_name="bedrock-stream-reqdata",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
)
mock_chunks = [
litellm.ModelResponseStream(
id="tid",
choices=[
litellm.types.utils.StreamingChoices(
delta=litellm.types.utils.Delta(content="Hi", role="assistant"),
finish_reason=None,
index=0,
)
],
created=1,
model="gpt-4o-mini",
object="chat.completion.chunk",
),
litellm.ModelResponseStream(
id="tid",
choices=[
litellm.types.utils.StreamingChoices(
delta=litellm.types.utils.Delta(content="!", role="assistant"),
finish_reason="stop",
index=0,
)
],
created=1,
model="gpt-4o-mini",
object="chat.completion.chunk",
),
]
async def mock_stream():
for c in mock_chunks:
yield c
minimal = {"action": "NONE", "assessments": [], "outputs": []}
with patch.object(guardrail, "make_bedrock_api_request", AsyncMock(return_value=minimal)) as mock_make:
out = []
async for chunk in guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(),
response=mock_stream(),
request_data=request_data,
):
out.append(chunk)
assert len(out) >= 1
output_calls = [c for c in mock_make.call_args_list if c.kwargs.get("source") == "OUTPUT"]
assert len(output_calls) == 1
assert output_calls[0].kwargs.get("request_data") is request_data
assert output_calls[0].kwargs.get("logging_event_type") == GuardrailEventHooks.post_call
input_calls = [c for c in mock_make.call_args_list if c.kwargs.get("source") == "INPUT"]
assert len(input_calls) == 0
@pytest.mark.asyncio
async def test_streaming_post_call_output_only_path_passes_request_data_to_make_bedrock():
"""When INPUT validation is skipped (pre/during already ran), OUTPUT still gets request_data."""
request_data = {
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "hi"}],
}
guardrail = BedrockGuardrail(
guardrail_name="bedrock-stream-out-only",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.during_call,
default_on=True,
)
mock_chunks = [
litellm.ModelResponseStream(
id="tid",
choices=[
litellm.types.utils.StreamingChoices(
delta=litellm.types.utils.Delta(content="x", role="assistant"),
finish_reason="stop",
index=0,
)
],
created=1,
model="gpt-4o-mini",
object="chat.completion.chunk",
),
]
async def mock_stream():
for c in mock_chunks:
yield c
minimal = {"action": "NONE", "assessments": [], "outputs": []}
with patch.object(guardrail, "make_bedrock_api_request", AsyncMock(return_value=minimal)) as mock_make:
async for _ in guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(),
response=mock_stream(),
request_data=request_data,
):
pass
assert mock_make.call_count == 1
c = mock_make.call_args
assert c.kwargs.get("source") == "OUTPUT"
assert c.kwargs.get("request_data") is request_data
# ---------------------------------------------------------------------------
# Regression: post_call only validates OUTPUT.
# Input scanning belongs to pre_call / during_call. Running an extra INPUT
# scan here used to produce a duplicate "post-call" trace entry and made no
# semantic sense for a "post-call" event.
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_post_call_success_hook_only_runs_output_scan():
"""
With only `post_call` configured, async_post_call_success_hook must call
make_bedrock_api_request exactly once with source="OUTPUT". An INPUT call
here would produce a duplicate post-call log entry.
"""
guardrail = BedrockGuardrail(
guardrail_name="bedrock-post-pii",
guardrailIdentifier="gid",
guardrailVersion="1",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
)
request_data = {
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "hi"}],
"metadata": {},
}
response = ModelResponse(
choices=[
litellm.Choices(
message=litellm.Message(role="assistant", content="hello"),
index=0,
finish_reason="stop",
)
],
model="gpt-4o-mini",
)
minimal = {"action": "NONE", "assessments": [], "outputs": []}
with patch.object(guardrail, "make_bedrock_api_request", AsyncMock(return_value=minimal)) as mock_make:
await guardrail.async_post_call_success_hook(
data=request_data,
user_api_key_dict=UserAPIKeyAuth(),
response=response,
)
sources = [c.kwargs.get("source") for c in mock_make.call_args_list]
assert sources == ["OUTPUT"]
assert mock_make.call_args.kwargs.get("logging_event_type") == GuardrailEventHooks.post_call
# ---------------------------------------------------------------------------
# Contextual grounding: request-side qualifiers
# ---------------------------------------------------------------------------
#
# Bedrock contextual grounding tags each ApplyGuardrail content block with a
# `qualifiers` array (grounding_source / query / guard_content). A caller marks
# message content blocks `{"type": "grounding_source", ...}` / `{"type": "query", ...}`;
# at post_call the hook assembles one source="OUTPUT" call carrying the source +
# query + the model response (as guard_content). A request without these tags
# produces the plain-text payload with no qualifiers.
_GROUNDING_SOURCE_TEXT = "Tokyo is the capital of Japan."
_GROUNDING_QUERY_TEXT = "What is the capital of Japan?"
_GROUNDING_RESPONSE_TEXT = "The capital of Japan is Tokyo."
def _grounding_guardrail(from_messages: bool = False) -> BedrockGuardrail:
return BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
contextual_grounding_from_messages=from_messages,
)
def _grounding_messages() -> list:
return [
{
"role": "system",
"content": [{"type": "grounding_source", "text": _GROUNDING_SOURCE_TEXT}],
},
{
"role": "user",
"content": [{"type": "query", "text": _GROUNDING_QUERY_TEXT}],
},
]
def _model_response(content: str) -> ModelResponse:
from litellm.types.utils import Choices, Message, ModelResponse
return ModelResponse(
choices=[
Choices(
index=0,
message=Message(role="assistant", content=content),
finish_reason="stop",
)
]
)
# Expected OUTPUT content blocks, keyed by their grounding qualifier, so the
# per-test assertions read as the block sequence they expect.
_GROUNDING_SOURCE_BLOCK = {"text": {"text": _GROUNDING_SOURCE_TEXT, "qualifiers": ["grounding_source"]}}
_QUERY_BLOCK = {"text": {"text": _GROUNDING_QUERY_TEXT, "qualifiers": ["query"]}}
_GUARD_BLOCK = {"text": {"text": _GROUNDING_RESPONSE_TEXT, "qualifiers": ["guard_content"]}}
def _input_request(messages: list) -> dict:
"""Arrange a guardrail and act: build the Bedrock INPUT payload."""
return _grounding_guardrail().convert_to_bedrock_format(source="INPUT", messages=messages)
def _output_request(messages: list, response=None, from_messages: bool = False) -> dict:
"""Arrange a guardrail and act: build the Bedrock OUTPUT payload."""
return _grounding_guardrail(from_messages).convert_to_bedrock_format(
source="OUTPUT", response=response, messages=messages
)
def test_grounding_input_strips_grounding_and_query_qualifiers():
"""Grounding is OUTPUT-only: tagged source/query reach Bedrock as plain text on an
INPUT scan, so a tag cannot change how input-safety policies scan content (no bypass).
"""
expected_request = {
"source": "INPUT",
"content": [
{"text": {"text": _GROUNDING_SOURCE_TEXT}},
{"text": {"text": _GROUNDING_QUERY_TEXT}},
],
}
actual_request = _input_request(_grounding_messages())
assert actual_request == expected_request
def test_grounding_input_leaves_existing_guarded_text_unqualified():
"""An existing guarded_text input block keeps its legacy unqualified payload."""
expected_request = {"source": "INPUT", "content": [{"text": {"text": "policy"}}]}
actual_request = _input_request([{"role": "user", "content": [{"type": "guarded_text", "text": "policy"}]}])
assert actual_request == expected_request
def test_grounding_output_assembles_source_query_and_response():
"""OUTPUT emits grounding_source + query (from the request) then the response as
guard_content, so Bedrock can grade the response against the source and query."""
expected_request = {
"source": "OUTPUT",
"content": [_GROUNDING_SOURCE_BLOCK, _QUERY_BLOCK, _GUARD_BLOCK],
}
actual_request = _output_request(_grounding_messages(), _model_response(_GROUNDING_RESPONSE_TEXT))
assert actual_request == expected_request
def test_grounding_output_keeps_legacy_payload_without_tags():
"""Without grounding tags the OUTPUT payload is the legacy single response block."""
expected_request = {
"source": "OUTPUT",
"content": [{"text": {"text": "Hi there."}}],
}
actual_request = _output_request([{"role": "user", "content": "hello"}], _model_response("Hi there."))
assert actual_request == expected_request
def test_grounding_output_derives_source_and_query_from_plain_messages():
"""Flag on: untagged system + user text is sent as grounding_source + query."""
messages = [
{"role": "system", "content": _GROUNDING_SOURCE_TEXT},
{"role": "user", "content": _GROUNDING_QUERY_TEXT},
]
expected_request = {
"source": "OUTPUT",
"content": [_GROUNDING_SOURCE_BLOCK, _QUERY_BLOCK, _GUARD_BLOCK],
}
actual_request = _output_request(messages, _model_response(_GROUNDING_RESPONSE_TEXT), from_messages=True)
assert actual_request == expected_request
def test_grounding_output_plain_messages_stay_legacy_when_flag_is_off():
"""Default config: plain system + user text is never sent as grounding context."""
messages = [
{"role": "system", "content": _GROUNDING_SOURCE_TEXT},
{"role": "user", "content": _GROUNDING_QUERY_TEXT},
]
expected_request = {"source": "OUTPUT", "content": [{"text": {"text": _GROUNDING_RESPONSE_TEXT}}]}
actual_request = _output_request(messages, _model_response(_GROUNDING_RESPONSE_TEXT))
assert actual_request == expected_request
def test_grounding_output_derived_query_is_latest_user_turn_only():
"""Only the latest user turn is the query; system and developer turns are the source."""
developer_text = "Answer in one sentence."
messages = [
{"role": "system", "content": _GROUNDING_SOURCE_TEXT},
{"role": "developer", "content": [{"type": "text", "text": developer_text}]},
{"role": "user", "content": "Hi"},
{"role": "assistant", "content": "Hello, how can I help?"},
{"role": "user", "content": _GROUNDING_QUERY_TEXT},
]
expected_request = {
"source": "OUTPUT",
"content": [
_GROUNDING_SOURCE_BLOCK,
{"text": {"text": developer_text, "qualifiers": ["grounding_source"]}},
_QUERY_BLOCK,
_GUARD_BLOCK,
],
}
actual_request = _output_request(messages, _model_response(_GROUNDING_RESPONSE_TEXT), from_messages=True)
assert actual_request == expected_request
@pytest.mark.parametrize(
"messages",
[
pytest.param([{"role": "system", "content": _GROUNDING_SOURCE_TEXT}], id="system-without-user"),
pytest.param(
[
{"role": "tool", "content": _GROUNDING_SOURCE_TEXT, "tool_call_id": "c1"},
{"role": "user", "content": _GROUNDING_QUERY_TEXT},
],
id="tool-result-is-not-a-source",
),
pytest.param(
[
{"role": "system", "content": ""},
{"role": "user", "content": _GROUNDING_QUERY_TEXT},
],
id="empty-system-prompt",
),
pytest.param(
[
{"role": "system", "content": _GROUNDING_SOURCE_TEXT},
{"role": "user", "content": [{"type": "image_url", "image_url": {"url": "https://x.test/a.png"}}]},
],
id="image-only-user-turn",
),
],
)
def test_grounding_output_stays_legacy_when_plain_source_or_query_is_missing(messages):
"""Bedrock rejects a source without a query and vice versa, so send neither."""
expected_request = {"source": "OUTPUT", "content": [{"text": {"text": _GROUNDING_RESPONSE_TEXT}}]}
actual_request = _output_request(messages, _model_response(_GROUNDING_RESPONSE_TEXT), from_messages=True)
assert actual_request == expected_request
def test_grounding_output_explicit_tags_take_precedence_over_plain_messages():
"""Tagged blocks win: untagged text around them is not added as source or query."""
messages = [
{"role": "system", "content": "You are a helpful assistant."},
*_grounding_messages(),
{"role": "user", "content": "Please be brief."},
]
expected_request = {
"source": "OUTPUT",
"content": [_GROUNDING_SOURCE_BLOCK, _QUERY_BLOCK, _GUARD_BLOCK],
}
actual_request = _output_request(messages, _model_response(_GROUNDING_RESPONSE_TEXT), from_messages=True)
assert actual_request == expected_request
def test_grounding_input_ignores_plain_message_derivation():
"""INPUT scans never derive grounding qualifiers from plain messages."""
messages = [
{"role": "system", "content": _GROUNDING_SOURCE_TEXT},
{"role": "user", "content": _GROUNDING_QUERY_TEXT},
]
expected_request = {
"source": "INPUT",
"content": [{"text": {"text": _GROUNDING_SOURCE_TEXT}}, {"text": {"text": _GROUNDING_QUERY_TEXT}}],
}
actual_request = _grounding_guardrail(from_messages=True).convert_to_bedrock_format(
source="INPUT", messages=messages
)
assert actual_request == expected_request
def test_grounding_output_combines_multiple_sources():
"""Every grounding_source block is emitted; Bedrock combines them into one corpus."""
uk_source_text = "London is the capital of UK."
uk_source_block = {"text": {"text": uk_source_text, "qualifiers": ["grounding_source"]}}
messages = [
{
"role": "system",
"content": [
{"type": "grounding_source", "text": uk_source_text},
{"type": "grounding_source", "text": _GROUNDING_SOURCE_TEXT},
],
},
{"role": "user", "content": [{"type": "query", "text": _GROUNDING_QUERY_TEXT}]},
]
expected_request = {
"source": "OUTPUT",
"content": [
uk_source_block,
_GROUNDING_SOURCE_BLOCK,
_QUERY_BLOCK,
_GUARD_BLOCK,
],
}
actual_request = _output_request(messages, _model_response(_GROUNDING_RESPONSE_TEXT))
assert actual_request == expected_request
def test_grounding_output_keeps_grounding_for_non_model_response():
"""Harvested grounding blocks survive a non-ModelResponse output instead of being
silently dropped (regression guard for the unconditional content assignment)."""
expected_request = {
"source": "OUTPUT",
"content": [_GROUNDING_SOURCE_BLOCK, _QUERY_BLOCK],
}
actual_request = _output_request(_grounding_messages(), response=None)
assert actual_request == expected_request
@pytest.mark.parametrize(
"role, is_trusted",
[
("system", True),
("developer", True),
("tool", False),
("function", False),
("user", False),
("assistant", False),
],
)
def test_grounding_source_trusted_only_from_app_roles(role, is_trusted):
"""grounding_source is honored only from app-authored roles (system/developer). A
tag on a user, tool, function or assistant message is ignored, so neither a forwarded
end user nor an externally-influenced tool result can supply fake evidence for the
grounding check to grade the response against; query is always collected."""
messages = [
{
"role": role,
"content": [{"type": "grounding_source", "text": _GROUNDING_SOURCE_TEXT}],
},
{"role": "user", "content": [{"type": "query", "text": _GROUNDING_QUERY_TEXT}]},
]
expected_content = [_QUERY_BLOCK, _GUARD_BLOCK]
if is_trusted:
expected_content = [_GROUNDING_SOURCE_BLOCK, *expected_content]
actual_request = _output_request(messages, _model_response(_GROUNDING_RESPONSE_TEXT))
assert actual_request == {"source": "OUTPUT", "content": expected_content}
@pytest.mark.asyncio
async def test_grounding_output_blocked_raises_400():
"""A BLOCKED contextualGroundingPolicy filter raises HTTP 400."""
guardrail = _grounding_guardrail()
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"contextualGroundingPolicy": {
"filters": [
{
"type": "GROUNDING",
"threshold": 0.7,
"score": 0.1,
"action": "BLOCKED",
}
]
}
}
],
"outputs": [{"text": "Response blocked: not grounded in the provided source."}],
}
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = mock_bedrock_response
with pytest.raises(HTTPException) as exc_info:
await guardrail.make_bedrock_api_request(
source="OUTPUT",
response=_model_response("The capital of Japan is Paris."),
messages=_grounding_messages(),
request_data={"messages": _grounding_messages()},
)
assert exc_info.value.status_code == 400
@pytest.mark.asyncio
@pytest.mark.parametrize(
"from_messages, request_messages",
[
(
True,
[
{"role": "system", "content": _GROUNDING_SOURCE_TEXT},
{"role": "user", "content": _GROUNDING_QUERY_TEXT},
],
),
(
False,
[
{"role": "system", "content": [{"type": "grounding_source", "text": _GROUNDING_SOURCE_TEXT}]},
{"role": "user", "content": [{"type": "query", "text": _GROUNDING_QUERY_TEXT}]},
],
),
],
ids=["plain-messages-flag-on", "tagged-messages-flag-off"],
)
async def test_apply_guardrail_response_forwards_request_messages_for_grounding(from_messages, request_messages):
guardrail = _grounding_guardrail(from_messages=from_messages)
expected_request = {
"source": "OUTPUT",
"content": [_GROUNDING_SOURCE_BLOCK, _QUERY_BLOCK, _GUARD_BLOCK],
}
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()) as mock_prepare,
):
mock_post.return_value = _passing_bedrock_httpx_response(_GROUNDING_RESPONSE_TEXT)
await guardrail.apply_guardrail(
inputs={"texts": [_GROUNDING_RESPONSE_TEXT]},
request_data={"messages": request_messages},
input_type="response",
)
assert mock_prepare.call_count == 1
assert json.loads(json.dumps(mock_prepare.call_args.kwargs["data"])) == expected_request
###############################################################################
# LIT-4186: disable_exception_on_block regression tests
#
# Before the fix, a Bedrock block with disable_exception_on_block=True raised
# GuardrailInterventionNormalStringError, which no proxy code handled: the
# unified pre_call path re-raised it, so the client saw HTTP 500 with the block
# message; the native during_call hook swallowed it and set data["mock_response"],
# which was dead code because route_request already unpacked kwargs.
#
# The fix converts blocks to ModifyResponseException at the raise site inside
# make_bedrock_api_request. That exception is already the industry-standard
# proxy contract (caught in proxy_server.py, anthropic_endpoints, etc.) and
# turns into a 200 response whose content is the block message.
###############################################################################
def _blocked_bedrock_httpx_response() -> MagicMock:
response = MagicMock()
response.status_code = 200
response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "Sorry, the model cannot answer this question."}],
"assessments": [{"topicPolicy": {"topics": [{"name": "Denied", "type": "DENY", "action": "BLOCKED"}]}}],
}
return response
@pytest.mark.asyncio
async def test_make_bedrock_api_request_block_raises_modify_response_when_flag_set():
from litellm.exceptions import ModifyResponseException
guardrail = BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=True,
)
request_data = {"model": "bedrock-nova-micro"}
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _blocked_bedrock_httpx_response()
with pytest.raises(ModifyResponseException) as exc_info:
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=[{"role": "user", "content": "My name is John Doe"}],
request_data=request_data,
)
assert exc_info.value.message == "Sorry, the model cannot answer this question."
assert exc_info.value.model == "bedrock-nova-micro"
assert exc_info.value.guardrail_name == "test-bedrock-guard"
@pytest.mark.asyncio
async def test_make_bedrock_api_request_block_raises_http_400_when_flag_unset():
guardrail = BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=False,
)
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _blocked_bedrock_httpx_response()
with pytest.raises(HTTPException) as exc_info:
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=[{"role": "user", "content": "hi"}],
request_data={"model": "bedrock-nova-micro"},
)
assert exc_info.value.status_code == 400
@pytest.mark.asyncio
async def test_async_pre_call_hook_propagates_modify_response_on_block():
"""pre_call: block with disable_exception_on_block=True must raise
ModifyResponseException so the endpoint handler returns 200 with the block
message. Before LIT-4186 the exception was swallowed and only data
["mock_response"] was mutated, which the unified pre_call path never read
(surfaced as HTTP 500)."""
from litellm.exceptions import ModifyResponseException
guardrail = BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=True,
)
request_data = {
"model": "bedrock-nova-micro",
"messages": [{"role": "user", "content": "My name is John Doe"}],
}
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _blocked_bedrock_httpx_response()
with pytest.raises(ModifyResponseException) as exc_info:
await guardrail.async_pre_call_hook(
user_api_key_dict=UserAPIKeyAuth(),
cache=DualCache(),
data=request_data,
call_type="acompletion",
)
assert exc_info.value.message == "Sorry, the model cannot answer this question."
# No `mock_response` mutation: the old broken contract must be gone
# (route_request unpacks kwargs before this hook runs, so `mock_response`
# would never reach the LLM call anyway).
assert "mock_response" not in request_data
@pytest.mark.asyncio
async def test_async_moderation_hook_propagates_modify_response_on_block():
"""during_call: block must raise ModifyResponseException from the moderation
task so the surrounding asyncio.gather cancels the LLM call, instead of
the old behavior of swallowing the block and letting the model call proceed
(LIT-4186 symptom 2: silent bypass, model billed)."""
from litellm.exceptions import ModifyResponseException
guardrail = BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=True,
)
request_data = {
"model": "bedrock-nova-micro",
"messages": [{"role": "user", "content": "My name is John Doe"}],
}
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _blocked_bedrock_httpx_response()
with pytest.raises(ModifyResponseException) as exc_info:
await guardrail.async_moderation_hook(
data=request_data,
user_api_key_dict=UserAPIKeyAuth(),
call_type="acompletion",
)
assert exc_info.value.message == "Sorry, the model cannot answer this question."
@pytest.mark.asyncio
async def test_async_post_call_success_hook_attaches_original_response_on_block():
"""post_call: block must raise ModifyResponseException and attach the LLM
response to `original_response` so the synthetic block reply reports the
upstream call's real token usage instead of zero."""
from litellm.exceptions import ModifyResponseException
guardrail = BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=True,
)
request_data = {
"model": "bedrock-nova-micro",
"messages": [{"role": "user", "content": "hi"}],
}
llm_response = _model_response("Hello John Doe! The capital of France is Paris.")
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _blocked_bedrock_httpx_response()
with pytest.raises(ModifyResponseException) as exc_info:
await guardrail.async_post_call_success_hook(
data=request_data,
user_api_key_dict=UserAPIKeyAuth(),
response=llm_response,
)
assert exc_info.value.original_response is llm_response
@pytest.mark.asyncio
async def test_apply_guardrail_propagates_modify_response_on_block():
"""apply_guardrail (unified path used by pre_call / /apply_guardrail
endpoint) must let ModifyResponseException propagate as-is so the endpoint
handler catches it and returns a 200."""
from litellm.exceptions import ModifyResponseException
guardrail = BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=True,
)
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.side_effect = ModifyResponseException(
message="Sorry, the model cannot answer this question.",
model="bedrock-nova-micro",
request_data={},
guardrail_name="test-bedrock-guard",
)
with pytest.raises(ModifyResponseException) as exc_info:
await guardrail.apply_guardrail(
inputs={"texts": ["My name is John Doe"]},
request_data={"model": "bedrock-nova-micro"},
input_type="request",
)
assert exc_info.value.message == "Sorry, the model cannot answer this question."
_ANTHROPIC_SSE_CHUNKS = (
b'event: message_start\ndata: {"type":"message_start","message":{"id":"msg_1","type":"message",'
b'"role":"assistant","model":"claude","content":[],"usage":{"input_tokens":5,"output_tokens":0}}}\n\n',
b'event: content_block_start\ndata: {"type":"content_block_start","index":0,'
b'"content_block":{"type":"text","text":""}}\n\n',
b'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,'
b'"delta":{"type":"text_delta","text":"my ssn is 123-45-6789"}}\n\n',
b'event: content_block_stop\ndata: {"type":"content_block_stop","index":0}\n\n',
b'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn"},'
b'"usage":{"output_tokens":9}}\n\n',
b'event: message_stop\ndata: {"type":"message_stop"}\n\n',
)
async def _anthropic_sse_stream():
for chunk in _ANTHROPIC_SSE_CHUNKS:
yield chunk
async def _drain_streaming_hook(
guardrail: BedrockGuardrail, request_data: dict[str, object] | None = None
) -> list[object]:
return [
chunk
async for chunk in guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(),
response=_anthropic_sse_stream(),
request_data=request_data
if request_data is not None
else {"model": "claude-sonnet-4-5", "messages": [{"role": "user", "content": "what is my ssn"}]},
)
]
def _sse_guardrail(**kwargs: object) -> BedrockGuardrail:
return BedrockGuardrail(
guardrail_name="bedrock-sse",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
**kwargs,
)
@pytest.mark.asyncio
async def test_streaming_hook_scans_raw_anthropic_sse_instead_of_crashing():
"""A /v1/messages stream arrives as raw SSE frames and must be assembled, then scanned.
Regression for `500 Error building chunks for logging/streaming usage calculation`:
stream_chunk_builder subscripts each chunk, which raises TypeError on bytes.
"""
guardrail = _sse_guardrail()
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = {"action": "NONE"}
delivered = await _drain_streaming_hook(guardrail)
mock_api.assert_called_once()
kwargs = mock_api.call_args.kwargs
assert kwargs["source"] == "OUTPUT"
assert "my ssn is 123-45-6789" in str(kwargs["response"].choices[0].message.content)
assert kwargs["messages"] == [{"role": "user", "content": "what is my ssn"}]
assert tuple(delivered) == _ANTHROPIC_SSE_CHUNKS
@pytest.mark.asyncio
async def test_streaming_hook_emits_masked_text_for_raw_anthropic_sse():
"""Masking must reach the client on /v1/messages, with mask_response_content unset.
The assembled path masks regardless of the flag, so forwarding the original frames here
would ship exactly the text the guardrail redacted.
"""
guardrail = _sse_guardrail()
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "my ssn is {SSN}"}],
}
delivered = await _drain_streaming_hook(guardrail)
body = b"".join(delivered)
assert b"{SSN}" in body
assert b"123-45-6789" not in body
@pytest.mark.asyncio
async def test_streaming_hook_block_stream_keeps_upstream_identity():
"""A blocked stream must carry the same id and model as the mask path, not the proxy alias."""
guardrail = _sse_guardrail(disable_exception_on_block=True)
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.side_effect = ModifyResponseException(
message="Sorry, the model cannot answer this question.",
model="my-proxy-alias",
request_data={},
)
delivered = await _drain_streaming_hook(guardrail)
body = b"".join(delivered)
# the shared block builder mints a new message id: the block is not the upstream message
assert b'"id": "msg_' in body
assert b'"model": "claude"' in body
assert b"my-proxy-alias" not in body
@pytest.mark.asyncio
async def test_streaming_hook_reraises_guardrail_service_failures():
"""A Bedrock outage must keep its status, not be reported to the caller as a guardrail decision.
A policy block is the only 400 detailing a Mapping.
"""
guardrail = _sse_guardrail()
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.side_effect = HTTPException(status_code=500, detail="Bedrock guardrail throttle retries exhausted")
with pytest.raises(HTTPException) as exc:
await _drain_streaming_hook(guardrail)
assert exc.value.status_code == 500
@pytest.mark.asyncio
async def test_streaming_hook_frames_a_service_failure_once_a_keepalive_ping_flushed_the_headers():
"""Past the ping the status line is already on the wire, so a raise reaches the client as nothing.
The failure has to travel as a frame instead, carrying its real status in the message.
"""
guardrail = _sse_guardrail()
with (
patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api,
patch.object(litellm, "anthropic_sse_ping_interval_seconds", 0.0001),
):
mock_api.side_effect = HTTPException(status_code=503, detail="Bedrock is unavailable")
delivered = await _drain_streaming_hook(guardrail)
body = b"".join(delivered).decode()
frame = next(line for line in body.splitlines() if line.startswith("data: "))
message = json.loads(frame[6:])["error"]["message"]
assert message == "503: Bedrock is unavailable"
@pytest.mark.asyncio
async def test_streaming_hook_reraises_a_service_failure_that_details_a_mapping():
"""InvokeGuardrailChecks details a Mapping on its 500, so detail shape alone cannot mean "block"."""
guardrail = _sse_guardrail()
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.side_effect = HTTPException(
status_code=500,
detail={"error": "Bedrock InvokeGuardrailChecks returned an unexpected response shape"},
)
with pytest.raises(HTTPException) as exc:
await _drain_streaming_hook(guardrail)
assert exc.value.status_code == 500
@pytest.mark.asyncio
async def test_streaming_block_error_frame_message_is_a_string():
"""AnthropicErrorDetail.message is typed str, built by the proxy's own detail serializer."""
guardrail = _sse_guardrail()
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.side_effect = HTTPException(
status_code=400, detail={"error": "Violated guardrail policy", "guardrailIdentifier": "gid"}
)
delivered = await _drain_streaming_hook(guardrail)
frame = next(line for line in b"".join(delivered).decode().splitlines() if line.startswith("data: "))
message = json.loads(frame[6:])["error"]["message"]
# AnthropicErrorDetail.message is typed str, and the proxy's own serializer produces the
# readable message rather than a repr of the detail dict
assert isinstance(message, str)
assert message == "Violated guardrail policy"
@pytest.mark.asyncio
async def test_streaming_hook_fails_closed_when_raw_sse_cannot_be_assembled():
"""An unscannable stream must not be delivered: forwarding it silently disables the guardrail."""
guardrail = _sse_guardrail()
async def _unparseable_stream():
yield b'data: {"type":"content_block_delta"}\n\n'
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
delivered = [
chunk
async for chunk in guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(),
response=_unparseable_stream(),
request_data={"model": "claude-sonnet-4-5"},
)
]
mock_api.assert_not_called()
body = b"".join(delivered)
# a raise cannot reach the client once a keepalive ping has flushed the headers
assert b"event: error" in body
assert b"could not be assembled" in body
assert b"content_block_delta" not in body
@pytest.mark.asyncio
async def test_streaming_hook_fails_closed_when_assembler_raises_api_error():
"""stream_chunk_builder re-raises assembly failures as litellm.APIError; it must not escape.
That exception message is the exact 500 this fix exists to remove.
"""
guardrail = _sse_guardrail()
with patch(
"litellm.llms.anthropic.pass_through.stream_assembly.build_complete_streaming_response",
side_effect=litellm.APIError(
status_code=500,
message="Error building chunks for logging/streaming usage calculation",
llm_provider="",
model="",
),
):
delivered = await _drain_streaming_hook(guardrail)
assert b"event: error" in b"".join(delivered)
@pytest.mark.asyncio
async def test_streaming_hook_preserves_message_id_and_model_when_re_emitting():
"""A rewritten stream must still look like the upstream Anthropic response."""
guardrail = _sse_guardrail()
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "my ssn is {SSN}"}],
}
delivered = await _drain_streaming_hook(guardrail)
body = b"".join(delivered)
assert b'"id": "msg_1"' in body
assert b"unknown-model" not in body
assert b'"model": "claude"' in body
@pytest.mark.asyncio
async def test_streaming_hook_blocks_raw_anthropic_sse_on_violation():
"""A block on the extracted text must stop the stream rather than deliver it."""
guardrail = _sse_guardrail()
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.side_effect = HTTPException(status_code=400, detail={"error": "Violated guardrail policy"})
delivered = await _drain_streaming_hook(guardrail)
body = b"".join(delivered)
# a keepalive ping may already have flushed the headers, so the block has to travel as a frame
assert b"event: error" in body
assert b"Violated guardrail policy" in body
assert b"123-45-6789" not in body
@pytest.mark.asyncio
async def test_streaming_hook_yields_synthetic_block_stream_for_raw_anthropic_sse():
"""disable_exception_on_block must keep behaving as a stream, not an SSE 500 frame."""
guardrail = _sse_guardrail(disable_exception_on_block=True)
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.side_effect = ModifyResponseException(
message="Sorry, the model cannot answer this question.",
model="claude",
request_data={},
)
delivered = await _drain_streaming_hook(guardrail)
body = b"".join(delivered)
assert b"Sorry, the model cannot answer this question." in body
assert b"123-45-6789" not in body
# the upstream call was already paid for, so the block frame must still report its usage
assert b'"input_tokens": 5' in body
assert b'"output_tokens": 9' in body
@pytest.mark.asyncio
async def test_streaming_post_call_block_yields_synthetic_stream_not_raise():
"""LIT-4186 regression: with disable_exception_on_block=True, streaming
post_call blocks must be delivered as a synthetic stream (finish_reason=
content_filter, block message as content), NOT raised. Pre-fix the local
handler already produced this shape; the LIT-4186 refactor briefly turned
it into an SSE 500 by letting ModifyResponseException escape the streaming
generator. This test locks in the correct streaming contract.
"""
from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
guardrail = BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=True,
)
async def _stream():
yield ModelResponseStream(
choices=[
StreamingChoices(
index=0,
delta=Delta(role="assistant", content="Coffee is a popular"),
)
]
)
yield ModelResponseStream(
choices=[StreamingChoices(index=0, delta=Delta(content=" beverage."), finish_reason="stop")]
)
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _blocked_bedrock_httpx_response()
chunks = [
c
async for c in guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(),
response=_stream(),
request_data={"model": "bedrock-nova-micro"},
)
]
assert chunks, "streaming block should yield synthetic chunks, not error out"
assembled_content = "".join(
(c.choices[0].delta.content or "")
for c in chunks
if getattr(c, "choices", None) and getattr(c.choices[0], "delta", None)
)
assert assembled_content == "Sorry, the model cannot answer this question."
assert chunks[-1].choices[0].finish_reason == "content_filter"
@pytest.mark.asyncio
async def test_streaming_post_call_block_preserves_upstream_usage():
"""LIT-4186: streaming block must report the usage the upstream LLM call
actually consumed. Non-streaming blocks carry it via original_response +
_blocked_response_usage in the endpoint handler; streaming has to copy it
onto the synthetic ModelResponse directly since the exception can't escape
the SSE generator. Without this, clients see accurate billing on
non-streaming blocks and zero on streaming blocks -- silent revenue leak."""
from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices, Usage
guardrail = BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=True,
)
async def _stream_with_usage():
# Terminal chunk carrying usage, as OpenAI-style streams do with
# stream_options={"include_usage": True}. stream_chunk_builder
# aggregates this into the assembled ModelResponse's .usage.
yield ModelResponseStream(
choices=[StreamingChoices(index=0, delta=Delta(role="assistant", content="Coffee is delicious"))]
)
yield ModelResponseStream(
choices=[StreamingChoices(index=0, delta=Delta(content=""), finish_reason="stop")],
usage=Usage(prompt_tokens=42, completion_tokens=17, total_tokens=59),
)
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _blocked_bedrock_httpx_response()
chunks = [
c
async for c in guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(),
response=_stream_with_usage(),
request_data={"model": "bedrock-nova-micro"},
)
]
# Find the chunk carrying usage (MockResponseIterator emits it on the
# terminating chunk when the source ModelResponse has .usage set)
usage_chunks = [c for c in chunks if getattr(c, "usage", None) is not None]
assert usage_chunks, "streaming block should carry the upstream call's usage on at least one chunk"
reported_usage = usage_chunks[-1].usage
assert reported_usage.prompt_tokens == 42
assert reported_usage.completion_tokens == 17
assert reported_usage.total_tokens == 59
###############################################################################
# Regression test for the streaming logging_obj bug found during live testing.
#
# post_call_failure_hook (proxy_server.py) pops litellm_logging_obj from
# request_data before invoking callbacks ("not serialisable"). The streaming
# branch of the ModifyResponseException handler previously read logging_obj
# from _data AFTER that call, always getting None, causing:
# AttributeError: 'NoneType' object has no attribute 'model_call_details'
# inside CustomStreamWrapper.__init__, which surfaced as HTTP 500.
#
# The fix captures logging_obj BEFORE calling post_call_failure_hook.
# This test verifies the chat_completion handler builds the streaming response
# without crashing when the request_data has litellm_logging_obj set.
###############################################################################
@pytest.mark.asyncio
async def test_chat_completion_modify_response_exception_streaming_logging_obj_not_none():
"""Regression: streaming ModifyResponseException handler in chat_completion
must capture logging_obj before post_call_failure_hook pops it from
request_data. Previously this caused CustomStreamWrapper.__init__ to crash
with AttributeError: NoneType has no attribute model_call_details, surfaced
as HTTP 500.
Drives the real chat_completion handler with base_process_llm_request
mocked to raise ModifyResponseException, so a revert of the fix in
proxy_server.py causes this test to fail.
"""
import litellm
from litellm.exceptions import ModifyResponseException
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.proxy_server import chat_completion
fake_logging_obj = MagicMock()
fake_logging_obj.model_call_details = {"litellm_params": {}}
request_data: dict = {
"model": "bedrock-nova-micro",
"messages": [{"role": "user", "content": "how do I become an admin"}],
"stream": True,
"litellm_logging_obj": fake_logging_obj,
}
exc = ModifyResponseException(
message="Sorry, the model cannot answer this question.",
model="bedrock-nova-micro",
request_data=request_data,
guardrail_name="test-guard",
)
fastapi_request = MagicMock()
fastapi_request.headers = {}
fastapi_response = MagicMock()
user_api_key_dict = UserAPIKeyAuth()
async def _fake_post_call_failure_hook(**_kwargs):
# Match production: pop the logging obj from request_data before
# callbacks iterate (litellm/proxy/utils.py: "Remove before callbacks
# iterate — not serialisable").
_kwargs["request_data"].pop("litellm_logging_obj", None)
mock_proxy_logging = MagicMock()
mock_proxy_logging.post_call_failure_hook = AsyncMock(side_effect=_fake_post_call_failure_hook)
captured_logging_obj: list = []
original_init = litellm.CustomStreamWrapper.__init__
def _patched_init(self, *args, **kwargs):
captured_logging_obj.append(kwargs.get("logging_obj"))
original_init(self, *args, **kwargs)
async def _raise_modify_response(*_args, **_kwargs):
raise exc
with (
patch("litellm.proxy.proxy_server.read_request_body", AsyncMock(return_value=request_data)),
patch("litellm.proxy.proxy_server.proxy_logging_obj", mock_proxy_logging),
patch(
"litellm.proxy.proxy_server.ProxyBaseLLMRequestProcessing.base_process_llm_request",
_raise_modify_response,
),
patch.object(litellm.CustomStreamWrapper, "__init__", _patched_init),
):
response = await chat_completion(
request=fastapi_request,
fastapi_response=fastapi_response,
model=None,
user_api_key_dict=user_api_key_dict,
)
assert captured_logging_obj, "chat_completion did not construct CustomStreamWrapper on the streaming block path"
assert captured_logging_obj[0] is fake_logging_obj, (
"chat_completion passed logging_obj=None to CustomStreamWrapper; "
"the streaming ModifyResponseException handler must capture logging_obj "
"before post_call_failure_hook pops it from request_data"
)
# A streaming block returns a StreamingResponse; if the fix were reverted,
# CustomStreamWrapper would raise AttributeError inside __init__ and this
# call would never reach here.
assert response is not None
def _too_large_validation_httpx_response() -> MagicMock:
response = MagicMock()
response.status_code = 400
response.json.return_value = {
"message": "Input is too long. Content size exceeds the maximum input size in text units."
}
response.text = json.dumps(response.json.return_value)
return response
def _other_validation_httpx_response() -> MagicMock:
response = MagicMock()
response.status_code = 400
response.json.return_value = {"message": "guardrailIdentifier is not valid"}
response.text = json.dumps(response.json.return_value)
return response
def _throttling_httpx_response() -> MagicMock:
response = MagicMock()
response.status_code = 429
response.json.return_value = {"message": "Rate exceeded"}
response.text = json.dumps(response.json.return_value)
response.headers = {}
return response
def _too_large_throttling_httpx_response() -> MagicMock:
"""The shape AWS actually returns for an oversized ApplyGuardrail request when
the guardrail has an active content-filter policy: a 429 ThrottlingException,
not the documented 400 ValidationException. Message taken from a live call."""
response = MagicMock()
response.status_code = 429
response.json.return_value = {
"message": (
"Input text size (3273 text units) exceeds the maximum allowed "
"(1000 text units) for the content filter policy (Classic tier)."
)
}
response.text = json.dumps(response.json.return_value)
response.headers = {}
return response
def _passing_bedrock_httpx_response(marker: str) -> MagicMock:
"""A successful ApplyGuardrail response tagged with `marker` so tests can
verify which chunk produced which output/usage after merging."""
response = MagicMock()
response.status_code = 200
response.json.return_value = {
"action": "NONE",
"outputs": [{"text": marker}],
"assessments": [],
"usage": {"contentPolicyUnits": 1},
}
return response
def _blocking_bedrock_httpx_response(marker: str) -> MagicMock:
response = MagicMock()
response.status_code = 200
response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": marker}],
"assessments": [{"topicPolicy": {"topics": [{"name": marker, "type": "DENY", "action": "BLOCKED"}]}}],
"usage": {"contentPolicyUnits": 1},
}
return response
def _bedrock_guardrail_for_chunk_tests() -> "BedrockGuardrail":
return BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=False,
)
@pytest.mark.asyncio
async def test_apply_guardrail_chunks_on_too_large_validation_error():
"""A too-large 400 on the whole-content call must trigger a bisect-and-retry,
and the two chunk responses must be merged (assessments concatenated, usage
summed, outputs concatenated) rather than losing either half's result."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{"role": "user", "content": "first half of a very long message"},
{"role": "user", "content": "second half of a very long message"},
]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
call_count = 0
async def _post_side_effect(*_args, **_kwargs):
nonlocal call_count
call_count += 1
if call_count == 1:
return _too_large_validation_httpx_response()
if call_count == 2:
return _passing_bedrock_httpx_response("chunk-1")
return _blocking_bedrock_httpx_response("chunk-2")
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = _post_side_effect
with pytest.raises(HTTPException) as exc_info:
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert call_count == 3
detail = exc_info.value.detail
assert exc_info.value.status_code == 400
assert "chunk-2" in detail["bedrock_guardrail_response"]
assert detail["assessments"][0]["matches"][0]["name"] == "chunk-2"
@pytest.mark.asyncio
async def test_apply_guardrail_merges_usage_and_outputs_across_chunks_when_both_pass():
"""When both chunks pass clean, the merged response must still carry both
chunks' outputs/usage forward (needed for accurate logging/telemetry) and
must not itself raise."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{"role": "user", "content": "chunk one text"},
{"role": "user", "content": "chunk two text"},
]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
call_count = 0
async def _post_side_effect(*_args, **_kwargs):
nonlocal call_count
call_count += 1
if call_count == 1:
return _too_large_validation_httpx_response()
if call_count == 2:
return _passing_bedrock_httpx_response("chunk-1")
return _passing_bedrock_httpx_response("chunk-2")
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = _post_side_effect
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert call_count == 3
assert result.get("action") == "NONE"
output_texts = [o.get("text") for o in result.get("outputs") or []]
assert output_texts == ["chunk-1", "chunk-2"]
assert result.get("usage", {}).get("contentPolicyUnits") == 2
@pytest.mark.asyncio
async def test_apply_guardrail_recurses_past_first_bisection_into_four_chunks():
"""A payload that is still too large after one bisection must keep splitting
-- chunking is not capped at two pieces. Four messages where both the
whole-content call AND both first-level halves are too large must recurse
one level deeper into four chunks that all fit, not give up after the
first split."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{"role": "user", "content": "message one"},
{"role": "user", "content": "message two"},
{"role": "user", "content": "message three"},
{"role": "user", "content": "message four"},
]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
responses = [
_too_large_validation_httpx_response(), # whole content: [1,2,3,4]
_too_large_validation_httpx_response(), # first half: [1,2]
_passing_bedrock_httpx_response("message one"),
_passing_bedrock_httpx_response("message two"),
_too_large_validation_httpx_response(), # second half: [3,4]
_passing_bedrock_httpx_response("message three"),
_passing_bedrock_httpx_response("message four"),
]
async def _post_side_effect(*_args, **_kwargs):
return responses.pop(0)
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = _post_side_effect
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert mock_post.await_count == 7
assert not responses
assert result.get("action") == "NONE"
output_texts = [o.get("text") for o in result.get("outputs") or []]
assert output_texts == ["message one", "message two", "message three", "message four"]
@pytest.mark.asyncio
async def test_apply_guardrail_does_not_chunk_when_grounding_present():
"""Contextual-grounding requests are scored holistically against the whole
source; chunking them would silently produce misleading grounding scores.
A too-large error on a grounded request must propagate unchanged, not be
bisected."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{
"role": "system",
"content": [{"type": "grounding_source", "text": "reference source text"}],
},
{"role": "user", "content": "what does the source say?"},
]
model_response = ModelResponse()
model_response.choices = [litellm.Choices(message=litellm.Message(content="a grounded answer", role="assistant"))]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _too_large_validation_httpx_response()
with pytest.raises(HTTPException) as exc_info:
await guardrail.make_bedrock_api_request(
source="OUTPUT",
messages=messages,
response=model_response,
request_data={"model": "bedrock-nova-micro"},
)
assert mock_post.await_count == 1
assert exc_info.value.status_code == 400
@pytest.mark.asyncio
async def test_apply_guardrail_does_not_chunk_on_non_size_validation_error():
"""A 400 for an unrelated validation problem (e.g. a bad guardrail id) must
not trigger chunking -- retrying a bad-config error split into pieces would
just fail twice more and mask the real problem."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{"role": "user", "content": "first"},
{"role": "user", "content": "second"},
]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _other_validation_httpx_response()
with pytest.raises(HTTPException) as exc_info:
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert mock_post.await_count == 1
assert exc_info.value.status_code == 400
assert "not valid" in str(exc_info.value.detail)
@pytest.mark.asyncio
async def test_apply_guardrail_too_large_on_unsplittable_text_propagates_original_error():
"""A too-large error on content that has been bisected down to text too
short to split further (< 2 characters) must propagate the original error
rather than looping or crashing."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [{"role": "user", "content": "a"}]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _too_large_validation_httpx_response()
with pytest.raises(HTTPException) as exc_info:
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert mock_post.await_count == 1
assert exc_info.value.status_code == 400
@pytest.mark.asyncio
async def test_apply_guardrail_too_large_on_single_item_splits_by_text_and_succeeds():
"""A too-large error on content that is already down to a single content
item must be bisected by that item's own text (not abandoned), so an
oversized single message can still be scanned successfully in halves."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [{"role": "user", "content": "one giant single block of text"}]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
call_count = 0
async def _post_side_effect(*_args, **_kwargs):
nonlocal call_count
call_count += 1
if call_count == 1:
return _too_large_validation_httpx_response()
return _passing_bedrock_httpx_response(f"half-{call_count}")
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = _post_side_effect
response = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert mock_post.await_count == 3
assert response.get("action") == "NONE"
def _raised_bedrock_error(status_code: int, message: str) -> httpx.HTTPStatusError:
"""A non-200 the way `AsyncHTTPHandler.post` actually surfaces it.
That handler calls `response.raise_for_status()`, so in production a non-200 from
Bedrock arrives as a raised `httpx.HTTPStatusError` carrying the response, never
as a returned response object. Tests that return the response instead exercise a
branch real traffic never reaches. A real `httpx.Response` is used rather than a
MagicMock because the transport helper branches on
`isinstance(err_response, httpx.Response)`."""
response = httpx.Response(
status_code=status_code,
json={"message": message},
request=httpx.Request("POST", "https://bedrock-runtime.us-east-1.amazonaws.com/guardrail"),
)
return httpx.HTTPStatusError(message, request=response.request, response=response)
_TOO_LARGE_MESSAGE = "Input is too long. Content size exceeds the maximum input size in text units."
@pytest.mark.asyncio
async def test_apply_guardrail_chunking_logs_once_when_client_raises_for_status():
"""The too-large attempt recovered by chunking must still produce exactly one
telemetry entry when the HTTP client raises for status, which is what really
happens: `AsyncHTTPHandler.post` calls `raise_for_status()`.
Regression for per-attempt `guardrail_failed_to_respond` entries leaking out of
the transport helper on a request that ultimately succeeded, which made a
recovered request look like several failures plus a success."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{"role": "user", "content": "chunk one text"},
{"role": "user", "content": "chunk two text"},
]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
call_count = 0
async def _post_side_effect(*_args, **_kwargs):
nonlocal call_count
call_count += 1
if call_count == 1:
raise _raised_bedrock_error(400, _TOO_LARGE_MESSAGE)
return _passing_bedrock_httpx_response(f"chunk-{call_count}")
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch.object(
guardrail,
"add_standard_logging_guardrail_information_to_request_data",
) as mock_log,
):
mock_post.side_effect = _post_side_effect
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert call_count == 3
assert result.get("action") == "NONE"
statuses = [call.kwargs.get("guardrail_status") for call in mock_log.call_args_list]
assert statuses == ["success"]
@pytest.mark.asyncio
async def test_apply_guardrail_unrecoverable_failure_still_logs_once_when_client_raises():
"""Suppressing the transport helper's per-attempt logging must not swallow the only
record of a genuine failure: an unsplittable too-large request still has to produce
exactly one `guardrail_failed_to_respond` entry, not zero."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [{"role": "user", "content": "x"}]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
async def _post_side_effect(*_args, **_kwargs):
raise _raised_bedrock_error(400, _TOO_LARGE_MESSAGE)
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch.object(
guardrail,
"add_standard_logging_guardrail_information_to_request_data",
) as mock_log,
):
mock_post.side_effect = _post_side_effect
with pytest.raises(HTTPException):
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
statuses = [call.kwargs.get("guardrail_status") for call in mock_log.call_args_list]
assert statuses == ["guardrail_failed_to_respond"]
@pytest.mark.asyncio
async def test_apply_guardrail_single_item_split_twice_still_yields_one_output_per_item():
"""One oversized content item that needs two levels of text bisection ends up
as four text fragments, and all four must still collapse back into exactly
ONE output entry, because they all came from one original content item.
Downstream masking (`_apply_masking_to_messages`) walks the merged outputs by
a running index across the original, unchunked message list, so emitting more
than one entry for a single message shifts every later message's masked text
onto the wrong message and drops the surplus. Regression for fragment
grouping assuming fragments only ever arrive as adjacent sibling *pairs*,
which holds for one bisection level but not for two."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [{"role": "user", "content": "aaaa bbbb cccc dddd eeee ffff gggg hhhh"}]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
responses = [
_too_large_validation_httpx_response(), # whole single item
_too_large_validation_httpx_response(), # first half
_passing_bedrock_httpx_response("q1"),
_passing_bedrock_httpx_response("q2"),
_too_large_validation_httpx_response(), # second half
_passing_bedrock_httpx_response("q3"),
_passing_bedrock_httpx_response("q4"),
]
async def _post_side_effect(*_args, **_kwargs):
return responses.pop(0)
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = _post_side_effect
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert mock_post.await_count == 7
assert not responses
assert result.get("action") == "NONE"
output_texts = [o.get("text") for o in result.get("outputs") or []]
assert output_texts == ["q1q2q3q4"]
@pytest.mark.asyncio
async def test_apply_guardrail_chunk_retries_after_throttling_then_succeeds():
"""A chunk call throttled with a 429 must be retried with backoff and
eventually succeed, rather than surfacing the 429 to the caller."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{"role": "user", "content": "chunk one text"},
{"role": "user", "content": "chunk two text"},
]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
call_count = 0
async def _post_side_effect(*_args, **_kwargs):
nonlocal call_count
call_count += 1
if call_count == 1:
return _too_large_validation_httpx_response()
if call_count == 2:
return _throttling_httpx_response()
if call_count == 3:
return _passing_bedrock_httpx_response("chunk-1")
return _passing_bedrock_httpx_response("chunk-2")
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch(
"litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails.asyncio.sleep",
new_callable=AsyncMock,
) as mock_sleep,
):
mock_post.side_effect = _post_side_effect
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert call_count == 4
mock_sleep.assert_awaited()
output_texts = [o.get("text") for o in result.get("outputs") or []]
assert output_texts == ["chunk-1", "chunk-2"]
@pytest.mark.asyncio
async def test_apply_guardrail_chunking_logs_exactly_once_as_success():
"""A too-large 400 that is recovered by chunking must not leave behind a
'guardrail_failed_to_respond' telemetry entry for the initial oversized
attempt: the whole logical request (1 too-large attempt + 2 chunk
attempts here) must produce exactly one standard-logging entry, and it
must reflect the eventual success, not the transient too-large failure."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{"role": "user", "content": "chunk one text"},
{"role": "user", "content": "chunk two text"},
]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
call_count = 0
async def _post_side_effect(*_args, **_kwargs):
nonlocal call_count
call_count += 1
if call_count == 1:
return _too_large_validation_httpx_response()
if call_count == 2:
return _passing_bedrock_httpx_response("chunk-1")
return _passing_bedrock_httpx_response("chunk-2")
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch.object(
guardrail,
"add_standard_logging_guardrail_information_to_request_data",
) as mock_log,
):
mock_post.side_effect = _post_side_effect
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert call_count == 3
mock_log.assert_called_once()
assert mock_log.call_args.kwargs["guardrail_status"] == "success"
@pytest.mark.asyncio
async def test_apply_guardrail_unrecoverable_failure_logs_exactly_once_as_failed():
"""A too-large error that cannot be recovered (chunking disabled by
contextual grounding) must still log exactly once, as a failure -- not be
silently dropped by the chunking telemetry consolidation."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{
"role": "system",
"content": [{"type": "grounding_source", "text": "reference source text"}],
},
{"role": "user", "content": "what does the source say?"},
]
model_response = ModelResponse()
model_response.choices = [litellm.Choices(message=litellm.Message(content="a grounded answer", role="assistant"))]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch.object(
guardrail,
"add_standard_logging_guardrail_information_to_request_data",
) as mock_log,
):
mock_post.return_value = _too_large_validation_httpx_response()
with pytest.raises(HTTPException):
await guardrail.make_bedrock_api_request(
source="OUTPUT",
messages=messages,
response=model_response,
request_data={"model": "bedrock-nova-micro"},
)
mock_post.assert_awaited_once()
mock_log.assert_called_once()
assert mock_log.call_args.kwargs["guardrail_status"] == "guardrail_failed_to_respond"
@pytest.mark.asyncio
async def test_apply_guardrail_chunk_merge_preserves_masking_position():
"""An earlier chunk that comes back clean (empty `outputs`) must not
shift a later chunk's masked text onto the wrong message. Regression for:
flattening outputs without positional metadata let a later chunk's PII
redaction get applied to the first message while the actual PII-bearing
message (in a later chunk) was forwarded unmasked."""
guardrail = BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=False,
mask_request_content=True,
)
request_data = {
"model": "bedrock-nova-micro",
"messages": [
{"role": "user", "content": "clean chunk with nothing to mask"},
{"role": "user", "content": "chunk with PII: John Doe"},
],
}
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
call_count = 0
def _clean_httpx_response() -> MagicMock:
response = MagicMock()
response.status_code = 200
response.json.return_value = {"action": "NONE", "assessments": []}
return response
def _masked_httpx_response() -> MagicMock:
response = MagicMock()
response.status_code = 200
response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "chunk with PII: [NAME]"}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [{"type": "NAME", "match": "John Doe", "action": "ANONYMIZED"}]
}
}
],
}
return response
async def _post_side_effect(*_args, **_kwargs):
nonlocal call_count
call_count += 1
if call_count == 1:
return _too_large_validation_httpx_response()
if call_count == 2:
return _clean_httpx_response()
return _masked_httpx_response()
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = _post_side_effect
await guardrail.async_pre_call_hook(
user_api_key_dict=UserAPIKeyAuth(),
cache=DualCache(),
data=request_data,
call_type="acompletion",
)
assert call_count == 3
updated_messages = request_data["messages"]
assert updated_messages[0]["content"] == "clean chunk with nothing to mask"
assert updated_messages[1]["content"] == "chunk with PII: [NAME]"
@pytest.mark.asyncio
async def test_apply_guardrail_accepted_content_costs_exactly_one_call():
"""Content AWS accepts must cost exactly one ApplyGuardrail call, however far over
the chunk budget it is. Chunking is a recovery path, not something every request
pays for. Regression for: bin-packing eagerly on every request, which split
conversations AWS was happy to take whole and multiplied billed calls and guardrail
latency on traffic that never had a size problem."""
guardrail = _bedrock_guardrail_for_chunk_tests()
item_text = "x" * (BEDROCK_APPLY_GUARDRAIL_CHUNK_BUDGET_CHARS // 2)
messages = [{"role": "user", "content": item_text} for _ in range(3)]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
call_count = 0
async def _post_side_effect(*_args, **_kwargs):
nonlocal call_count
call_count += 1
return _passing_bedrock_httpx_response(f"batch-{call_count}")
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = _post_side_effect
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert call_count == 1
assert result.get("action") == "NONE"
@pytest.mark.asyncio
async def test_apply_guardrail_small_content_makes_exactly_one_call():
"""Content that fits entirely within the budget in a single batch must
make exactly one ApplyGuardrail call -- confirms bin-packing does not
introduce an extra probe call for the common (small-request) case."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{"role": "user", "content": "short message one"},
{"role": "user", "content": "short message two"},
]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.return_value = _passing_bedrock_httpx_response("single-batch")
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
mock_post.assert_awaited_once()
assert result.get("action") == "NONE"
@pytest.mark.asyncio
async def test_apply_guardrail_batch_under_budget_still_rejected_falls_back_to_bisection():
"""A batch that fits the budget guess but is still rejected by AWS as too large
(a lower real per-account/region/policy cap) must fall back to bisection for that
batch only, and any other batch from the same request that AWS already accepted
must not be re-sent.
Three half-budget items pack into two batches once the whole-payload probe is
rejected, so the sequence is probe, batch one (rejected), its two halves, batch
two."""
guardrail = _bedrock_guardrail_for_chunk_tests()
item_text = "x" * (BEDROCK_APPLY_GUARDRAIL_CHUNK_BUDGET_CHARS // 2)
messages = [
{"role": "user", "content": item_text},
{"role": "user", "content": item_text},
{"role": "user", "content": item_text},
]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
call_count = 0
async def _post_side_effect(*_args, **_kwargs):
nonlocal call_count
call_count += 1
if call_count in (1, 2):
return _too_large_validation_httpx_response()
return _passing_bedrock_httpx_response(f"chunk-{call_count}")
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = _post_side_effect
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert call_count == 5
assert result.get("action") == "NONE"
output_texts = [o.get("text") for o in result.get("outputs") or []]
assert output_texts == ["chunk-3", "chunk-4", "chunk-5"]
def test_split_bedrock_content_single_item_splits_on_whitespace_not_mid_word():
"""A single content item whose raw character midpoint would fall inside a
word must instead split at the nearest whitespace, so neither fragment
ends or begins mid-token. Regression for the Veria AI review finding: a
denied word/PII pattern straddling a raw character-midpoint cut could be
truncated on both fragments and scan clean on each, then reassemble into
the original unmasked text -- a detection bypass."""
text = ("a" * 20) + " " + ("b" * 30)
raw_midpoint = len(text) // 2
assert text[raw_midpoint] == "b"
content = [BedrockContentItem(text=BedrockTextContent(text=text))]
split_content = BedrockGuardrail._split_bedrock_content(content)
assert split_content is not None
first_half, second_half = split_content
first_text = first_half[0]["text"]["text"]
second_text = second_half[0]["text"]["text"]
assert first_text + second_text == text
assert first_text == ("a" * 20) + " "
assert second_text == "b" * 30
def test_split_bedrock_content_single_item_with_no_whitespace_falls_back_to_midpoint():
"""A single giant token with no whitespace anywhere has no safe split
point, so the split must fall back to the raw character midpoint rather
than failing or looping."""
text = "a" * 40
content = [BedrockContentItem(text=BedrockTextContent(text=text))]
split_content = BedrockGuardrail._split_bedrock_content(content)
assert split_content is not None
first_half, second_half = split_content
first_text = first_half[0]["text"]["text"]
second_text = second_half[0]["text"]["text"]
assert first_text + second_text == text
assert len(first_text) == 20
assert len(second_text) == 20
def test_bin_pack_bedrock_content_packs_minimal_batches_within_budget():
"""Many medium items should pack into the minimal number of in-order
batches that each stay within budget, not one batch per item."""
items = [BedrockContentItem(text=BedrockTextContent(text="x" * 30)) for _ in range(10)]
batches = BedrockGuardrail._bin_pack_bedrock_content(items, budget=100)
assert sum(len(batch) for batch in batches) == 10
for batch in batches:
combined_len = sum(len(item["text"]["text"]) for item in batch)
assert combined_len <= 100
assert len(batches) == 4
def test_bin_pack_bedrock_content_oversized_single_item_becomes_its_own_batch():
"""An item whose own text already exceeds the budget must not be
pre-split here -- it becomes its own oversized batch, and only the
reactive bisection fallback (on an AWS rejection) may split it later."""
small_item = BedrockContentItem(text=BedrockTextContent(text="short"))
oversized_item = BedrockContentItem(text=BedrockTextContent(text="x" * 200))
items = [small_item, oversized_item, small_item]
batches = BedrockGuardrail._bin_pack_bedrock_content(items, budget=100)
assert batches == ((small_item,), (oversized_item,), (small_item,))
def test_bin_pack_bedrock_content_empty_content_makes_exactly_one_empty_batch():
"""Empty content must still pack into exactly one (empty) batch, matching
pre-bin-packing behavior of sending the content list as-is in one call --
bin-packing must not turn an empty request into zero ApplyGuardrail calls."""
assert BedrockGuardrail._bin_pack_bedrock_content([], budget=100) == ((),)
@pytest.mark.asyncio
async def test_apply_guardrail_too_large_reported_as_429_bisects_without_burning_retries():
"""AWS reports an oversized ApplyGuardrail request as a 429 ThrottlingException
(not the documented 400 ValidationException) when the guardrail has an active
content-filter policy. That is not a transient throttle -- re-posting the same
oversized content can never succeed -- so it must bisect immediately instead of
consuming the exponential-backoff retry budget first.
Regression for a bug found against a live guardrail: because the throttle retry
only keyed off status 429, every oversized chunk burned all
_BEDROCK_APPLY_GUARDRAIL_MAX_THROTTLE_RETRIES attempts (each a billed AWS call,
each preceded by a backoff sleep) before bisection got a chance, at every level
of the recursion."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [
{"role": "user", "content": "chunk one text"},
{"role": "user", "content": "chunk two text"},
]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
call_count = 0
async def _post_side_effect(*_args, **_kwargs):
nonlocal call_count
call_count += 1
if call_count == 1:
return _too_large_throttling_httpx_response()
return _passing_bedrock_httpx_response(f"half-{call_count}")
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch(
"litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails.asyncio.sleep",
new_callable=AsyncMock,
) as mock_sleep,
):
mock_post.side_effect = _post_side_effect
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert call_count == 3
mock_sleep.assert_not_awaited()
assert result.get("action") == "NONE"
output_texts = [o.get("text") for o in result.get("outputs") or []]
assert output_texts == ["half-2", "half-3"]
def test_chunk_budget_defaults_to_apply_guardrail_per_second_quota():
"""The default budget must track ApplyGuardrail's default quota of 25 text units
(about 1,000 characters each) per second. Packing to that size and posting
sequentially is what stops chunking from trading a size error for a throttle, so
this default is a deliberate match to AWS behaviour rather than an arbitrary
number."""
assert BEDROCK_APPLY_GUARDRAIL_CHUNK_BUDGET_CHARS == 25_000
assert BedrockGuardrail(guardrailIdentifier="g", guardrailVersion="DRAFT").chunk_budget_chars == 25_000
@pytest.mark.asyncio
async def test_configured_chunk_budget_changes_how_content_is_packed():
"""An account with raised quotas can set a larger `chunk_budget_chars` and have it
actually drive packing once AWS has rejected a payload, spending fewer
ApplyGuardrail calls for the same content instead of being pinned to the
conservative default.
Four 20,000-character messages are 80,000 characters total, and every call here is
preceded by the one whole-payload probe AWS rejects. At the 25,000 default only one
message fits per batch, so it is the probe plus four; at 50,000 two fit per batch,
so it is the probe plus two."""
messages = [{"role": "user", "content": "x" * 20_000} for _ in range(4)]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
async def _calls_made_with_budget(budget: int) -> int:
guardrail = BedrockGuardrail(
guardrail_name="test-bedrock-guard",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=False,
chunk_budget_chars=budget,
)
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
posted = 0
async def _post_side_effect(*_args, **_kwargs):
nonlocal posted
posted += 1
if posted == 1:
return _too_large_validation_httpx_response()
return _passing_bedrock_httpx_response("ok")
mock_post.side_effect = _post_side_effect
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
return mock_post.await_count
assert await _calls_made_with_budget(BEDROCK_APPLY_GUARDRAIL_CHUNK_BUDGET_CHARS) == 5
assert await _calls_made_with_budget(50_000) == 3
def test_split_index_never_produces_an_empty_fragment():
"""Both fragments must be non-empty for every splittable text, so bisection always
makes progress.
A text whose only qualifying whitespace is its final character is the dangerous
shape: taking that boundary puts the split at len(text), leaving the first fragment
identical to the input that was just rejected and the second empty. The recursion
would then resubmit the unchanged fragment forever and exhaust the stack instead of
scanning or surfacing Bedrock's error."""
for text in ("ab ", "xxxx ", ("x" * 40) + " ", " ab", "a b", "ab", " "):
split_at = BedrockGuardrail._nearest_whitespace_split_index(text)
assert 0 < split_at < len(text), f"degenerate split {split_at} for {text!r}"
assert text[:split_at] and text[split_at:], f"empty fragment for {text!r}"
assert text[:split_at] + text[split_at:] == text
@pytest.mark.asyncio
async def test_oversized_single_item_with_trailing_space_gives_up_instead_of_recursing():
"""An oversized single item whose only space is trailing must bottom out and
surface Bedrock's error, not recurse forever.
AWS is modelled the way it really behaves, rejecting every attempt, because the
danger is a fragment identical to the input that was just rejected: AWS would
reject it again, and each retry would split it into the same unchanged fragment.
A split that always shrinks the text terminates and re-raises; one that can return
the whole text raises RecursionError instead. The call-count bound is generous:
halving 41 characters down to unsplittable is a handful of attempts, nowhere near
a stack limit."""
guardrail = _bedrock_guardrail_for_chunk_tests()
messages = [{"role": "user", "content": ("x" * 40) + " "}]
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = lambda *_a, **_k: _too_large_validation_httpx_response()
with pytest.raises(HTTPException) as excinfo:
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=messages,
request_data={"model": "bedrock-nova-micro"},
)
assert excinfo.value.status_code == 400
assert mock_post.await_count < 200
class TestBedrockOnlyScanNewMessages:
"""Bedrock apply_guardrail honors only_scan_new_messages: scans only the per-session diff.
apply_guardrail is the path the proxy actually runs for Bedrock (via the unified
guardrail interface), so these tests exercise it directly rather than the legacy
async_pre_call_hook. Each test uses a unique session id to isolate the process-wide
incremental cache.
"""
def _guardrail(self):
return BedrockGuardrail(
guardrail_name="bedrock-incremental",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
default_on=True,
only_scan_new_messages=True,
)
@pytest.mark.asyncio
async def test_second_turn_scans_only_new_messages(self):
guardrail = self._guardrail()
session = {"litellm_session_id": "sess-bedrock-diff"}
bedrock_none = {"action": "NONE", "output": [], "outputs": []}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = bedrock_none
await guardrail.apply_guardrail(
inputs={"texts": ["be helpful", "first question"]},
request_data=session,
input_type="request",
)
assert mock_api.call_count == 1
first_scanned = mock_api.call_args.kwargs["messages"]
assert [m["content"] for m in first_scanned] == ["be helpful", "first question"]
mock_api.reset_mock()
await guardrail.apply_guardrail(
inputs={"texts": ["be helpful", "first question", "first answer", "second question"]},
request_data=session,
input_type="request",
)
assert mock_api.call_count == 1
second_scanned = mock_api.call_args.kwargs["messages"]
assert [m["content"] for m in second_scanned] == ["first answer", "second question"]
@pytest.mark.asyncio
async def test_identical_resend_skips_api_call(self):
guardrail = self._guardrail()
session = {"litellm_session_id": "sess-bedrock-resend"}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = {"action": "NONE", "output": [], "outputs": []}
await guardrail.apply_guardrail(
inputs={"texts": ["only question"]}, request_data=session, input_type="request"
)
assert mock_api.call_count == 1
mock_api.reset_mock()
result = await guardrail.apply_guardrail(
inputs={"texts": ["only question"]}, request_data=session, input_type="request"
)
mock_api.assert_not_called()
assert result["texts"] == ["only question"]
@pytest.mark.asyncio
async def test_no_session_id_scans_full_context(self):
guardrail = self._guardrail()
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = {"action": "NONE", "output": [], "outputs": []}
await guardrail.apply_guardrail(
inputs={"texts": ["q1", "a1", "q2"]},
request_data={"metadata": {}},
input_type="request",
)
assert mock_api.call_count == 1
scanned = mock_api.call_args.kwargs["messages"]
assert [m["content"] for m in scanned] == ["q1", "a1", "q2"]
@pytest.mark.asyncio
async def test_masking_guardrail_falls_back_and_does_not_persist(self):
"""A guardrail that anonymizes content must not be short-circuited.
Regression: the incremental fast path used to ignore the guardrail response,
so masked/anonymized output was dropped, the raw text reached the model, and
the segment was marked scanned so it was never re-checked. Detecting masked
output must force a full-context scan (which applies the masking) and must not
persist session state, so an identical resend is scanned again.
"""
guardrail = self._guardrail()
session = {"litellm_session_id": "sess-bedrock-mask"}
masked = {
"action": "GUARDRAIL_INTERVENED",
"output": [],
"outputs": [{"text": "my ssn is [REDACTED]"}],
}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = masked
result = await guardrail.apply_guardrail(
inputs={"texts": ["my ssn is 123-45-6789"]},
request_data=session,
input_type="request",
)
assert mock_api.call_count == 2
assert result["texts"] == ["my ssn is [REDACTED]"]
mock_api.reset_mock()
await guardrail.apply_guardrail(
inputs={"texts": ["my ssn is 123-45-6789"]},
request_data=session,
input_type="request",
)
assert mock_api.call_count >= 1
first_scanned = mock_api.call_args_list[0].kwargs.get("messages")
assert first_scanned is not None
assert [m["content"] for m in first_scanned] == ["my ssn is 123-45-6789"]
@pytest.mark.asyncio
async def test_generic_agent_multi_turn_scans_only_new_each_turn(self):
"""A generic agent (not Claude Code) opts in by propagating a session id.
Agent frameworks on the OpenAI SDK carry the session through the request
body (metadata.session_id here), not the x-claude-code-session-id header.
Across a growing multi-turn conversation every turn after the first must
send Bedrock only the newly appended segments, never the whole context.
"""
guardrail = self._guardrail()
session = {"metadata": {"session_id": "agent-multi-turn"}}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = {"action": "NONE", "output": [], "outputs": []}
await guardrail.apply_guardrail(
inputs={"texts": ["system prompt", "turn 1 question"]},
request_data=session,
input_type="request",
)
assert [m["content"] for m in mock_api.call_args.kwargs["messages"]] == [
"system prompt",
"turn 1 question",
]
mock_api.reset_mock()
await guardrail.apply_guardrail(
inputs={"texts": ["system prompt", "turn 1 question", "turn 1 answer", "turn 2 question"]},
request_data=session,
input_type="request",
)
assert [m["content"] for m in mock_api.call_args.kwargs["messages"]] == [
"turn 1 answer",
"turn 2 question",
]
mock_api.reset_mock()
await guardrail.apply_guardrail(
inputs={
"texts": [
"system prompt",
"turn 1 question",
"turn 1 answer",
"turn 2 question",
"turn 2 answer",
"turn 3 question",
]
},
request_data=session,
input_type="request",
)
assert [m["content"] for m in mock_api.call_args.kwargs["messages"]] == [
"turn 2 answer",
"turn 3 question",
]
def test_incremental_scan_cache_prefers_proxy_shared_cache(self):
guardrail = self._guardrail()
shared = DualCache()
proxy_logging = MagicMock()
proxy_logging.internal_usage_cache.dual_cache = shared
with patch("litellm.proxy.proxy_server.proxy_logging_obj", proxy_logging):
assert guardrail._incremental_scan_cache() is shared
def test_incremental_scan_cache_falls_back_when_proxy_logging_missing(self):
from litellm.integrations.custom_guardrail import dc as fallback_cache
guardrail = self._guardrail()
with patch("litellm.proxy.proxy_server.proxy_logging_obj", None):
assert guardrail._incremental_scan_cache() is fallback_cache
def test_incremental_scan_cache_falls_back_when_proxy_not_importable(self):
from litellm.integrations.custom_guardrail import dc as fallback_cache
guardrail = self._guardrail()
with patch.dict(sys.modules, {"litellm.proxy.proxy_server": None}):
assert guardrail._incremental_scan_cache() is fallback_cache
@pytest.mark.asyncio
async def test_blocked_turn_is_rescanned_on_retry(self):
guardrail = self._guardrail()
session = {"litellm_session_id": "sess-bedrock-blocked"}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.side_effect = HTTPException(status_code=400, detail="blocked")
with pytest.raises(HTTPException):
await guardrail.apply_guardrail(
inputs={"texts": ["blocked prompt"]}, request_data=session, input_type="request"
)
mock_api.reset_mock()
mock_api.side_effect = None
mock_api.return_value = {"action": "NONE", "output": [], "outputs": []}
await guardrail.apply_guardrail(
inputs={"texts": ["blocked prompt"]}, request_data=session, input_type="request"
)
assert mock_api.call_count == 1
scanned = mock_api.call_args.kwargs["messages"]
assert [m["content"] for m in scanned] == ["blocked prompt"]
class TestBedrockIncrementalFlagInteractions:
"""Regression coverage for only_scan_new_messages combined with the other
Bedrock guardrail flags, from the PR #33278 live validation. Live evidence:
each of these was reproduced against a real Bedrock ApplyGuardrail first;
the mocks here encode the wire payloads observed there.
"""
def _guardrail(self, **overrides):
params = dict(
guardrail_name="bedrock-incremental-flags",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
default_on=True,
only_scan_new_messages=True,
)
params.update(overrides)
return BedrockGuardrail(**params)
@pytest.mark.asyncio
async def test_edited_history_segment_rescans_only_that_segment(self):
guardrail = self._guardrail()
session = {"litellm_session_id": "sess-flags-edit"}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = {"action": "NONE", "output": [], "outputs": []}
await guardrail.apply_guardrail(
inputs={"texts": ["q1", "a1", "q2"]}, request_data=session, input_type="request"
)
mock_api.reset_mock()
await guardrail.apply_guardrail(
inputs={"texts": ["q1 EDITED", "a1", "q2"]}, request_data=session, input_type="request"
)
assert mock_api.call_count == 1
assert [m["content"] for m in mock_api.call_args.kwargs["messages"]] == ["q1 EDITED"]
@pytest.mark.asyncio
async def test_same_content_different_session_rescans_everything(self):
guardrail = self._guardrail()
texts = ["shared question", "shared answer"]
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = {"action": "NONE", "output": [], "outputs": []}
await guardrail.apply_guardrail(
inputs={"texts": list(texts)}, request_data={"litellm_session_id": "sess-x1"}, input_type="request"
)
mock_api.reset_mock()
await guardrail.apply_guardrail(
inputs={"texts": list(texts)}, request_data={"litellm_session_id": "sess-x2"}, input_type="request"
)
assert mock_api.call_count == 1
assert [m["content"] for m in mock_api.call_args.kwargs["messages"]] == texts
@pytest.mark.asyncio
async def test_litellm_masking_flag_disables_incremental_single_full_scan(self):
"""mask_request_content must fall back to exactly ONE full scan per turn
and never persist hashes (verified live: 1 call/turn, no cache writes)."""
guardrail = self._guardrail(mask_request_content=True)
session = {"litellm_session_id": "sess-flags-mask"}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = {"action": "NONE", "output": [], "outputs": []}
await guardrail.apply_guardrail(inputs={"texts": ["q1"]}, request_data=session, input_type="request")
assert mock_api.call_count == 1
mock_api.reset_mock()
await guardrail.apply_guardrail(inputs={"texts": ["q1"]}, request_data=session, input_type="request")
assert mock_api.call_count == 1, "masking mode must re-scan every turn, exactly once"
@pytest.mark.asyncio
async def test_server_side_anonymize_falls_back_full_scan_and_never_persists(self):
"""A guardrail that rewrites content (Bedrock-side ANONYMIZE) must fall back
to the full scan so masking applies, and record no session state. Live
validation showed this costs 2 provider calls per turn; the count is
asserted here as documentation of that intended-tradeoff behavior."""
guardrail = self._guardrail()
session = {"litellm_session_id": "sess-flags-anon"}
masked = {"action": "NONE", "output": [{"text": "MASKED q1"}], "outputs": [{"text": "MASKED q1"}]}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = masked
result = await guardrail.apply_guardrail(
inputs={"texts": ["q1"]}, request_data=session, input_type="request"
)
assert mock_api.call_count == 2, "incremental attempt + full-scan fallback"
assert result["texts"] == ["MASKED q1"], "masked content must be applied"
mock_api.reset_mock()
await guardrail.apply_guardrail(inputs={"texts": ["q1"]}, request_data=session, input_type="request")
assert mock_api.call_count == 2, "no hashes persisted, so the double scan repeats"
@pytest.mark.asyncio
@pytest.mark.xfail(
reason="PR #33278 known gap: incremental path bypasses _select_messages_for_apply_guardrail, "
"so experimental_use_latest_role_message_only is silently ignored. Intended semantics "
"(pending DRI decision): incremental mode defers to the latest-role selection.",
strict=False,
)
async def test_latest_role_only_is_respected_with_incremental(self):
guardrail = self._guardrail(experimental_use_latest_role_message_only=True)
session = {"litellm_session_id": "sess-flags-latestrole"}
structured = [
{"role": "system", "content": "sys"},
{"role": "user", "content": "q1"},
]
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = {"action": "NONE", "output": [], "outputs": []}
await guardrail.apply_guardrail(
inputs={"texts": ["sys", "q1"], "structured_messages": structured},
request_data=session,
input_type="request",
)
scanned = [m["content"] for m in mock_api.call_args.kwargs["messages"]]
assert scanned == ["q1"], "latest-role selection must exclude the system prompt"
@pytest.mark.asyncio
@pytest.mark.parametrize(
"mode, call_type, should_scan",
[
("during_mcp_call", "call_mcp_tool", True),
("during_call", "completion", True),
("during_mcp_call", "completion", False),
("during_call", "call_mcp_tool", False),
],
)
async def test_moderation_hook_honors_the_mcp_event_type(mode, call_type, should_scan):
"""A guardrail configured mode: during_mcp_call must actually scan MCP tool calls.
ProxyLogging.during_call_hook remaps call_mcp_tool to during_mcp_call before
dispatching, so re-checking during_call here would reject the very requests the
guardrail was configured for and let the tool call through unscanned.
"""
guardrail = BedrockGuardrail(
guardrail_name="bedrock-mcp",
guardrailIdentifier="gid",
guardrailVersion="1",
event_hook=mode,
default_on=True,
)
data = {
"messages": [{"role": "user", "content": "scan me"}],
"mcp_tool_name": "search",
"mcp_arguments": {"query": "scan me"},
}
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.return_value = MagicMock(action="NONE", output=[], outputs=[], assessments=[])
await guardrail.async_moderation_hook(
data=data,
user_api_key_dict=UserAPIKeyAuth(api_key="sk-test", user_id="u"),
call_type=call_type,
)
assert (mock_api.call_count == 1) is should_scan, (
f"mode={mode} call_type={call_type}: expected scan={should_scan}, "
f"bedrock api called {mock_api.call_count} times"
)
if should_scan:
expected_event = (
GuardrailEventHooks.during_mcp_call
if call_type == CallTypes.call_mcp_tool.value
else GuardrailEventHooks.during_call
)
assert mock_api.call_args.kwargs["logging_event_type"] == expected_event, (
"the scan must be logged under the event it actually ran for, so guardrail logs, "
"OTel spans, and Langfuse metadata do not misclassify MCP enforcement as an LLM call"
)
class TestScanOnlyToolResultsWithLatestRoleFilter:
@pytest.mark.asyncio
async def test_warns_and_skips_when_scoped_payload_has_no_user_message(self):
"""scan_only_tool_results hands Bedrock a tool-role-only payload, but
experimental_use_latest_role_message_only scans only the latest user
message: the silent no-op must warn."""
guardrail = BedrockGuardrail(
guardrail_name="bedrock-latest-role-scoped",
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
default_on=True,
experimental_use_latest_role_message_only=True,
)
guardrail.scan_only_tool_results = True
inputs = {
"texts": ["TOOL-RESULT"],
"structured_messages": [{"role": "tool", "tool_call_id": "call_1", "content": "TOOL-RESULT"}],
}
with (
patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api,
patch(
"litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails.verbose_proxy_logger.warning"
) as mock_warning,
):
result = await guardrail.apply_guardrail(
inputs=inputs,
request_data={"litellm_call_id": "test-call-id"},
input_type="request",
)
mock_api.assert_not_called()
assert result["texts"] == ["TOOL-RESULT"]
warning_text = " ".join(str(arg) for c in mock_warning.call_args_list for arg in c.args)
assert "scan_only_tool_results" in warning_text
@pytest.mark.parametrize("separator", ["\n", "\t", "\r\n", " "])
def test_split_bedrock_content_splits_on_any_whitespace_not_just_space(separator):
"""Regression: the midpoint split must land on any Unicode whitespace, not only an
ASCII space.
Matching only " " left the boundary unguarded for exactly the payloads that grow
large enough to need splitting: JSON lines, source code, logs and transcripts are
newline or tab delimited. A deny-listed word sitting at the midpoint of one was cut
in half, scanned clean on both fragments, and reassembled intact, which is the
single-token detection bypass the whitespace split exists to close."""
text = separator.join(["aaaaaaa"] * 4) + separator + "BADWORDXYZ" + separator + separator.join(["bbbbbbb"] * 4)
first, second = BedrockGuardrail._split_bedrock_content([BedrockContentItem(text=BedrockTextContent(text=text))])
first_text = first[0]["text"]["text"]
second_text = second[0]["text"]["text"]
assert first_text + second_text == text, "split must stay lossless"
assert "BADWORDXYZ" in first_text or "BADWORDXYZ" in second_text, "split severed the token"
def test_merge_bedrock_responses_preserves_fields_the_merge_has_no_opinion_on():
"""Regression: merging must not drop AWS response fields it does not itself merge.
The merged response used to be rebuilt from an empty dict holding only action,
outputs, assessments and usage, so actionReason, guardrailCoverage and anything AWS
adds later vanished from the guardrail_json_response the Admin UI renders, on every
ApplyGuardrail request rather than only chunked ones."""
chunk = BedrockContentChunkResult(
response={
"action": "NONE",
"actionReason": "No action.",
"guardrailCoverage": {"textCharacters": {"guarded": 41, "total": 41}},
"usage": {"contentPolicyUnits": 1},
},
content=[BedrockContentItem(text=BedrockTextContent(text="hello"))],
fragment_group_size=1,
)
merged = BedrockGuardrail._merge_bedrock_guardrail_responses([chunk])
assert merged["actionReason"] == "No action."
assert merged["guardrailCoverage"] == {"textCharacters": {"guarded": 41, "total": 41}}
def test_merge_bedrock_usage_sums_counters_not_on_the_known_list():
"""Regression: usage counters were summed from a hardcoded list of six keys, so the
ones AWS also returns (contentPolicyImageUnits, the automatedReasoning pair) were
reported as absent no matter what the chunks actually used."""
chunks = [
BedrockContentChunkResult(
response={"action": "NONE", "usage": {"contentPolicyImageUnits": units, "contentPolicyUnits": 1}},
content=[BedrockContentItem(text=BedrockTextContent(text="x"))],
fragment_group_size=1,
)
for units in (3, 4)
]
usage = BedrockGuardrail._merge_bedrock_guardrail_responses(chunks)["usage"]
assert usage["contentPolicyImageUnits"] == 7
assert usage["contentPolicyUnits"] == 2
@pytest.mark.asyncio
async def test_apply_guardrail_exception_inside_200_logs_failure_and_proceeds():
"""Regression: AWS can report a failure inside an HTTP 200 body via Output.__type,
and that must be logged as guardrail_failed_to_respond rather than success.
Real AWS does this: an unrecognised operation path on bedrock-runtime answers
HTTP 200 with {"Output": {"__type": "com.amazon.coral.service#UnknownOperationException"}}.
Consolidating telemetry had replaced the derived status with a hardcoded "success",
which reported a failed scan as a clean one. The request itself still proceeds, which
is the behaviour of the code before chunking existed."""
guardrail = _bedrock_guardrail_for_chunk_tests()
exception_response = MagicMock()
exception_response.status_code = 200
exception_response.json.return_value = {
"Output": {"__type": "com.amazon.coral.service#UnknownOperationException"},
"Version": "1.0",
}
exception_response.text = json.dumps(exception_response.json.return_value)
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch.object(guardrail, "add_standard_logging_guardrail_information_to_request_data") as mock_log,
):
mock_post.return_value = exception_response
result = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=[{"role": "user", "content": "hello"}],
request_data={"model": "bedrock-nova-micro"},
)
assert result is not None, "the request proceeds, as it did before chunking existed"
mock_log.assert_called_once()
assert mock_log.call_args.kwargs["guardrail_status"] == "guardrail_failed_to_respond"
@pytest.mark.asyncio
async def test_apply_guardrail_failure_logs_a_dict_not_a_bare_string():
"""Regression: the consolidated failure logger must log guardrail_json_response as a
dict, the shape the pre-chunking code and the InvokeGuardrailChecks path both use.
Consolidating telemetry had changed it to a bare string on the ApplyGuardrail path
only, which breaks any consumer that reads it as a mapping and leaves the two paths
in this file inconsistent."""
guardrail = _bedrock_guardrail_for_chunk_tests()
mock_credentials = MagicMock()
mock_credentials.access_key = "k"
mock_credentials.secret_key = "s"
mock_credentials.token = None
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
patch.object(guardrail, "add_standard_logging_guardrail_information_to_request_data") as mock_log,
):
mock_post.side_effect = _raised_bedrock_error(400, "guardrailIdentifier is not valid")
with pytest.raises(HTTPException):
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=[{"role": "user", "content": "hello"}],
request_data={"model": "bedrock-nova-micro"},
)
mock_log.assert_called_once()
logged = mock_log.call_args.kwargs["guardrail_json_response"]
assert isinstance(logged, dict), f"expected a dict, got {type(logged).__name__}"
assert "error" in logged
def test_build_tracing_detail_surfaces_usage_counters_and_cost(monkeypatch):
"""LIT-5650/LIT-5651: AWS-billed usage must land as guardrail_usage priced into guardrail_cost."""
monkeypatch.setattr(
litellm,
"model_cost",
{
"bedrock/guardrails": {
"guardrail_cost_per_unit": {
"topicPolicyUnits": 0.00015,
"contentPolicyUnits": 0.00015,
"wordPolicyUnits": 0.0,
}
}
},
)
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
detail = guardrail._build_tracing_detail(
{
"action": "GUARDRAIL_INTERVENED",
"usage": {
"topicPolicyUnits": 1,
"contentPolicyUnits": 2,
"wordPolicyUnits": 0,
"someFutureCounter": 3,
"oddball": "not-an-int",
},
},
aws_region_name="us-east-1",
)
assert detail["guardrail_usage"] == {
"topicPolicyUnits": 1,
"contentPolicyUnits": 2,
"wordPolicyUnits": 0,
"someFutureCounter": 3,
}
assert detail["guardrail_cost"] == pytest.approx(0.00045)
by_unit = detail["guardrail_cost_by_unit"]
assert by_unit is not None and by_unit.keys() == detail["guardrail_usage"].keys()
assert by_unit["topicPolicyUnits"] == pytest.approx(0.00015)
assert by_unit["contentPolicyUnits"] == pytest.approx(0.0003)
assert by_unit["wordPolicyUnits"] == 0.0
assert by_unit["someFutureCounter"] is None
assert by_unit["wordPolicyUnits"] == 0.0
def test_build_tracing_detail_omits_cost_by_unit_when_unpriced_but_keeps_scalar_zero(monkeypatch):
"""LIT-5652: without a cost-map entry the spend path still bills 0.0, but the
per-counter stamp must be absent so the rollup records NULL, not $0."""
monkeypatch.setattr(litellm, "model_cost", {})
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
detail = guardrail._build_tracing_detail(
{"action": "NONE", "usage": {"contentPolicyUnits": 5}}, aws_region_name="us-east-1"
)
assert detail["guardrail_usage"] == {"contentPolicyUnits": 5}
assert detail["guardrail_cost"] == 0.0
assert "guardrail_cost_by_unit" not in detail
def test_build_tracing_detail_omits_guardrail_usage_when_bedrock_reports_none():
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
for detail in (
guardrail._build_tracing_detail({"action": "NONE"}, aws_region_name="us-east-1"),
guardrail._build_tracing_detail({"action": "NONE", "usage": {}}, aws_region_name="us-east-1"),
):
assert "guardrail_usage" not in detail
assert "guardrail_cost" not in detail
assert "guardrail_cost_by_unit" not in detail
@pytest.mark.asyncio
async def test_blocked_chunk_logs_usage_and_cost_of_prior_passed_chunks(monkeypatch):
"""LIT-5651 regression: a block on a later chunk must still bill the chunks AWS already processed."""
monkeypatch.setattr(
litellm,
"model_cost",
{
"bedrock/guardrails": {
"guardrail_cost_per_unit": {
"contentPolicyUnits": 0.00015,
"wordPolicyUnits": 0.0,
}
}
},
)
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
chunk_budget_chars=40,
)
too_large_response = MagicMock()
too_large_response.status_code = 429
too_large_response.json.return_value = {
"message": "Input text size (60 text units) exceeds the maximum allowed (1 text units) for the content filter policy"
}
passed_chunk_response = MagicMock()
passed_chunk_response.status_code = 200
passed_chunk_response.json.return_value = {
"action": "NONE",
"outputs": [],
"assessments": [],
"usage": {"contentPolicyUnits": 2, "wordPolicyUnits": 1},
}
blocked_chunk_response = MagicMock()
blocked_chunk_response.status_code = 200
blocked_chunk_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [{"contentPolicy": {"filters": [{"type": "HATE", "confidence": "HIGH", "action": "BLOCKED"}]}}],
"outputs": [{"text": "Content blocked"}],
"usage": {"contentPolicyUnits": 3},
}
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
request_data = {
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "a" * 30},
{"role": "user", "content": "b" * 30},
],
}
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = [too_large_response, passed_chunk_response, blocked_chunk_response]
with pytest.raises(HTTPException):
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=request_data["messages"],
request_data=request_data,
)
assert mock_post.call_count == 3
logged_entries = request_data["metadata"]["standard_logging_guardrail_information"]
assert len(logged_entries) == 1
logged = logged_entries[0]
assert logged["guardrail_usage"] == {"contentPolicyUnits": 5, "wordPolicyUnits": 1}
assert logged["guardrail_cost"] == pytest.approx(0.00075)
assert logged["guardrail_response"]["usage"] == {"contentPolicyUnits": 5, "wordPolicyUnits": 1}
@pytest.mark.asyncio
async def test_terminal_failure_logs_usage_and_cost_of_prior_passed_chunks(monkeypatch):
"""LIT-5651 regression: a terminal failure on a later chunk must still bill the chunks AWS already processed."""
monkeypatch.setattr(
litellm,
"model_cost",
{
"bedrock/guardrails": {
"guardrail_cost_per_unit": {
"contentPolicyUnits": 0.00015,
"wordPolicyUnits": 0.0,
}
}
},
)
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
chunk_budget_chars=40,
)
too_large_response = MagicMock()
too_large_response.status_code = 429
too_large_response.json.return_value = {
"message": "Input text size (60 text units) exceeds the maximum allowed (1 text units) for the content filter policy"
}
passed_chunk_response = MagicMock()
passed_chunk_response.status_code = 200
passed_chunk_response.json.return_value = {
"action": "NONE",
"outputs": [],
"assessments": [],
"usage": {"contentPolicyUnits": 2, "wordPolicyUnits": 1},
}
failed_chunk_response = MagicMock()
failed_chunk_response.status_code = 400
failed_chunk_response.json.return_value = {"message": "ValidationException: guardrail is in a failed state"}
mock_credentials = MagicMock()
mock_credentials.access_key = "test-access-key"
mock_credentials.secret_key = "test-secret-key"
mock_credentials.token = None
request_data = {
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "a" * 30},
{"role": "user", "content": "b" * 30},
],
}
with (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post,
patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
):
mock_post.side_effect = [too_large_response, passed_chunk_response, failed_chunk_response]
with pytest.raises(HTTPException):
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=request_data["messages"],
request_data=request_data,
)
assert mock_post.call_count == 3
logged_entries = request_data["metadata"]["standard_logging_guardrail_information"]
assert len(logged_entries) == 1
logged = logged_entries[0]
assert logged["guardrail_status"] == "guardrail_failed_to_respond"
assert logged["guardrail_usage"] == {"contentPolicyUnits": 2, "wordPolicyUnits": 1}
assert logged["guardrail_cost"] == pytest.approx(0.0003)
assert logged["guardrail_response"]["usage"] == {"contentPolicyUnits": 2, "wordPolicyUnits": 1}
assert "error" in logged["guardrail_response"]
def test_load_credentials_assumes_role_with_external_id():
"""A trust policy requiring sts:ExternalId must be satisfied by the guardrail's aws_external_id."""
import datetime
import boto3
from botocore.exceptions import ClientError
class FakeSTSClient:
"""STS that mirrors a cross-account role whose trust policy requires an ExternalId."""
def get_caller_identity(self):
return {"Arn": "arn:aws:iam::111111111111:user/litellm-proxy-pod"}
def assume_role(self, **params):
if params.get("ExternalId") != "external-id-123":
raise ClientError(
{"Error": {"Code": "AccessDenied", "Message": "is not authorized to perform: sts:AssumeRole"}},
"AssumeRole",
)
return {
"Credentials": {
"AccessKeyId": "ASIAASSUMEDROLEKEY",
"SecretAccessKey": "assumed-secret",
"SessionToken": "assumed-session-token",
"Expiration": datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=30),
}
}
guardrail = BedrockGuardrail(
guardrail_name="bedrock-external-id",
event_hook=GuardrailEventHooks.pre_call,
guardrailIdentifier="gr-1",
guardrailVersion="DRAFT",
aws_region_name="us-east-1",
aws_access_key_id="AKIAPODCALLERKEY",
aws_secret_access_key="pod-caller-secret",
aws_role_name="arn:aws:iam::999999999999:role/litellm-guardrail-role",
aws_session_name="litellm-session",
aws_external_id="external-id-123",
)
with patch.object(boto3, "client", return_value=FakeSTSClient()):
credentials, aws_region_name = guardrail._load_credentials()
assert credentials.access_key == "ASIAASSUMEDROLEKEY"
assert credentials.token == "assumed-session-token"
assert aws_region_name == "us-east-1"
def test_initialize_bedrock_forwards_aws_external_id():
"""aws_external_id configured on the guardrail must survive LitellmParams and the initializer."""
from litellm.proxy.guardrails.guardrail_initializers import initialize_bedrock
from litellm.types.guardrails import LitellmParams
litellm_params = LitellmParams(
guardrail="bedrock",
mode="pre_call",
guardrailIdentifier="gr-1",
guardrailVersion="DRAFT",
aws_region_name="us-east-1",
aws_role_name="arn:aws:iam::999999999999:role/litellm-guardrail-role",
aws_external_id="external-id-123",
)
guardrail = initialize_bedrock(litellm_params, {"guardrail_name": "bedrock-external-id"})
try:
assert guardrail.optional_params["aws_external_id"] == "external-id-123"
finally:
litellm.logging_callback_manager.remove_callback_from_list_by_object(litellm.callbacks, guardrail)
def _chat_chunk(content: str, finish_reason: str | None) -> litellm.ModelResponseStream:
return litellm.ModelResponseStream(
id="tid",
choices=[
litellm.types.utils.StreamingChoices(
delta=litellm.types.utils.Delta(content=content, role="assistant"),
finish_reason=finish_reason,
index=0,
)
],
created=1,
model="gpt-4o-mini",
object="chat.completion.chunk",
)
def _streaming_litellm_params(**extras):
from litellm.types.guardrails import LitellmParams
return LitellmParams(
guardrail="bedrock",
mode="post_call",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
**extras,
)
def test_initialize_bedrock_wires_streaming_flags():
from litellm.proxy.guardrails.guardrail_initializers import initialize_bedrock
configured = initialize_bedrock(
_streaming_litellm_params(
streaming_buffer_until_moderated=False,
streaming_sampling_rate=3,
streaming_end_of_stream_only=True,
streaming_buffer_release_on_scan=True,
),
{"guardrail_name": "bedrock-streaming"},
)
defaulted = initialize_bedrock(
_streaming_litellm_params(),
{"guardrail_name": "bedrock-defaults"},
)
for registered in (configured, defaulted):
litellm.logging_callback_manager.remove_callback_from_list_by_object(litellm.callbacks, registered)
assert configured.streaming_buffer_until_moderated is False
assert configured.streaming_sampling_rate == 3
assert configured.streaming_end_of_stream_only is True
assert configured.streaming_buffer_release_on_scan is True
assert defaulted.streaming_buffer_until_moderated is True
assert defaulted.streaming_sampling_rate == 5
assert defaulted.streaming_end_of_stream_only is False
assert defaulted.streaming_buffer_release_on_scan is False
def test_initialize_bedrock_rejects_non_positive_sampling_rate():
from pydantic import ValidationError
from litellm.proxy.guardrails.guardrail_initializers import initialize_bedrock
with pytest.raises(ValidationError):
initialize_bedrock(
_streaming_litellm_params(streaming_sampling_rate=0),
{"guardrail_name": "bedrock-bad-rate"},
)
def test_update_in_memory_litellm_params_round_trips_streaming_flags():
guardrail = BedrockGuardrail(
guardrail_name="bedrock-update",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
)
guardrail.update_in_memory_litellm_params(
_streaming_litellm_params(
streaming_buffer_until_moderated=False,
streaming_sampling_rate=7,
streaming_end_of_stream_only=True,
)
)
assert guardrail.streaming_buffer_until_moderated is False
assert guardrail.streaming_sampling_rate == 7
assert guardrail.streaming_end_of_stream_only is True
guardrail.update_in_memory_litellm_params(_streaming_litellm_params())
assert guardrail.streaming_buffer_until_moderated is True
assert guardrail.streaming_sampling_rate == 5
assert guardrail.streaming_end_of_stream_only is False
async def _run_streaming_hook_recording_order(guardrail: BedrockGuardrail) -> list:
events = []
minimal = {"action": "NONE", "assessments": [], "outputs": []}
async def record_scan(*args, **kwargs):
events.append("scan")
return minimal
async def mock_stream():
yield _chat_chunk("Hello", None)
yield _chat_chunk(" world", None)
yield _chat_chunk("", "stop")
with patch.object(guardrail, "make_bedrock_api_request", AsyncMock(side_effect=record_scan)):
async for chunk in guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(),
response=mock_stream(),
request_data={"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "hi"}]},
):
content = chunk.choices[0].delta.content if chunk.choices else None
events.append(("chunk", content))
return events
@pytest.mark.asyncio
async def test_unbuffered_end_of_stream_hook_yields_chunks_before_scan():
guardrail = BedrockGuardrail(
guardrail_name="bedrock-audit-mode",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
streaming_buffer_until_moderated=False,
streaming_end_of_stream_only=True,
)
events = await _run_streaming_hook_recording_order(guardrail)
scan_index = events.index("scan")
chunk_events = [e for e in events if e != "scan"]
assert events.count("scan") == 1
assert [e for e in events[:scan_index] if e != "scan"] == chunk_events[:scan_index]
assert ("chunk", "Hello") in events[:scan_index]
assert ("chunk", " world") in events[:scan_index]
assert len(chunk_events) == 3
@pytest.mark.asyncio
async def test_unbuffered_end_of_stream_hook_scans_released_chunks_when_the_client_closes_early():
guardrail = BedrockGuardrail(
guardrail_name="bedrock-audit-mode",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
streaming_buffer_until_moderated=False,
streaming_end_of_stream_only=True,
)
scans = []
async def record_scan(*args, **kwargs):
scans.append(kwargs["source"])
return {"action": "NONE", "assessments": [], "outputs": []}
async def mock_stream():
yield _chat_chunk("Hello", None)
yield _chat_chunk(" world", None)
yield _chat_chunk("", "stop")
with patch.object(guardrail, "make_bedrock_api_request", AsyncMock(side_effect=record_scan)):
stream = guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(),
response=mock_stream(),
request_data={"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "hi"}]},
)
first = await stream.__anext__()
await stream.aclose()
assert first.choices[0].delta.content == "Hello"
assert scans == ["OUTPUT"]
@pytest.mark.asyncio
async def test_buffered_default_hook_scans_before_any_chunk():
guardrail = BedrockGuardrail(
guardrail_name="bedrock-buffered-default",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
)
events = await _run_streaming_hook_recording_order(guardrail)
assert events[0] == "scan"
assert all(e == "scan" or e[0] == "chunk" for e in events)
assert len([e for e in events if e != "scan"]) >= 1
@pytest.mark.asyncio
async def test_buffered_release_on_scan_hook_releases_each_window_after_its_scan():
guardrail = BedrockGuardrail(
guardrail_name="bedrock-release-on-scan",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
streaming_buffer_release_on_scan=True,
streaming_sampling_rate=1,
)
assert guardrail._streams_incrementally() is True
events = await _run_streaming_hook_recording_order(guardrail)
assert events == ["scan", ("chunk", "Hello"), "scan", ("chunk", " world"), ("chunk", "")]
@pytest.mark.asyncio
async def test_buffered_release_on_scan_defers_to_end_of_stream_only():
guardrail = BedrockGuardrail(
guardrail_name="bedrock-release-on-scan-end-only",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
streaming_buffer_release_on_scan=True,
streaming_end_of_stream_only=True,
streaming_sampling_rate=1,
)
assert guardrail._streams_incrementally() is False
events = await _run_streaming_hook_recording_order(guardrail)
assert events.count("scan") == 1
assert events[0] == "scan"
@pytest.mark.asyncio
async def test_masking_keeps_buffered_path_even_when_unbuffered_configured():
guardrail = BedrockGuardrail(
guardrail_name="bedrock-mask-buffered",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
mask_response_content=True,
streaming_buffer_until_moderated=False,
streaming_end_of_stream_only=True,
)
assert guardrail._streams_incrementally() is False
events = await _run_streaming_hook_recording_order(guardrail)
assert events[0] == "scan"
@pytest.mark.asyncio
async def test_streaming_end_of_stream_block_emits_error_frame_instead_of_truncating():
"""Regression for PR #38722: a topicPolicy DENY caught by the end-of-stream
scan used to raise after SSE headers were flushed, so the client saw a
silently truncated stream. The unified hook must emit the chat in-stream
error frame instead. The finish chunk is withheld while the end-of-stream
scan runs, so on a block it is dropped rather than relayed before the
frame."""
from litellm.proxy.guardrails.guardrail_hooks.unified_guardrail.unified_guardrail import (
UnifiedLLMGuardrails,
)
from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
streaming_end_of_stream_only=True,
streaming_buffer_until_moderated=False,
guardrail_name="bedrock-eos",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
)
blocked_response = {
"action": "GUARDRAIL_INTERVENED",
"actionReason": "Guardrail blocked.",
"outputs": [{"text": "Sorry, the model cannot answer this question."}],
"assessments": [
{"topicPolicy": {"topics": [{"name": "Forbidden topic", "type": "DENY", "action": "BLOCKED"}]}}
],
}
def _chunk(content, finish_reason=None):
return ModelResponseStream(
choices=[
StreamingChoices(
index=0,
delta={"content": content, "role": "assistant"},
finish_reason=finish_reason,
)
],
)
async def _mock_stream():
yield _chunk("the forbidden ")
yield _chunk("topic answer", finish_reason="stop")
with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:
mock_api.side_effect = guardrail._get_http_exception_for_blocked_guardrail(blocked_response)
out = []
async for item in UnifiedLLMGuardrails().async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(api_key="test", request_route="/v1/chat/completions"),
response=_mock_stream(),
request_data={"guardrail_to_apply": guardrail, "model": "gpt-4"},
):
out.append(item)
assert len(out) == 2
assert isinstance(out[0], ModelResponseStream)
assert out[0].choices[0].finish_reason is None
frame = out[-1]
assert isinstance(frame, bytes)
payload = json.loads(frame.decode()[len("data: ") :])
assert payload["error"]["message"] == "Violated guardrail policy"
assert payload["error"]["code"] == "400"
assert payload["error"]["provider_specific_fields"]["guardrailIdentifier"] == "test-guardrail"
def _responses_stream_events() -> list:
from litellm.types.llms.openai import (
OutputTextDeltaEvent,
ResponseCompletedEvent,
ResponsesAPIResponse,
ResponsesAPIStreamEvents,
)
deltas = [
OutputTextDeltaEvent(
type=ResponsesAPIStreamEvents.OUTPUT_TEXT_DELTA,
item_id="msg_lit6457",
output_index=0,
content_index=0,
delta=part,
)
for part in ("Hello", " world")
]
completed = ResponseCompletedEvent(
type=ResponsesAPIStreamEvents.RESPONSE_COMPLETED,
response=ResponsesAPIResponse(
id="resp_lit6457",
created_at=1234567890,
model="gpt-4o",
object="response",
status="completed",
output=[
{
"type": "message",
"id": "msg_lit6457",
"status": "completed",
"role": "assistant",
"content": [{"type": "output_text", "text": "Hello world"}],
}
],
),
)
return [*deltas, completed]
@pytest.mark.asyncio
async def test_responses_api_stream_scans_output_and_replays_buffered_events():
"""Streamed /v1/responses events must be scanned via the unified translation
layer, not fed to stream_chunk_builder (which raises APIError on them)."""
guardrail = BedrockGuardrail(
guardrail_name="bedrock-responses-stream",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
)
stream_events = _responses_stream_events()
order = []
yielded = []
async def record_scan(*args, **kwargs):
order.append("scan")
return {"action": "NONE", "assessments": [], "outputs": []}
async def mock_stream():
for event in stream_events:
yield event
with patch.object(guardrail, "make_bedrock_api_request", AsyncMock(side_effect=record_scan)):
async for chunk in guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(api_key="test-key", request_route="/v1/responses"),
response=mock_stream(),
request_data={"model": "gpt-4o", "input": "hi"},
):
order.append("chunk")
yielded.append(chunk)
assert order == ["scan", "chunk", "chunk", "chunk"]
assert len(yielded) == len(stream_events)
assert all(emitted is original for emitted, original in zip(yielded, stream_events))
def _responses_failed_stream_events() -> list:
from litellm.types.llms.openai import (
OutputTextDeltaEvent,
ResponseFailedEvent,
ResponsesAPIResponse,
ResponsesAPIStreamEvents,
)
deltas = [
OutputTextDeltaEvent(
type=ResponsesAPIStreamEvents.OUTPUT_TEXT_DELTA,
item_id="msg_lit6457_failed",
output_index=0,
content_index=0,
delta=part,
)
for part in ("Hello", " world")
]
failed = ResponseFailedEvent(
type=ResponsesAPIStreamEvents.RESPONSE_FAILED,
response=ResponsesAPIResponse(
id="resp_lit6457_failed",
created_at=1234567890,
model="gpt-4o",
object="response",
status="failed",
output=[],
),
)
return [*deltas, failed]
@pytest.mark.asyncio
async def test_responses_api_failed_stream_scans_delta_text_before_replay():
"""A responses stream that dies mid-generation carries its text only in delta
events; the end-of-stream scan must still see that text instead of skipping
on an empty assembled string and replaying the buffer unmoderated."""
guardrail = BedrockGuardrail(
guardrail_name="bedrock-responses-failed-stream",
guardrailIdentifier="test-id",
guardrailVersion="DRAFT",
event_hook=GuardrailEventHooks.post_call,
default_on=True,
)
stream_events = _responses_failed_stream_events()
order = []
scan_payloads = []
yielded = []
async def record_scan(*args, **kwargs):
order.append("scan")
scan_payloads.append(str(args) + str(kwargs))
return {"action": "NONE", "assessments": [], "outputs": []}
async def mock_stream():
for event in stream_events:
yield event
with patch.object(guardrail, "make_bedrock_api_request", AsyncMock(side_effect=record_scan)):
async for chunk in guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=UserAPIKeyAuth(api_key="test-key", request_route="/v1/responses"),
response=mock_stream(),
request_data={"model": "gpt-4o", "input": "hi"},
):
order.append("chunk")
yielded.append(chunk)
assert order == ["scan", "chunk", "chunk", "chunk"]
assert "Hello world" in scan_payloads[0]
assert len(yielded) == len(stream_events)
assert all(emitted is original for emitted, original in zip(yielded, stream_events))
@pytest.mark.asyncio
async def test_apply_guardrail_debug_log_masks_signed_request_headers():
import logging
from litellm._logging import verbose_proxy_logger
session_token = "FakeSessionTokenValueThatMustNeverAppearInLogs1234567890"
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
aws_access_key_id="ASIAFAKEACCESSKEYID1",
aws_secret_access_key="fakeSecretAccessKeyForSigning",
aws_session_token=session_token,
aws_region_name="us-east-1",
)
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"action": "NONE", "outputs": []}
captured_records: list[logging.LogRecord] = []
class _RecordingHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
captured_records.append(record)
handler = _RecordingHandler(level=logging.DEBUG)
previous_level = verbose_proxy_logger.level
verbose_proxy_logger.addHandler(handler)
verbose_proxy_logger.setLevel(logging.DEBUG)
try:
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
mock_post.return_value = mock_response
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=[{"role": "user", "content": "hello"}],
request_data={},
)
finally:
verbose_proxy_logger.removeHandler(handler)
verbose_proxy_logger.setLevel(previous_level)
rendered_messages = [record.getMessage() for record in captured_records]
header_lines = [message for message in rendered_messages if "headers:" in message]
assert header_lines, "expected the signed-request debug line to be logged"
assert any("X-Amz-Security-Token" in message for message in header_lines)
assert all(session_token not in message for message in rendered_messages)
@pytest.mark.asyncio
async def test_bearer_token_never_runs_the_sigv4_credential_chain(monkeypatch):
"""The guardrail's AWS profile does not exist, so resolving SigV4 credentials
raises; with a bearer token configured the guardrail must still run, since
the bearer token alone signs the request."""
monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", "env-bearer-token-12345")
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
aws_profile_name="litellm-no-such-aws-profile",
)
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"action": "NONE", "assessments": []}
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock, return_value=mock_response) as mock_post:
response = await guardrail.make_bedrock_api_request(
source="INPUT", messages=[{"role": "user", "content": "hello"}]
)
assert response["action"] == "NONE"
assert mock_post.call_args.kwargs["headers"]["Authorization"] == "Bearer env-bearer-token-12345"
@pytest.mark.asyncio
async def test_apply_guardrail_signs_off_the_event_loop(monkeypatch):
"""Regression for issue #40165: the ApplyGuardrail request is signed with SigV4, and botocore
refreshes expiring credentials inside that signing with a blocking HTTP call, so it must run
on a worker thread to keep the loop serving other requests."""
monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False)
guardrail = BedrockGuardrail(guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT")
probe = EventLoopProbe()
allowed = httpx.Response(
200,
json={"action": "NONE", "outputs": [], "assessments": []},
request=httpx.Request("POST", "https://bedrock-runtime.us-east-1.amazonaws.com"),
)
with patch.object(guardrail.async_handler, "post", new=AsyncMock(return_value=allowed)):
release = asyncio.create_task(probe.release_refresh_from_the_loop())
response = await guardrail._post_apply_guardrail_content(
content=[{"text": {"text": "hello"}}],
base_request_data={"source": "INPUT"},
credentials=probe.credentials(),
aws_region_name="us-east-1",
api_key=None,
request_data={},
event_type=GuardrailEventHooks.pre_call,
start_time=datetime.now(timezone.utc),
completed_chunk_usages=[],
)
await release
assert response["action"] == "NONE"
assert probe.served_during_refresh is True
_ATTACHMENT_PNG_B64 = "iVBORw0KGgpmYWtlLXBuZw=="
_ATTACHMENT_PDF_URI = "data:application/pdf;base64,JVBERi0xLjQgZmFrZQ=="
def _image_only_chat_request() -> dict:
return {
"model": "claude",
"messages": [
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{_ATTACHMENT_PNG_B64}"}}
],
}
],
}
def _pdf_chat_request() -> dict:
return {
"model": "claude",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "summarize"},
{"type": "file", "file": {"file_data": _ATTACHMENT_PDF_URI}},
],
}
],
}
def _attachment_guardrail(**kwargs) -> BedrockGuardrail:
return BedrockGuardrail(
guardrail_name="bedrock-attachments",
guardrailIdentifier="gid",
guardrailVersion="DRAFT",
**kwargs,
)
def _patched_bedrock_post(guardrail: BedrockGuardrail, response: MagicMock):
credentials = MagicMock(access_key="k", secret_key="s", token=None)
return (
patch.object(guardrail.async_handler, "post", new_callable=AsyncMock, return_value=response),
patch.object(guardrail, "_load_credentials", return_value=(credentials, "us-east-1")),
patch.object(guardrail, "_prepare_request", return_value=MagicMock()),
)
@pytest.mark.asyncio
async def test_attachment_scan_sends_image_only_turn_to_apply_guardrail_and_blocks():
guardrail = _attachment_guardrail()
post_patch, credentials_patch, prepare_patch = _patched_bedrock_post(
guardrail, _blocking_bedrock_httpx_response("violence")
)
with post_patch as mock_post, credentials_patch, prepare_patch as mock_prepare:
with pytest.raises(HTTPException) as exc_info:
await guardrail.async_scan_request_attachments(
data=_image_only_chat_request(), call_type=CallTypes.acompletion.value
)
assert exc_info.value.status_code == 400
assert mock_post.await_count == 1
sent_body = mock_prepare.call_args.kwargs["data"]
assert sent_body["source"] == "INPUT"
assert sent_body["content"] == ({"image": {"format": "png", "source": {"bytes": _ATTACHMENT_PNG_B64}}},)
@pytest.mark.asyncio
async def test_attachment_scan_lets_a_passing_image_through():
guardrail = _attachment_guardrail()
data = _image_only_chat_request()
post_patch, credentials_patch, prepare_patch = _patched_bedrock_post(
guardrail, _passing_bedrock_httpx_response("ok")
)
with post_patch as mock_post, credentials_patch, prepare_patch:
await guardrail.async_scan_request_attachments(data=data, call_type=CallTypes.acompletion.value)
assert mock_post.await_count == 1
logged = data["metadata"]["standard_logging_guardrail_information"]
assert [entry["guardrail_status"] for entry in logged] == ["success"]
@pytest.mark.asyncio
async def test_attachment_scan_blocks_unscannable_attachment_without_calling_bedrock():
guardrail = _attachment_guardrail()
data = _pdf_chat_request()
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
with pytest.raises(HTTPException) as exc_info:
await guardrail.async_scan_request_attachments(data=data, call_type=CallTypes.acompletion.value)
assert exc_info.value.status_code == 400
assert exc_info.value.detail["error"] == "Violated guardrail policy"
assert "cannot scan 1 attachment(s) (file)" in exc_info.value.detail["reason"]
assert exc_info.value.detail["guardrailIdentifier"] == "gid"
mock_post.assert_not_awaited()
logged = data["metadata"]["standard_logging_guardrail_information"]
assert logged[0]["guardrail_status"] == "guardrail_intervened"
assert logged[0]["guardrail_response"] == {"unscannable_attachments": ["file"]}
@pytest.mark.asyncio
async def test_attachment_scan_skip_unscannable_attachments_lets_documents_through():
guardrail = _attachment_guardrail(skip_unscannable_attachments=True)
data = _pdf_chat_request()
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
result = await guardrail.async_scan_request_attachments(data=data, call_type=CallTypes.acompletion.value)
assert result is None
assert data == _pdf_chat_request()
mock_post.assert_not_awaited()
@pytest.mark.asyncio
async def test_attachment_scan_block_returns_a_response_when_exceptions_are_disabled():
guardrail = _attachment_guardrail(disable_exception_on_block=True)
with pytest.raises(ModifyResponseException) as exc_info:
await guardrail.async_scan_request_attachments(data=_pdf_chat_request(), call_type=CallTypes.acompletion.value)
assert "cannot scan 1 attachment(s) (file)" in exc_info.value.message
@pytest.mark.asyncio
async def test_attachment_scan_treats_images_as_unscannable_in_checks_mode():
guardrail = BedrockGuardrail(guardrail_name="bedrock-checks", checks={"contentFilter": {}})
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
with pytest.raises(HTTPException) as exc_info:
await guardrail.async_scan_request_attachments(
data=_image_only_chat_request(), call_type=CallTypes.acompletion.value
)
assert "cannot scan 1 attachment(s) (image)" in exc_info.value.detail["reason"]
mock_post.assert_not_awaited()
@pytest.mark.asyncio
async def test_attachment_scan_does_nothing_for_text_only_requests():
guardrail = _attachment_guardrail()
data = {"model": "claude", "messages": [{"role": "user", "content": [{"type": "text", "text": "hi"}]}]}
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
await guardrail.async_scan_request_attachments(data=data, call_type=CallTypes.acompletion.value)
mock_post.assert_not_awaited()
assert "metadata" not in data
@pytest.mark.asyncio
async def test_during_call_hook_scans_image_only_turn():
guardrail = _attachment_guardrail(event_hook=GuardrailEventHooks.during_call, default_on=True)
post_patch, credentials_patch, prepare_patch = _patched_bedrock_post(
guardrail, _blocking_bedrock_httpx_response("violence")
)
with post_patch as mock_post, credentials_patch, prepare_patch:
with pytest.raises(HTTPException):
await guardrail.async_moderation_hook(
data=_image_only_chat_request(),
user_api_key_dict=UserAPIKeyAuth(),
call_type=CallTypes.acompletion.value,
)
assert mock_post.await_count == 1
@pytest.mark.asyncio
async def test_attachment_scan_in_checks_mode_never_posts_images_even_when_skipping():
guardrail = BedrockGuardrail(
guardrail_name="bedrock-checks", checks={"contentFilter": {}}, skip_unscannable_attachments=True
)
data = _image_only_chat_request()
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
result = await guardrail.async_scan_request_attachments(data=data, call_type=CallTypes.acompletion.value)
assert result is None
assert data == _image_only_chat_request()
mock_post.assert_not_awaited()
@pytest.mark.asyncio
async def test_attachment_scan_sends_at_most_twenty_images_per_call():
guardrail = _attachment_guardrail()
data = _image_only_chat_request()
data["messages"][0]["content"] = data["messages"][0]["content"] * 25
post_patch, credentials_patch, prepare_patch = _patched_bedrock_post(
guardrail, _passing_bedrock_httpx_response("ok")
)
with post_patch as mock_post, credentials_patch, prepare_patch as mock_prepare:
await guardrail.async_scan_request_attachments(data=data, call_type=CallTypes.acompletion.value)
assert mock_post.await_count == 2
assert [len(call.kwargs["data"]["content"]) for call in mock_prepare.call_args_list] == [20, 5]
@pytest.mark.asyncio
async def test_during_call_hook_skips_attachment_scan_when_guardrail_is_not_requested():
guardrail = _attachment_guardrail(event_hook=GuardrailEventHooks.during_call, default_on=False)
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
result = await guardrail.async_moderation_hook(
data=_pdf_chat_request(), user_api_key_dict=UserAPIKeyAuth(), call_type="acompletion"
)
assert result is None
assert mock_post.await_count == 0
@pytest.mark.asyncio
async def test_attachment_scan_honors_scan_only_tool_results():
guardrail = _attachment_guardrail()
guardrail.scan_only_tool_results = True
data = _pdf_chat_request()
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
result = await guardrail.async_scan_request_attachments(data=data, call_type=CallTypes.acompletion.value)
assert result is None
assert mock_post.await_count == 0
assert data == _pdf_chat_request()
@pytest.mark.asyncio
async def test_native_pre_call_hook_blocks_unscannable_attachment():
guardrail = _attachment_guardrail(event_hook=GuardrailEventHooks.pre_call, default_on=True)
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
with pytest.raises(HTTPException) as exc_info:
await guardrail.async_pre_call_hook(
user_api_key_dict=UserAPIKeyAuth(),
cache=MagicMock(),
data=_pdf_chat_request(),
call_type="acompletion",
)
assert exc_info.value.status_code == 400
assert "cannot scan 1 attachment(s) (file)" in exc_info.value.detail["reason"]
assert mock_post.await_count == 0
@pytest.mark.asyncio
async def test_attachment_scan_debug_log_omits_image_bytes():
guardrail = _attachment_guardrail()
post_patch, credentials_patch, prepare_patch = _patched_bedrock_post(
guardrail, _passing_bedrock_httpx_response("ok")
)
with (
post_patch,
credentials_patch,
prepare_patch,
patch("litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails.verbose_proxy_logger.debug") as mock_debug,
):
await guardrail.async_scan_request_attachments(
data=_image_only_chat_request(), call_type=CallTypes.acompletion.value
)
logged = " ".join(str(call.args) for call in mock_debug.call_args_list)
assert _ATTACHMENT_PNG_B64 not in logged
assert f"<{len(_ATTACHMENT_PNG_B64)} base64 chars>" in logged
@pytest.mark.asyncio
async def test_text_only_debug_log_prints_the_signed_request_body():
guardrail = _attachment_guardrail()
post_patch, credentials_patch, prepare_patch = _patched_bedrock_post(
guardrail, _passing_bedrock_httpx_response("ok")
)
captured_records: list[logging.LogRecord] = []
class _RecordingHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
captured_records.append(record)
handler = _RecordingHandler(level=logging.DEBUG)
previous_level = verbose_proxy_logger.level
verbose_proxy_logger.addHandler(handler)
verbose_proxy_logger.setLevel(logging.DEBUG)
try:
with post_patch, credentials_patch, prepare_patch:
await guardrail.make_bedrock_api_request(
source="INPUT", messages=[{"role": "user", "content": "hello"}], request_data={}
)
finally:
verbose_proxy_logger.removeHandler(handler)
verbose_proxy_logger.setLevel(previous_level)
body_lines = [
record.getMessage() for record in captured_records if record.getMessage().startswith("Bedrock AI request body")
]
assert len(body_lines) == 1
assert "'content': ({'text': {'text': 'hello'}},)" in body_lines[0]
def _text_document_messages_request(text: str) -> dict:
return {
"model": "claude",
"messages": [
{
"role": "user",
"content": [
{"type": "document", "source": {"type": "text", "media_type": "text/plain", "data": text}},
{"type": "text", "text": "summarize"},
],
}
],
}
def _anonymizing_bedrock_httpx_response() -> MagicMock:
response = MagicMock()
response.status_code = 200
response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "SSN {US_SOCIAL_SECURITY_NUMBER}"}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [{"type": "US_SOCIAL_SECURITY_NUMBER", "action": "ANONYMIZED"}]
}
}
],
"usage": {"contentPolicyUnits": 1},
}
return response
@pytest.mark.asyncio
@pytest.mark.parametrize(
"response, blocked",
[
pytest.param(_passing_bedrock_httpx_response("ok"), False, id="pass"),
pytest.param(_blocking_bedrock_httpx_response("deny"), True, id="block"),
pytest.param(_anonymizing_bedrock_httpx_response(), True, id="anonymize"),
],
)
async def test_attachment_scan_sends_text_document_as_text(response, blocked):
guardrail = _attachment_guardrail()
post_patch, credentials_patch, prepare_patch = _patched_bedrock_post(guardrail, response)
with post_patch as mock_post, credentials_patch, prepare_patch as mock_prepare:
if blocked:
with pytest.raises(HTTPException) as exc_info:
await guardrail.async_scan_request_attachments(
data=_text_document_messages_request("SSN 123-45-6789"),
call_type=CallTypes.anthropic_messages.value,
)
assert exc_info.value.status_code == 400
else:
result = await guardrail.async_scan_request_attachments(
data=_text_document_messages_request("SSN 123-45-6789"),
call_type=CallTypes.anthropic_messages.value,
)
assert result is None
assert mock_post.await_count == 1
sent_body = mock_prepare.call_args.kwargs["data"]
assert sent_body["source"] == "INPUT"
assert sent_body["content"] == ({"text": {"text": "SSN 123-45-6789"}},)
class _CustomApplyGuardrail(BedrockGuardrail):
async def apply_guardrail(self, inputs, request_data, input_type, logging_obj=None):
return inputs
@pytest.mark.asyncio
async def test_attachment_scan_skipped_when_subclass_overrides_apply_guardrail():
guardrail = _CustomApplyGuardrail(
guardrail_name="bedrock-attachments", guardrailIdentifier="gid", guardrailVersion="DRAFT"
)
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
pdf_result = await guardrail.async_scan_request_attachments(
data=_pdf_chat_request(), call_type=CallTypes.acompletion.value
)
image_result = await guardrail.async_scan_request_attachments(
data=_image_only_chat_request(), call_type=CallTypes.acompletion.value
)
assert pdf_result is None
assert image_result is None
assert mock_post.await_count == 0
@pytest.mark.asyncio
async def test_attachment_scan_skipped_when_instance_replaces_make_bedrock_api_request():
guardrail = BedrockGuardrail(
guardrail_name="bedrock-attachments", guardrailIdentifier="gid", guardrailVersion="DRAFT"
)
guardrail.make_bedrock_api_request = AsyncMock(return_value={"action": "NONE"})
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
pdf_result = await guardrail.async_scan_request_attachments(
data=_pdf_chat_request(), call_type=CallTypes.acompletion.value
)
assert pdf_result is None
assert mock_post.await_count == 0
class _CustomRequestGuardrail(BedrockGuardrail):
async def make_bedrock_api_request(self, source, messages=None, response=None, request_data=None, **kwargs):
return {"action": "NONE"}
@pytest.mark.asyncio
async def test_attachment_scan_skipped_when_subclass_overrides_make_bedrock_api_request():
guardrail = _CustomRequestGuardrail(
guardrail_name="bedrock-attachments", guardrailIdentifier="gid", guardrailVersion="DRAFT"
)
with patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post:
pdf_result = await guardrail.async_scan_request_attachments(
data=_pdf_chat_request(), call_type=CallTypes.acompletion.value
)
image_result = await guardrail.async_scan_request_attachments(
data=_image_only_chat_request(), call_type=CallTypes.acompletion.value
)
assert pdf_result is None
assert image_result is None
assert mock_post.await_count == 0
@pytest.mark.asyncio
async def test_bedrock_guardrails_streaming_request_body_mock(aws_test_credentials):
"""Test that the exact request body sent to Bedrock matches expected format when using streaming"""
import json
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
from litellm.caching import DualCache
from litellm.types.guardrails import GuardrailEventHooks
mock_user_api_key_dict = UserAPIKeyAuth()
mock_cache = MagicMock(spec=DualCache)
guardrail = BedrockGuardrail(
guardrailIdentifier="wf0hkdb5x07f",
guardrailVersion="DRAFT",
supported_event_hooks=[GuardrailEventHooks.post_call],
guardrail_name="bedrock-post-guard",
)
mock_response = litellm.ModelResponse(
id="test-id",
choices=[
litellm.Choices(
index=0,
message=litellm.Message(
role="assistant", content="The capital of Spain is Madrid."
),
finish_reason="stop",
)
],
created=1234567890,
model="gpt-5.5",
object="chat.completion",
)
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {"action": "NONE", "outputs": []}
with patch.object(guardrail, "async_handler") as mock_async_handler:
mock_async_handler.post = AsyncMock(return_value=mock_bedrock_response)
request_data = {
"model": "gpt-5.5",
"messages": [{"role": "user", "content": "what's the capital of spain?"}],
"stream": True,
"metadata": {"guardrails": ["bedrock-post-guard"]},
}
await guardrail.make_bedrock_api_request(
source="OUTPUT", response=mock_response, request_data=request_data
)
mock_async_handler.post.assert_called_once()
call_args = mock_async_handler.post.call_args
prepared_request_body = call_args.kwargs.get("data")
if isinstance(prepared_request_body, bytes):
actual_body = json.loads(prepared_request_body.decode("utf-8"))
else:
actual_body = json.loads(prepared_request_body)
expected_body = {
"source": "OUTPUT",
"content": [{"text": {"text": "The capital of Spain is Madrid."}}],
}
print("Actual Bedrock request body:", json.dumps(actual_body, indent=2))
print("Expected Bedrock request body:", json.dumps(expected_body, indent=2))
assert (
actual_body == expected_body
), f"Request body mismatch. Expected: {expected_body}, Got: {actual_body}"
@pytest.mark.asyncio
async def test_bedrock_guardrail_aws_param_persistence():
"""Test that AWS auth params set on init are used for every request and not popped out."""
from litellm.proxy._types import UserAPIKeyAuth
from litellm.types.guardrails import GuardrailEventHooks
guardrail = BedrockGuardrail(
guardrailIdentifier="wf0hkdb5x07f",
guardrailVersion="DRAFT",
aws_access_key_id="test-access-key",
aws_secret_access_key="test-secret-key",
aws_region_name="us-east-1",
supported_event_hooks=[GuardrailEventHooks.post_call],
guardrail_name="bedrock-post-guard",
)
with patch.object(
guardrail, "get_credentials", wraps=guardrail.get_credentials
) as mock_get_creds:
for i in range(3):
request_data = {
"model": "gpt-5.5",
"messages": [{"role": "user", "content": f"request {i}"}],
"stream": False,
"metadata": {"guardrails": ["bedrock-post-guard"]},
}
with patch.object(
guardrail.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_response = AsyncMock()
mock_response.status_code = 200
mock_response.json = MagicMock(
return_value={"action": "NONE", "outputs": []}
)
mock_post.return_value = mock_response
await guardrail.make_bedrock_api_request(
source="INPUT",
messages=request_data.get("messages"),
request_data=request_data,
)
assert mock_get_creds.call_count == 3
for call in mock_get_creds.call_args_list:
kwargs = call.kwargs
print("used the following kwargs to get credentials=", kwargs)
assert kwargs["aws_access_key_id"] == "test-access-key"
assert kwargs["aws_secret_access_key"] == "test-secret-key"
assert kwargs["aws_region_name"] == "us-east-1"
@pytest.mark.asyncio
async def test_bedrock_guardrail_blocked_vs_anonymized_actions_including_content_policy():
"""Test that BLOCKED actions raise exceptions but ANONYMIZED actions do not"""
from unittest.mock import MagicMock
from litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
BedrockGuardrail,
)
from litellm.types.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
BedrockGuardrailResponse,
)
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT"
)
anonymized_response: BedrockGuardrailResponse = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "Hello, my phone number is {PHONE}"}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "PHONE",
"match": "+1 412 555 1212",
"action": "ANONYMIZED",
}
]
}
}
],
}
should_raise = guardrail._should_raise_guardrail_blocked_exception(
anonymized_response
)
assert should_raise is False, "ANONYMIZED actions should not raise exceptions"
blocked_response: BedrockGuardrailResponse = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "I can't provide that information."}],
"assessments": [
{
"topicPolicy": {
"topics": [
{"name": "Sensitive Topic", "type": "DENY", "action": "BLOCKED"}
]
}
}
],
}
should_raise = guardrail._should_raise_guardrail_blocked_exception(blocked_response)
assert should_raise is True, "BLOCKED actions should raise exceptions"
mixed_response: BedrockGuardrailResponse = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "I can't provide that information."}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "PHONE",
"match": "+1 412 555 1212",
"action": "ANONYMIZED",
}
]
},
"topicPolicy": {
"topics": [
{"name": "Blocked Topic", "type": "DENY", "action": "BLOCKED"}
]
},
}
],
}
should_raise = guardrail._should_raise_guardrail_blocked_exception(mixed_response)
assert (
should_raise is True
), "Mixed actions with any BLOCKED should raise exceptions"
none_response: BedrockGuardrailResponse = {
"action": "NONE",
"outputs": [],
"assessments": [],
}
should_raise = guardrail._should_raise_guardrail_blocked_exception(none_response)
assert should_raise is False, "NONE actions should not raise exceptions"
content_blocked_response: BedrockGuardrailResponse = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "I can't provide that information."}],
"assessments": [
{
"contentPolicy": {
"filters": [
{"type": "VIOLENCE", "confidence": "HIGH", "action": "BLOCKED"}
]
}
}
],
}
should_raise = guardrail._should_raise_guardrail_blocked_exception(
content_blocked_response
)
assert (
should_raise is True
), "Content policy BLOCKED actions should raise exceptions"
@pytest.mark.asyncio
async def test_bedrock_guardrail_masking_with_anonymized_response(aws_test_credentials):
"""Test that masking works correctly when guardrail returns ANONYMIZED actions"""
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
from litellm.caching import DualCache
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
mask_request_content=True,
)
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "Hello, my phone number is {PHONE}"}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "PHONE",
"match": "+1 412 555 1212",
"action": "ANONYMIZED",
}
]
}
}
],
}
request_data = {
"model": "gpt-5.5",
"messages": [
{"role": "user", "content": "Hello, my phone number is +1 412 555 1212"},
],
}
with patch.object(
guardrail.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_post.return_value = mock_bedrock_response
try:
response = await guardrail.async_moderation_hook(
data=request_data,
user_api_key_dict=mock_user_api_key_dict,
call_type="completion",
)
assert response is not None
assert (
response["messages"][0]["content"]
== "Hello, my phone number is {PHONE}"
)
except Exception as e:
pytest.fail(
f"Should not raise exception for ANONYMIZED actions, but got: {e}"
)
@pytest.mark.asyncio
async def test_bedrock_guardrail_uses_masked_output_without_masking_flags(aws_test_credentials):
"""Test that masked output from guardrails is used even when masking flags are not enabled"""
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
)
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "Hello, my phone number is {PHONE} and email is {EMAIL}"}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "PHONE",
"match": "+1 412 555 1212",
"action": "ANONYMIZED",
},
{
"type": "EMAIL",
"match": "[email protected]",
"action": "ANONYMIZED",
},
]
}
}
],
}
request_data = {
"model": "gpt-5.5",
"messages": [
{
"role": "user",
"content": "Hello, my phone number is +1 412 555 1212 and email is [email protected]",
},
],
}
with patch.object(
guardrail.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_post.return_value = mock_bedrock_response
response = await guardrail.async_moderation_hook(
data=request_data,
user_api_key_dict=mock_user_api_key_dict,
call_type="completion",
)
assert response is not None
assert (
response["messages"][0]["content"]
== "Hello, my phone number is {PHONE} and email is {EMAIL}"
)
print("✅ Masked output was applied even without masking flags enabled")
@pytest.mark.asyncio
async def test_bedrock_guardrail_response_pii_masking_non_streaming(aws_test_credentials):
"""Test that PII masking is applied to response content in non-streaming scenarios"""
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
)
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [
{
"text": "My credit card number is {CREDIT_DEBIT_CARD_NUMBER} and my phone is {PHONE}"
}
],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "CREDIT_DEBIT_CARD_NUMBER",
"match": "1234-5678-9012-3456",
"action": "ANONYMIZED",
},
{
"type": "PHONE",
"match": "+1 412 555 1212",
"action": "ANONYMIZED",
},
]
}
}
],
}
mock_response = litellm.ModelResponse(
id="test-id",
choices=[
litellm.Choices(
index=0,
message=litellm.Message(
role="assistant",
content="My credit card number is 1234-5678-9012-3456 and my phone is +1 412 555 1212",
),
finish_reason="stop",
)
],
created=1234567890,
model="gpt-5.5",
object="chat.completion",
)
request_data = {
"model": "gpt-5.5",
"messages": [
{"role": "user", "content": "What's your credit card and phone number?"},
],
}
with patch.object(
guardrail.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_post.return_value = mock_bedrock_response
await guardrail.async_post_call_success_hook(
data=request_data,
user_api_key_dict=mock_user_api_key_dict,
response=mock_response,
)
assert (
mock_response.choices[0].message.content
== "My credit card number is {CREDIT_DEBIT_CARD_NUMBER} and my phone is {PHONE}"
)
print("✓ Non-streaming response PII masking test passed")
@pytest.mark.asyncio
async def test_bedrock_guardrail_response_pii_masking_streaming(aws_test_credentials):
"""Test that PII masking is applied to response content in streaming scenarios"""
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
from litellm.types.utils import ModelResponseStream
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
)
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "Sure! My email is {EMAIL} and SSN is {US_SSN}"}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "EMAIL",
"match": "[email protected]",
"action": "ANONYMIZED",
},
{
"type": "US_SSN",
"match": "123-45-6789",
"action": "ANONYMIZED",
},
]
}
}
],
}
async def mock_streaming_response():
chunks = [
ModelResponseStream(
id="test-id",
choices=[
litellm.utils.StreamingChoices(
index=0,
delta=litellm.utils.Delta(content="Sure! My email is "),
finish_reason=None,
)
],
created=1234567890,
model="gpt-5.5",
object="chat.completion.chunk",
),
ModelResponseStream(
id="test-id",
choices=[
litellm.utils.StreamingChoices(
index=0,
delta=litellm.utils.Delta(
content="[email protected] and SSN is "
),
finish_reason=None,
)
],
created=1234567890,
model="gpt-5.5",
object="chat.completion.chunk",
),
ModelResponseStream(
id="test-id",
choices=[
litellm.utils.StreamingChoices(
index=0,
delta=litellm.utils.Delta(content="123-45-6789"),
finish_reason="stop",
)
],
created=1234567890,
model="gpt-5.5",
object="chat.completion.chunk",
),
]
for chunk in chunks:
yield chunk
request_data = {
"model": "gpt-5.5",
"messages": [
{"role": "user", "content": "What's your email and SSN?"},
],
"stream": True,
}
with patch.object(
guardrail.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_post.return_value = mock_bedrock_response
masked_stream = guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=mock_user_api_key_dict,
response=mock_streaming_response(),
request_data=request_data,
)
masked_chunks = []
async for chunk in masked_stream:
masked_chunks.append(chunk)
assert len(masked_chunks) > 0
full_content = ""
for chunk in masked_chunks:
if hasattr(chunk, "choices") and chunk.choices:
if hasattr(chunk.choices[0], "delta") and chunk.choices[0].delta:
if (
hasattr(chunk.choices[0].delta, "content")
and chunk.choices[0].delta.content
):
full_content += chunk.choices[0].delta.content
assert "Sure! My email is {EMAIL} and SSN is {US_SSN}" == full_content
print("✓ Streaming response PII masking test passed")
@pytest.mark.asyncio
async def test_convert_to_bedrock_format_input_source():
"""Test convert_to_bedrock_format with INPUT source and mock messages"""
from litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
BedrockGuardrail,
)
from litellm.types.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
BedrockRequest,
)
from unittest.mock import patch
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT"
)
mock_messages = [
{"role": "user", "content": "Hello, how are you?"},
{"role": "assistant", "content": "I'm doing well, thank you!"},
{
"role": "user",
"content": [
{"type": "text", "text": "What's the weather like?"},
{"type": "text", "text": "Is it sunny today?"},
],
},
]
result = guardrail.convert_to_bedrock_format(source="INPUT", messages=mock_messages)
assert isinstance(result, dict)
assert result.get("source") == "INPUT"
assert "content" in result
assert isinstance(result.get("content"), list)
expected_content_items = [
{"text": {"text": "Hello, how are you?"}},
{"text": {"text": "I'm doing well, thank you!"}},
{"text": {"text": "What's the weather like?"}},
{"text": {"text": "Is it sunny today?"}},
]
assert result.get("content") == expected_content_items
print("✅ INPUT source test passed - result:", result)
@pytest.mark.asyncio
async def test_convert_to_bedrock_format_output_source():
"""Test convert_to_bedrock_format with OUTPUT source and mock ModelResponse"""
from litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
BedrockGuardrail,
)
from litellm.types.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
BedrockRequest,
)
import litellm
from unittest.mock import patch
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT"
)
mock_response = litellm.ModelResponse(
id="test-response-id",
choices=[
litellm.Choices(
index=0,
message=litellm.Message(
role="assistant", content="This is a test response from the model."
),
finish_reason="stop",
),
litellm.Choices(
index=1,
message=litellm.Message(
role="assistant", content="This is a second choice response."
),
finish_reason="stop",
),
],
created=1234567890,
model="gpt-5.5",
object="chat.completion",
)
result = guardrail.convert_to_bedrock_format(
source="OUTPUT", response=mock_response
)
assert isinstance(result, dict)
assert result.get("source") == "OUTPUT"
assert "content" in result
assert isinstance(result.get("content"), list)
expected_content_items = [
{"text": {"text": "This is a test response from the model."}},
{"text": {"text": "This is a second choice response."}},
]
assert result.get("content") == expected_content_items
print("✅ OUTPUT source test passed - result:", result)
@pytest.mark.asyncio
async def test_convert_to_bedrock_format_post_call_streaming_hook():
"""Test async_post_call_streaming_iterator_hook makes OUTPUT bedrock request and applies masking"""
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
from litellm.types.utils import ModelResponseStream
import litellm
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT"
)
async def mock_streaming_response():
chunks = [
ModelResponseStream(
id="test-id",
choices=[
litellm.utils.StreamingChoices(
index=0,
delta=litellm.utils.Delta(content="My email is "),
finish_reason=None,
)
],
created=1234567890,
model="gpt-5.5",
object="chat.completion.chunk",
),
ModelResponseStream(
id="test-id",
choices=[
litellm.utils.StreamingChoices(
index=0,
delta=litellm.utils.Delta(content="[email protected]"),
finish_reason="stop",
)
],
created=1234567890,
model="gpt-5.5",
object="chat.completion.chunk",
),
]
for chunk in chunks:
yield chunk
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "My email is {EMAIL}"}],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{
"type": "EMAIL",
"match": "[email protected]",
"action": "ANONYMIZED",
}
]
}
}
],
}
request_data = {
"model": "gpt-5.5",
"messages": [{"role": "user", "content": "What's your email?"}],
"stream": True,
}
bedrock_calls = []
async def mock_make_bedrock_api_request(
source,
messages=None,
response=None,
request_data=None,
logging_event_type=None,
**kwargs,
):
bedrock_calls.append(
{
"source": source,
"messages": messages,
"response": response,
"request_data": request_data,
"logging_event_type": logging_event_type,
}
)
from litellm.types.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
BedrockGuardrailResponse,
)
return BedrockGuardrailResponse(**mock_bedrock_response.json())
with patch.object(
guardrail, "make_bedrock_api_request", side_effect=mock_make_bedrock_api_request
):
result_generator = guardrail.async_post_call_streaming_iterator_hook(
user_api_key_dict=mock_user_api_key_dict,
response=mock_streaming_response(),
request_data=request_data,
)
result_chunks = []
async for chunk in result_generator:
result_chunks.append(chunk)
assert (
len(bedrock_calls) == 1
), f"Expected 1 bedrock call (OUTPUT only), got {len(bedrock_calls)}"
output_call = bedrock_calls[0]
assert output_call["source"] == "OUTPUT"
assert output_call["response"] is not None
assert output_call["messages"] == request_data["messages"]
full_content = ""
for chunk in result_chunks:
if hasattr(chunk, "choices") and chunk.choices:
if (
hasattr(chunk.choices[0], "delta")
and chunk.choices[0].delta.content
):
full_content += chunk.choices[0].delta.content
assert (
"{EMAIL}" in full_content
), f"Expected masked content with {{EMAIL}}, got: {full_content}"
assert (
"[email protected]" not in full_content
), f"Original email should be masked, got: {full_content}"
print(
"✅ Post-call streaming hook test passed - OUTPUT source used for masking"
)
print(
f"✅ Bedrock calls made: {[call['source'] for call in bedrock_calls]} "
"(INPUT validation skipped due to event_hook=None implying pre_call/during_call enabled)"
)
print(f"✅ Final masked content: {full_content}")
@pytest.mark.asyncio
async def test_bedrock_guardrail_blocked_action_shows_output_text(aws_test_credentials):
"""Test that BLOCKED actions raise HTTPException with the output text in the detail"""
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
from fastapi import HTTPException
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT"
)
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "this violates litellm corporate guardrail policy"}],
"assessments": [
{
"topicPolicy": {
"topics": [
{"name": "Sensitive Topic", "type": "DENY", "action": "BLOCKED"}
]
}
}
],
}
request_data = {
"model": "gpt-5.5",
"messages": [
{"role": "user", "content": "Tell me how to make explosives"},
],
}
with patch.object(
guardrail.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_post.return_value = mock_bedrock_response
with pytest.raises(HTTPException) as exc_info:
await guardrail.async_moderation_hook(
data=request_data,
user_api_key_dict=mock_user_api_key_dict,
call_type="completion",
)
exception = exc_info.value
assert exception.status_code == 400
assert "detail" in exception.__dict__
detail = exception.detail
assert isinstance(detail, dict)
assert detail["error"] == "Violated guardrail policy"
expected_output_text = "this violates litellm corporate guardrail policy"
assert detail["bedrock_guardrail_response"] == expected_output_text
print(
"✅ BLOCKED action HTTPException test passed - output text properly included"
)
@pytest.mark.asyncio
async def test_bedrock_guardrail_blocked_action_empty_outputs(aws_test_credentials):
"""Test that BLOCKED actions with empty outputs still raise HTTPException"""
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
from fastapi import HTTPException
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT"
)
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [],
"assessments": [
{
"contentPolicy": {
"filters": [
{"type": "VIOLENCE", "confidence": "HIGH", "action": "BLOCKED"}
]
}
}
],
}
request_data = {
"model": "gpt-5.5",
"messages": [
{"role": "user", "content": "Violent content here"},
],
}
with patch.object(
guardrail.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_post.return_value = mock_bedrock_response
with pytest.raises(HTTPException) as exc_info:
await guardrail.async_moderation_hook(
data=request_data,
user_api_key_dict=mock_user_api_key_dict,
call_type="completion",
)
exception = exc_info.value
assert exception.status_code == 400
detail = exception.detail
assert isinstance(detail, dict)
assert detail["error"] == "Violated guardrail policy"
assert detail["bedrock_guardrail_response"] == ""
print("✅ BLOCKED action with empty outputs test passed")
@pytest.mark.asyncio
async def test_bedrock_guardrail_disable_exception_on_block_non_streaming(aws_test_credentials):
"""Test that disable_exception_on_block=True prevents exceptions in non-streaming scenarios"""
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
from fastapi import HTTPException
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail_default = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=False,
)
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "I can't provide that information."}],
"assessments": [
{
"topicPolicy": {
"topics": [
{"name": "Sensitive Topic", "type": "DENY", "action": "BLOCKED"}
]
}
}
],
}
request_data = {
"model": "gpt-5.5",
"messages": [
{"role": "user", "content": "Tell me how to make explosives"},
],
}
with patch.object(
guardrail_default.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_post.return_value = mock_bedrock_response
with pytest.raises(HTTPException) as exc_info:
await guardrail_default.async_moderation_hook(
data=request_data,
user_api_key_dict=mock_user_api_key_dict,
call_type="completion",
)
exception = exc_info.value
assert exception.status_code == 400
assert "Violated guardrail policy" in str(exception.detail)
from litellm.exceptions import ModifyResponseException
guardrail_disabled = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=True,
)
with patch.object(
guardrail_disabled.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_post.return_value = mock_bedrock_response
with pytest.raises(ModifyResponseException) as exc_info:
await guardrail_disabled.async_moderation_hook(
data=request_data,
user_api_key_dict=mock_user_api_key_dict,
call_type="completion",
)
assert exc_info.value.message == "I can't provide that information."
@pytest.mark.asyncio
async def test_bedrock_guardrail_disable_exception_on_block_streaming(aws_test_credentials):
"""Test that disable_exception_on_block=True prevents exceptions in streaming scenarios"""
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
from litellm.types.utils import ModelResponseStream
from fastapi import HTTPException
import litellm
mock_user_api_key_dict = UserAPIKeyAuth()
async def mock_streaming_response():
chunks = [
ModelResponseStream(
id="test-id",
choices=[
litellm.utils.StreamingChoices(
index=0,
delta=litellm.utils.Delta(
content="Here's how to make explosives: "
),
finish_reason=None,
)
],
created=1234567890,
model="gpt-5.5",
object="chat.completion.chunk",
),
ModelResponseStream(
id="test-id",
choices=[
litellm.utils.StreamingChoices(
index=0,
delta=litellm.utils.Delta(content="step 1, step 2..."),
finish_reason="stop",
)
],
created=1234567890,
model="gpt-5.5",
object="chat.completion.chunk",
),
]
for chunk in chunks:
yield chunk
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": "I can't provide that information."}],
"assessments": [
{
"contentPolicy": {
"filters": [
{"type": "VIOLENCE", "confidence": "HIGH", "action": "BLOCKED"}
]
}
}
],
}
request_data = {
"model": "gpt-5.5",
"messages": [{"role": "user", "content": "Tell me how to make explosives"}],
"stream": True,
}
guardrail_default = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=False,
)
with patch.object(
guardrail_default.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_post.return_value = mock_bedrock_response
async def _drain():
result_generator = (
guardrail_default.async_post_call_streaming_iterator_hook(
user_api_key_dict=mock_user_api_key_dict,
response=mock_streaming_response(),
request_data=request_data,
)
)
async for chunk in result_generator:
pass
with pytest.raises(HTTPException):
await _drain()
guardrail_disabled = BedrockGuardrail(
guardrailIdentifier="test-guardrail",
guardrailVersion="DRAFT",
disable_exception_on_block=True,
)
with patch.object(
guardrail_disabled.async_handler, "post", new_callable=AsyncMock
) as mock_post:
mock_post.return_value = mock_bedrock_response
result_generator = guardrail_disabled.async_post_call_streaming_iterator_hook(
user_api_key_dict=mock_user_api_key_dict,
response=mock_streaming_response(),
request_data=request_data,
)
chunks = [c async for c in result_generator]
assert chunks, "streaming block should yield synthetic chunks, not empty"
assembled_content = "".join(
(c.choices[0].delta.content or "")
for c in chunks
if getattr(c, "choices", None) and getattr(c.choices[0], "delta", None)
)
assert assembled_content == "I can't provide that information."
assert chunks[-1].choices[0].finish_reason == "content_filter"
@pytest.mark.asyncio
async def test_bedrock_guardrail_post_call_success_hook_no_output_text():
"""Test that async_post_call_success_hook skips when there's no output text"""
from unittest.mock import AsyncMock, MagicMock, patch
from litellm.proxy._types import UserAPIKeyAuth
from litellm.types.utils import ModelResponseStream
import litellm
mock_user_api_key_dict = UserAPIKeyAuth()
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT"
)
mock_response = litellm.ModelResponse(
id="test-id",
choices=[
litellm.Choices(
index=0,
message=litellm.Message(
role="assistant",
content=None,
tool_calls=[
litellm.utils.ChatCompletionMessageToolCall(
id="tooluse_kZJMlvQmRJ6eAyJE5GIl7Q",
function=litellm.utils.Function(
name="top_song", arguments='{"sign": "WZPZ"}'
),
type="function",
)
],
),
finish_reason="tool_calls",
)
],
created=1234567890,
model="gpt-5.5",
object="chat.completion",
)
data = {
"model": "gpt-5.5",
"messages": [
{"role": "user", "content": "Hello"},
],
}
mock_user_api_key_dict = UserAPIKeyAuth()
result = await guardrail.async_post_call_success_hook(
data=data,
response=mock_response,
user_api_key_dict=mock_user_api_key_dict,
)
assert result is None
print("✅ No output text in response test passed")
@pytest.mark.asyncio
async def test__redact_pii_matches_null_list_fields():
"""Test that explicit null values from Bedrock API are handled correctly.
The Bedrock API can return explicit JSON null for list fields like
piiEntities, regexes, customWords, managedWordLists. This would cause
TypeError: 'NoneType' object is not iterable if not handled.
"""
response_with_null_pii = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": None,
"regexes": None,
}
}
],
}
redacted = _redact_pii_matches(response_with_null_pii)
assert redacted is not None
assert (
redacted["assessments"][0]["sensitiveInformationPolicy"]["piiEntities"] is None
)
assert redacted["assessments"][0]["sensitiveInformationPolicy"]["regexes"] is None
response_with_null_words = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"wordPolicy": {
"customWords": None,
"managedWordLists": None,
}
}
],
}
redacted = _redact_pii_matches(response_with_null_words)
assert redacted is not None
assert redacted["assessments"][0]["wordPolicy"]["customWords"] is None
assert redacted["assessments"][0]["wordPolicy"]["managedWordLists"] is None
response_with_null_assessments = {
"action": "GUARDRAIL_INTERVENED",
"assessments": None,
}
redacted = _redact_pii_matches(response_with_null_assessments)
assert redacted is not None
@pytest.mark.asyncio
async def test__redact_pii_matches_malformed_response_with_non_list_assessments():
"""Test _redact_pii_matches with malformed response (should not crash)"""
malformed_response = {
"action": "GUARDRAIL_INTERVENED",
"assessments": "not_a_list",
}
redacted_response = _redact_pii_matches(malformed_response)
assert redacted_response == malformed_response
missing_keys_response = {
"action": "GUARDRAIL_INTERVENED",
}
redacted_response = _redact_pii_matches(missing_keys_response)
assert redacted_response == missing_keys_response
@pytest.mark.asyncio
async def test_should_raise_guardrail_blocked_exception_null_fields():
"""Test that _should_raise_guardrail_blocked_exception handles null list fields.
Validates the or [] null-safety pattern works for all policy fields
in _should_raise_guardrail_blocked_exception.
"""
guardrail = BedrockGuardrail(
guardrailIdentifier="test-guardrail", guardrailVersion="DRAFT"
)
response_null_assessments = {
"action": "GUARDRAIL_INTERVENED",
"assessments": None,
}
assert (
guardrail._should_raise_guardrail_blocked_exception(response_null_assessments)
is False
)
response_null_topics = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [{"topicPolicy": {"topics": None}}],
}
assert (
guardrail._should_raise_guardrail_blocked_exception(response_null_topics)
is False
)
response_null_filters = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [{"contentPolicy": {"filters": None}}],
}
assert (
guardrail._should_raise_guardrail_blocked_exception(response_null_filters)
is False
)
response_null_words = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{"wordPolicy": {"customWords": None, "managedWordLists": None}}
],
}
assert (
guardrail._should_raise_guardrail_blocked_exception(response_null_words)
is False
)
response_null_pii = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{"sensitiveInformationPolicy": {"piiEntities": None, "regexes": None}}
],
}
assert (
guardrail._should_raise_guardrail_blocked_exception(response_null_pii) is False
)
response_null_grounding = {
"action": "GUARDRAIL_INTERVENED",
"assessments": [{"contextualGroundingPolicy": {"filters": None}}],
}
assert (
guardrail._should_raise_guardrail_blocked_exception(response_null_grounding)
is False
)
_MASKING_GUARDRAIL_URL: Final = "https://bedrock-runtime.us-east-1.amazonaws.com/guardrail/wf0hkdb5x07f/version/DRAFT/apply"
def _masking_guardrail() -> BedrockGuardrail:
return BedrockGuardrail(
guardrailIdentifier="wf0hkdb5x07f",
guardrailVersion="DRAFT",
aws_access_key_id="fake-access-key",
aws_secret_access_key="fake-secret-key",
aws_region_name="us-east-1",
)
def _anonymized_reply(masked_texts: tuple[str, ...], entity_types: tuple[str, ...]) -> httpx.Response:
return httpx.Response(
200,
json={
"action": "GUARDRAIL_INTERVENED",
"outputs": [{"text": text} for text in masked_texts],
"assessments": [
{
"sensitiveInformationPolicy": {
"piiEntities": [
{"type": entity_type, "match": "redacted", "action": "ANONYMIZED"}
for entity_type in entity_types
]
}
}
],
},
)
def _sent_texts(route: respx.Route) -> tuple[str, ...]:
body: Final = json.loads(route.calls[0].request.content)
assert body["source"] == "INPUT"
return tuple(item["text"]["text"] for item in body["content"])
@pytest.mark.asyncio
async def test_during_call_masking_rewrites_pii_in_messages(
respx_mock: respx.MockRouter, httpx_transport: None
) -> None:
guardrail: Final = _masking_guardrail()
route: Final = respx_mock.post(_MASKING_GUARDRAIL_URL).mock(
return_value=_anonymized_reply(
(
"Hello, my phone number is {PHONE}",
"Hello, how can I help you today?",
"I need to cancel my order",
"ok, my credit card number is {CREDIT_DEBIT_CARD_NUMBER}",
),
("PHONE", "CREDIT_DEBIT_CARD_NUMBER"),
)
)
response: Final = await guardrail.async_moderation_hook(
data={
"model": "gpt-5.5",
"messages": [
{"role": "user", "content": "Hello, my phone number is +1 412 555 1212"},
{"role": "assistant", "content": "Hello, how can I help you today?"},
{"role": "user", "content": "I need to cancel my order"},
{"role": "user", "content": "ok, my credit card number is 1234-5678-9012-3456"},
],
},
user_api_key_dict=UserAPIKeyAuth(),
call_type="completion",
)
assert route.call_count == 1
assert _sent_texts(route) == (
"Hello, my phone number is +1 412 555 1212",
"Hello, how can I help you today?",
"I need to cancel my order",
"ok, my credit card number is 1234-5678-9012-3456",
)
assert response is not None
assert [message["content"] for message in response["messages"]] == [
"Hello, my phone number is {PHONE}",
"Hello, how can I help you today?",
"I need to cancel my order",
"ok, my credit card number is {CREDIT_DEBIT_CARD_NUMBER}",
]
@pytest.mark.asyncio
async def test_during_call_masking_rewrites_only_pii_block_in_content_list(
respx_mock: respx.MockRouter, httpx_transport: None
) -> None:
guardrail: Final = _masking_guardrail()
route: Final = respx_mock.post(_MASKING_GUARDRAIL_URL).mock(
return_value=_anonymized_reply(
(
"Hello, my phone number is {PHONE}",
"what time is it?",
"Hello, how can I help you today?",
"who is the president of the united states?",
),
("PHONE",),
)
)
response: Final = await guardrail.async_moderation_hook(
data={
"model": "gpt-5.5",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Hello, my phone number is +1 412 555 1212"},
{"type": "text", "text": "what time is it?"},
],
},
{"role": "assistant", "content": "Hello, how can I help you today?"},
{"role": "user", "content": "who is the president of the united states?"},
],
},
user_api_key_dict=UserAPIKeyAuth(),
call_type="completion",
)
assert route.call_count == 1
assert _sent_texts(route) == (
"Hello, my phone number is +1 412 555 1212",
"what time is it?",
"Hello, how can I help you today?",
"who is the president of the united states?",
)
assert response is not None
messages: Final = response["messages"]
assert messages[0]["content"] == [
{"type": "text", "text": "Hello, my phone number is {PHONE}"},
{"type": "text", "text": "what time is it?"},
]
assert messages[1]["content"] == "Hello, how can I help you today?"
assert messages[2]["content"] == "who is the president of the united states?"
@pytest.mark.asyncio
@pytest.mark.parametrize(
("guardrail_config", "upstream_answer"),
[
pytest.param(
{"guardrailIdentifier": "wf0hkdb5x07f", "guardrailVersion": "DRAFT"},
{"action": "NONE", "outputs": [], "assessments": []},
id="apply_guardrail",
),
pytest.param(
{"checks": {"contentFilter": {"categories": [{"category": "VIOLENCE"}]}}, "content_filter_threshold": 0.5},
{"results": {"contentFilter": {"results": [{"category": "VIOLENCE", "severityScore": 0.1}]}}},
id="invoke_guardrail_checks",
),
],
)
async def test_guardrail_secret_lookups_leave_the_event_loop_free(
guardrail_config: dict[str, object],
upstream_answer: dict[str, object],
respx_mock: respx.MockRouter,
httpx_transport: None,
aws_test_credentials: None,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False)
monkeypatch.setenv("AWS_REGION_NAME", "us-west-2")
vault: Final = host_in_loop_probing_vault(monkeypatch, "AWS_BEARER_TOKEN_BEDROCK", "AWS_REGION_NAME")
guardrail: Final = BedrockGuardrail(**guardrail_config)
upstream: Final = respx_mock.route().mock(return_value=httpx.Response(200, json=upstream_answer))
_ = await guardrail.make_bedrock_api_request(
source="INPUT",
messages=[{"role": "user", "content": "hello"}],
request_data={"messages": []},
)
assert vault.read.is_set()
assert not vault.read_on_loop.is_set()
assert upstream.calls.last.request.url.host == "bedrock-runtime.us-west-2.amazonaws.com"