Skip to content

Commit 93e347b

Browse files
Merge pull request #15106 from plafleur/ISSUE-15105
Guardrails - Don't run post_call guardrail if no text returned from Bedrock
2 parents d538cf4 + 8e5efd2 commit 93e347b

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,15 @@ async def async_post_call_success_hook(
727727
)
728728
return
729729

730+
outputs: List[BedrockGuardrailOutput] = (
731+
response.get("outputs", []) or []
732+
)
733+
if not any(output.get("text") for output in outputs):
734+
verbose_proxy_logger.warning(
735+
"Bedrock AI: not running guardrail. No output text in response"
736+
)
737+
return
738+
730739
#########################################################
731740
########## 1. Make parallel Bedrock API requests ##########
732741
#########################################################

tests/guardrails_tests/test_bedrock_guardrails.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,3 +1366,59 @@ async def mock_streaming_response():
13661366

13671367
except Exception as e:
13681368
pytest.fail(f"Should not raise exception when disable_exception_on_block=True in streaming, but got: {e}")
1369+
1370+
@pytest.mark.asyncio
1371+
async def test_bedrock_guardrail_post_call_success_hook_no_output_text():
1372+
"""Test that async_post_call_success_hook skips when there's no output text"""
1373+
from unittest.mock import AsyncMock, MagicMock, patch
1374+
from litellm.proxy._types import UserAPIKeyAuth
1375+
from litellm.types.utils import ModelResponseStream
1376+
import litellm
1377+
1378+
# Create proper mock objects
1379+
mock_user_api_key_dict = UserAPIKeyAuth()
1380+
1381+
# Create guardrail instance
1382+
guardrail = BedrockGuardrail(
1383+
guardrailIdentifier="test-guardrail",
1384+
guardrailVersion="DRAFT"
1385+
)
1386+
1387+
# Mock Bedrock API with no output text
1388+
mock_bedrock_response = MagicMock()
1389+
mock_bedrock_response.status_code = 200
1390+
mock_bedrock_response.json.return_value = {
1391+
"output": {
1392+
"message": {
1393+
"role": "assistant",
1394+
"content": [
1395+
{
1396+
"toolUse": {
1397+
"toolUseId": "tooluse_kZJMlvQmRJ6eAyJE5GIl7Q",
1398+
"name": "top_song",
1399+
"input": {
1400+
"sign": "WZPZ"
1401+
}
1402+
}
1403+
}
1404+
]
1405+
}
1406+
},
1407+
"stopReason": "tool_use"
1408+
}
1409+
1410+
data = {
1411+
"model": "gpt-4o",
1412+
"messages": [
1413+
{"role": "user", "content": "Hello"},
1414+
],
1415+
}
1416+
mock_user_api_key_dict = UserAPIKeyAuth()
1417+
1418+
return await guardrail.async_post_call_success_hook(
1419+
data=data,
1420+
response=mock_bedrock_response,
1421+
user_api_key_dict=mock_user_api_key_dict,
1422+
)
1423+
# If no error is raised, then the test passes
1424+
print("✅ No output text in response test passed")

0 commit comments

Comments
 (0)