Skip to content

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]

Don't run post_call guardrail if no text returned from bedrock

Patrick Lafleur3e5d585f7d7a
diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py
index f498d647a5e..c88ebe16d99 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 b98a1b16bef..1b1fea74afd 100644
--- a/tests/guardrails_tests/test_bedrock_guardrails.py
+++ b/tests/guardrails_tests/test_bedrock_guardrails.py
@@ -1366,3 +1366,52 @@ 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}")
+
+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 response with PII masking
+ 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 = {} # request data not used by our condition
+ mock_user_api_key_dict = UserAPIKeyAuth()
+
+ return await guardrail.async_post_call_success_hook(
+ data=data,
+ response=mock_bedrock_response, # dict with "outputs"
+ user_api_key_dict=mock_user_api_key_dict,
+ )
\ No newline at end of file