Skip to content

Commit 228754d

Browse files
authored
Python: Fix AG-UI url source dropping attachments when the URL is in source.value (microsoft#7655)
The ag-ui-protocol `InputContentUrlSource` carries the URL in `source.value`, but `_extract_multimodal_source_fields` only read `source.url`/`source.uri` for url-typed sources, so attachments sent in the spec shape were dropped during the AG-UI to MAF conversion. The base64 branch already read `source.value` correctly. Read `source.value` first, keeping `url`/`uri` as fallbacks for the non-spec shape. Adds tests for both. Fixes microsoft#7653
1 parent 8461667 commit 228754d

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,10 @@ def _extract_multimodal_source_fields(
360360
) -> tuple[str | None, str | None, str | None, str | None]:
361361
"""Extract ``(url, data, binary_id, mime_type)`` from an AG-UI multimodal part.
362362
363-
Handles both the current AG-UI spec (``source.value`` for base64 payloads) and the
364-
legacy ``source.data`` field for backward compatibility. Returned values are the
365-
raw extracted strings (or ``None`` when absent); callers apply their own defaults.
363+
Handles both the current AG-UI spec (``source.value`` for both URL and base64
364+
payloads) and the legacy ``source.url``/``source.data`` fields for backward
365+
compatibility. Returned values are the raw extracted strings (or ``None`` when
366+
absent); callers apply their own defaults.
366367
"""
367368
mime_type = cast(str | None, part.get("mimeType") or part.get("mime_type"))
368369
url = cast(str | None, part.get("url") or part.get("uri"))
@@ -378,7 +379,7 @@ def _extract_multimodal_source_fields(
378379
mime_type = source_mime
379380

380381
if source_type in {"url", "uri"}:
381-
url = cast(str | None, source_dict.get("url") or source_dict.get("uri"))
382+
url = cast(str | None, source_dict.get("value") or source_dict.get("url") or source_dict.get("uri"))
382383
elif source_type in {"base64", "data", "binary"}:
383384
data = cast(str | None, source_dict.get("value") or source_dict.get("data"))
384385
elif source_type in {"id", "file"}:

‎python/packages/ag-ui/tests/ag_ui/test_message_adapters.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,3 +2045,39 @@ def test_parse_multimodal_media_part_unknown_source_value_fallback():
20452045
)
20462046
assert result is not None
20472047
assert "aGVsbG8=" in result.uri # type: ignore[operator] # pyrefly: ignore[not-iterable] # ty: ignore[unsupported-operator]
2048+
2049+
2050+
def test_parse_multimodal_media_part_url_value_field():
2051+
"""Source with type='url' reads the URL from the 'value' field per AG-UI spec."""
2052+
from agent_framework_ag_ui._message_adapters import _parse_multimodal_media_part
2053+
2054+
result = _parse_multimodal_media_part(
2055+
{
2056+
"type": "document",
2057+
"source": {
2058+
"type": "url",
2059+
"value": "https://example.com/files/document.pdf",
2060+
"mime_type": "application/pdf",
2061+
},
2062+
}
2063+
)
2064+
assert result is not None
2065+
assert result.uri == "https://example.com/files/document.pdf"
2066+
assert result.media_type == "application/pdf"
2067+
2068+
2069+
def test_parse_multimodal_media_part_url_field_backward_compat():
2070+
"""Source with type='url' still supports the non-spec 'url' and 'uri' fields."""
2071+
from agent_framework_ag_ui._message_adapters import _parse_multimodal_media_part
2072+
2073+
from_url = _parse_multimodal_media_part(
2074+
{"type": "image", "source": {"type": "url", "url": "https://example.com/a.png"}}
2075+
)
2076+
assert from_url is not None
2077+
assert from_url.uri == "https://example.com/a.png"
2078+
2079+
from_uri = _parse_multimodal_media_part(
2080+
{"type": "image", "source": {"type": "uri", "uri": "https://example.com/b.png"}}
2081+
)
2082+
assert from_uri is not None
2083+
assert from_uri.uri == "https://example.com/b.png"

0 commit comments

Comments
 (0)