diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py index f498d647a5..c88ebe16d9 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py @@ -727,6 +727,15 @@ 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 + ######################################################### ########## 1. Make parallel Bedrock API requests ########## ######################################################### diff --git a/tests/guardrails_tests/test_bedrock_guardrails.py b/tests/guardrails_tests/test_bedrock_guardrails.py index b98a1b16be..c4d1655594 100644 --- a/tests/guardrails_tests/test_bedrock_guardrails.py +++ b/tests/guardrails_tests/test_bedrock_guardrails.py @@ -1366,3 +1366,59 @@ async def test_bedrock_guardrail_disable_exception_on_block_streaming(): except Exception as e: pytest.fail(f"Should not raise exception when disable_exception_on_block=True in streaming, but got: {e}") + +@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 + + # Create proper mock objects + mock_user_api_key_dict = UserAPIKeyAuth() + + # Create guardrail instance + guardrail = BedrockGuardrail( + guardrailIdentifier="test-guardrail", + 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" + } + + data = { + "model": "gpt-4o", + "messages": [ + {"role": "user", "content": "Hello"}, + ], + } + mock_user_api_key_dict = UserAPIKeyAuth() + + return await guardrail.async_post_call_success_hook( + data=data, + response=mock_bedrock_response, + user_api_key_dict=mock_user_api_key_dict, + ) + # If no error is raised, then the test passes + print("✅ No output text in response test passed") \ No newline at end of file