Commit 6bee147
authored
fix(utils): handle image_url string shorthand in _is_image_type_with_blob_content (#6478)
## What's broken
`_is_image_type_with_blob_content` crashes with `AttributeError: 'str'
object has no attribute 'get'` when `image_url` is a plain string
shorthand (e.g. `{"type": "image_url", "image_url":
"data:image/jpeg;base64,..."}`) instead of a dict. The OpenAI API
supports this form and `transform_openai_content_part` in the same file
documents it. Any call to `redact_blob_message_parts` — which is invoked
from `truncate_and_annotate_messages` across the OpenAI, LiteLLM,
Anthropic, LangChain, pydantic-ai, and openai-agents integrations when
`send_default_pii=True` — would crash on such input.
## Why it happens
`item.get("image_url", {})` returns the raw string when the value is a
string, and the subsequent `.get("url", "")` call fails because strings
have no `.get` method.
## Fix
Check `isinstance(image_url_val, dict)` before calling `.get("url",
"")`, falling back to using the string directly. The same guard is
applied to the redaction path at line 702 where
`item["image_url"]["url"]` would also fail on a string.
## Test
Added `test_redact_blob_message_parts_image_url_string_shorthand` to
`TestRedactBlobMessageParts` which passes a message with the string
shorthand form and asserts it is redacted to `[Blob substitute]` without
raising.
Fixes #6477
Co-authored-by: devteamaegis <devteamaegis@users.noreply.github.com>1 parent 7f724ec commit 6bee147
2 files changed
Lines changed: 27 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
609 | 609 | | |
610 | 610 | | |
611 | 611 | | |
612 | | - | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
613 | 618 | | |
614 | 619 | | |
615 | 620 | | |
| |||
694 | 699 | | |
695 | 700 | | |
696 | 701 | | |
697 | | - | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
698 | 706 | | |
699 | 707 | | |
700 | 708 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
923 | 923 | | |
924 | 924 | | |
925 | 925 | | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
926 | 943 | | |
927 | 944 | | |
928 | 945 | | |
| |||
0 commit comments