From a09231ed06172acfce4a6456f3a86555e89bfeb2 Mon Sep 17 00:00:00 2001 From: yucheng Date: Wed, 23 Sep 2026 19:30:35 +0000 Subject: [PATCH] refactor(guardrails): extract the non-string image ref refusal to keep complexity under the ceiling Co-authored-by: feng.tsai Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../guardrails/guardrail_hooks/bedrock_guardrails.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py index 08dcaa36e7a..afcc2e07668 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py @@ -658,6 +658,11 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): # Unrecognized inputs reach the decoder so the guardrail fails closed. return value + def _refuse_non_string_image_refs(self, image_urls: tuple[object, ...]) -> None: + 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") + def _refuse_unscannable_image_ref(self, image_url: str) -> None: """Refuse an image ref that is cheaply known to be unscannable. @@ -3604,9 +3609,7 @@ 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") + self._refuse_non_string_image_refs(image_urls=image_urls) # 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: