mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(guardrails): refuse non-string image references before normalization
Co-authored-by: feng.tsai <fengtsai@berri.ai> Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
0d2d7b6e99
commit
89de4a0f90
2 changed files with 27 additions and 0 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue