mirror of
https://github.com/tiennm99/litellm.git
synced 2026-07-11 21:46:22 +00:00
Merge pull request #15106 from plafleur/ISSUE-15105
Guardrails - Don't run post_call guardrail if no text returned from Bedrock
This commit is contained in:
@@ -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 ##########
|
||||
#########################################################
|
||||
|
||||
@@ -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")
|
||||
Reference in New Issue
Block a user