diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py index 02c245f16b9..08dcaa36e7a 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py @@ -3604,6 +3604,9 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): # from `image`/`source` blocks. Five other guardrails already consume this # field; Bedrock was the one that dropped it on the floor. image_urls: Final = tuple(inputs.get("images") or ()) if input_type == "request" else () + for image_ref in image_urls: + if not isinstance(image_ref, str): + self._handle_unscannable_attachment(reason="a non-string image reference cannot be scanned") # Refused before the shortcuts: ApplyGuardrail has no content type for a document/file attachment. files: Final = tuple(inputs.get("files") or ()) if input_type == "request" else () if files: diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py index 6f583e4efda..d645193712d 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py @@ -7254,6 +7254,8 @@ async def test_during_call_hook_refuses_serialized_tool_output_attachment_unknow call_type=CallTypes.acompletion.value, ) + mock_post.assert_called_once() + @pytest.mark.asyncio @pytest.mark.parametrize( @@ -7333,3 +7335,25 @@ async def test_during_call_hook_drops_bare_attachment_shells(): user_api_key_dict=UserAPIKeyAuth(), call_type=CallTypes.acompletion.value, ) + + mock_post.assert_called_once() + + +@pytest.mark.asyncio +async def test_apply_guardrail_refuses_non_string_image_reference(): + """A non-string entry in inputs["images"] must refuse cleanly, not crash on startswith.""" + guardrail = BedrockGuardrail( + guardrail_name="bedrock-int-image-ref", + guardrailIdentifier="test-guardrail", + guardrailVersion="DRAFT", + event_hook=GuardrailEventHooks.pre_call, + default_on=True, + ) + + with pytest.raises(HTTPException) as exc_info: + await guardrail.apply_guardrail( + inputs={"texts": ["look"], "images": [5]}, request_data={}, input_type="request" + ) + + assert exc_info.value.status_code == 400 + assert "cannot be scanned" in str(exc_info.value.detail)