Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions python/packages/core/agent_framework/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,17 @@ def _validate_uri(uri: str, media_type: str | None) -> dict[str, Any]:
if "," not in uri:
raise ContentError("Data URI must contain a comma separating metadata and data")
prefix, _ = uri.split(",", 1)
if ";" in prefix:
parts = prefix.split(";")
if len(parts) < 2:
raise ContentError("Invalid data URI format")
# Check encoding
encoding = parts[-1]
if encoding not in ("base64", ""):
raise ContentError(f"Unsupported data URI encoding: {encoding}")
if media_type is None:
# attempt to extract:
media_type = parts[0][5:] # Remove 'data:'
# RFC 2397: data:[<mediatype>][;base64],<data>, where <mediatype> is type/subtype followed by
# optional ;attribute=value parameters. Only a final bare "base64" token names the encoding.
declared_type, *parameters = prefix[len("data:") :].split(";")
if parameters and parameters[-1] in ("base64", ""):
parameters = parameters[:-1]
for parameter in parameters:
if "=" not in parameter:
raise ContentError(f"Unsupported data URI encoding: {parameter}")
Comment on lines +257 to +259
if media_type is None:
# RFC 2397 defaults an omitted media type to text/plain.
media_type = declared_type or "text/plain"
return {"type": "data", "uri": uri, "media_type": media_type}

# Check for common URI schemes
Expand Down
29 changes: 29 additions & 0 deletions python/packages/core/tests/core/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4206,6 +4206,35 @@ def test_validate_uri_data_uri():
assert "uri" in result


@mark.parametrize(
("uri", "expected_media_type"),
[
("data:text/plain,hello", "text/plain"),
("data:text/plain;charset=utf-8,hello%20world", "text/plain"),
("data:application/json;charset=utf-8;base64,e30=", "application/json"),
("data:,hello", "text/plain"),
("data:;base64,aGVsbG8=", "text/plain"),
],
)
def test_validate_uri_data_uri_parameters_and_default_media_type(uri: str, expected_media_type: str):
"""RFC 2397 data URIs may carry parameters, and the media type comes from the prefix."""
assert _validate_uri(uri, None) == {"type": "data", "uri": uri, "media_type": expected_media_type}


@mark.parametrize("uri", ["data:image/png;base32,AAAA", "data:text/plain;base64;charset=utf-8,AAAA"])
def test_validate_uri_rejects_unsupported_data_uri_encoding(uri: str):
"""Only a final bare base64 token names an encoding; any other bare token is rejected."""
with raises(ContentError, match="Unsupported data URI encoding"):
_validate_uri(uri, None)


def test_from_uri_accepts_data_uri_with_charset_parameter():
"""Content.from_uri accepts a non-base64 data URI with a charset parameter."""
content = Content.from_uri("data:text/plain;charset=utf-8,hello%20world")
assert content.type == "data"
assert content.media_type == "text/plain"


# endregion


Expand Down
Loading