test bedrock guardrails

This commit is contained in:
Ishaan Jaffer
2025-10-04 09:18:26 -07:00
parent 5d22229d35
commit 4400a6c189
2 changed files with 47 additions and 33 deletions
@@ -727,14 +727,21 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):
)
return
outputs: List[BedrockGuardrailOutput] = (
response.get("outputs", []) or []
)
if not any(output.get("text") for output in outputs):
verbose_proxy_logger.warning(
"Bedrock AI: not running guardrail. No output text in response"
)
return
# Check if the ModelResponse has text content in its choices
# to avoid sending empty content to Bedrock (e.g., during tool calls)
if isinstance(response, litellm.ModelResponse):
has_text_content = False
for choice in response.choices:
if isinstance(choice, litellm.Choices):
if choice.message.content and isinstance(choice.message.content, str):
has_text_content = True
break
if not has_text_content:
verbose_proxy_logger.warning(
"Bedrock AI: not running guardrail. No output text in response"
)
return
#########################################################
########## 1. Make parallel Bedrock API requests ##########
@@ -1384,28 +1384,34 @@ async def test_bedrock_guardrail_post_call_success_hook_no_output_text():
guardrailVersion="DRAFT"
)
# Mock Bedrock API with no output text
mock_bedrock_response = MagicMock()
mock_bedrock_response.status_code = 200
mock_bedrock_response.json.return_value = {
"output": {
"message": {
"role": "assistant",
"content": [
{
"toolUse": {
"toolUseId": "tooluse_kZJMlvQmRJ6eAyJE5GIl7Q",
"name": "top_song",
"input": {
"sign": "WZPZ"
}
}
}
]
}
},
"stopReason": "tool_use"
}
# Create a ModelResponse with tool calls (no text content)
# This simulates a response where the LLM is making a tool call
mock_response = litellm.ModelResponse(
id="test-id",
choices=[
litellm.Choices(
index=0,
message=litellm.Message(
role="assistant",
content=None, # No text content
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-4o",
object="chat.completion"
)
data = {
"model": "gpt-4o",
@@ -1415,10 +1421,11 @@ async def test_bedrock_guardrail_post_call_success_hook_no_output_text():
}
mock_user_api_key_dict = UserAPIKeyAuth()
return await guardrail.async_post_call_success_hook(
result = await guardrail.async_post_call_success_hook(
data=data,
response=mock_bedrock_response,
response=mock_response,
user_api_key_dict=mock_user_api_key_dict,
)
# If no error is raised, then the test passes
# If no error is raised and result is None, then the test passes
assert result is None
print("✅ No output text in response test passed")