Skip to content

Commit c857582

Browse files
Share one data URI parser across validation, detection and payload extraction
1 parent 521a7fe commit c857582

2 files changed

Lines changed: 70 additions & 24 deletions

File tree

‎python/packages/core/agent_framework/_types.py‎

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from copy import deepcopy
2525
from datetime import datetime
2626
from inspect import isawaitable
27-
from typing import TYPE_CHECKING, Any, ClassVar, Final, Generic, Literal, NewType, cast, overload
27+
from typing import TYPE_CHECKING, Any, ClassVar, Final, Generic, Literal, NamedTuple, NewType, cast, overload
2828

2929
from typing_extensions import Required, TypedDict
3030

@@ -80,6 +80,44 @@ def _parse_content_list(contents_data: Sequence[Any]) -> list[Content]:
8080
# region Internal Helper functions for unified Content
8181

8282

83+
class _ParsedDataUri(NamedTuple):
84+
"""The parts of an RFC 2397 data URI."""
85+
86+
media_type: str
87+
is_base64: bool
88+
payload: str
89+
90+
91+
def _parse_data_uri(uri: str) -> _ParsedDataUri:
92+
"""Parse a ``data:`` URI once, so every caller reads it the same way.
93+
94+
RFC 2397: ``data:[<media-type>][;parameter[=value]]...[;base64],<data>``. The media type is
95+
everything between ``data:`` and the first ``;`` and defaults to ``text/plain`` when empty.
96+
``base64`` is the only encoding marker and is only valid once, as the last parameter; other
97+
parameters such as ``charset=utf-8`` are metadata. Only the metadata before the first comma
98+
is inspected, so a ``;base64,`` sequence inside the payload is never treated as a marker.
99+
100+
Raises:
101+
ContentError: If the URI has no comma or carries an unsupported encoding marker.
102+
"""
103+
if "," not in uri:
104+
raise ContentError("Data URI must contain a comma separating metadata and data")
105+
prefix, payload = uri.split(",", 1)
106+
parts = prefix.split(";")
107+
parameters = parts[1:]
108+
for index, parameter in enumerate(parameters):
109+
if parameter == "base64":
110+
if index != len(parameters) - 1:
111+
raise ContentError("Data URI 'base64' marker must be the last parameter")
112+
elif parameter and "=" not in parameter:
113+
raise ContentError(f"Unsupported data URI encoding: {parameter}")
114+
return _ParsedDataUri(
115+
media_type=parts[0][5:] or "text/plain", # Remove 'data:'
116+
is_base64=bool(parameters) and parameters[-1] == "base64",
117+
payload=payload,
118+
)
119+
120+
83121
def detect_media_type_from_base64(
84122
*,
85123
data_bytes: bytes | None = None,
@@ -132,9 +170,13 @@ def detect_media_type_from_base64(
132170
# Remove data URI prefix if present
133171
if not data_uri.startswith("data:") or "," not in data_uri:
134172
raise ValueError("Invalid data URI format.")
135-
prefix, data_str = data_uri.split(",", 1)
136-
if not prefix.endswith(";base64"):
173+
try:
174+
parsed = _parse_data_uri(data_uri)
175+
except ContentError as exc:
176+
raise ValueError(str(exc)) from exc
177+
if not parsed.is_base64:
137178
raise ValueError("Data URI must use base64 encoding.")
179+
data_str = parsed.payload
138180
if data_str is not None:
139181
if data is not None:
140182
raise ValueError("Provide exactly one of data_bytes, data_str, or data_uri.")
@@ -212,11 +254,10 @@ def _get_data_bytes_as_str(content: Content) -> str | None:
212254
if not uri.startswith("data:"):
213255
return None
214256

215-
if ";base64," not in uri:
257+
parsed = _parse_data_uri(uri)
258+
if not parsed.is_base64:
216259
raise ContentError("Data URI must use base64 encoding")
217-
218-
_, data = uri.split(";base64,", 1)
219-
return data
260+
return parsed.payload
220261

221262

222263
def _get_data_bytes(content: Content) -> bytes | None: # pyright: ignore[reportUnusedFunction]
@@ -259,24 +300,9 @@ def _validate_uri(uri: str, media_type: str | None) -> dict[str, Any]:
259300

260301
# Check for data URI
261302
if uri.startswith("data:"):
262-
if "," not in uri:
263-
raise ContentError("Data URI must contain a comma separating metadata and data")
264-
prefix, _ = uri.split(",", 1)
265-
# RFC 2397: data:[<media-type>][;parameter[=value]]...[;base64],<data>.
266-
# The media type is everything between "data:" and the first ";" and defaults
267-
# to "text/plain" when empty. "base64" is the only encoding marker; other
268-
# parameters such as charset=... are metadata, not encodings.
269-
parts = prefix.split(";")
303+
parsed = _parse_data_uri(uri)
270304
if media_type is None:
271-
media_type = parts[0][5:] or "text/plain" # Remove 'data:'
272-
parameters = parts[1:]
273-
for index, parameter in enumerate(parameters):
274-
if parameter == "base64":
275-
# base64 is only valid as the last parameter, and only once.
276-
if index != len(parameters) - 1:
277-
raise ContentError("Data URI 'base64' marker must be the last parameter")
278-
elif parameter and "=" not in parameter:
279-
raise ContentError(f"Unsupported data URI encoding: {parameter}")
305+
media_type = parsed.media_type
280306
return {"type": "data", "uri": uri, "media_type": media_type}
281307

282308
# Check for common URI schemes

‎python/packages/core/tests/core/test_types.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5939,3 +5939,23 @@ def test_agent_response_update_serialization_includes_finish_reason() -> None:
59395939

59405940

59415941
# endregion
5942+
5943+
5944+
def test_get_data_bytes_as_str_ignores_base64_marker_inside_the_payload():
5945+
"""A ';base64,' sequence inside the payload is data, not the encoding marker."""
5946+
content = Content.from_uri(uri="data:text/plain,a;base64,QUJD")
5947+
with raises(ContentError, match="base64 encoding"):
5948+
_get_data_bytes_as_str(content)
5949+
5950+
5951+
def test_data_uri_readers_agree_on_the_base64_marker():
5952+
"""Validation, media type detection and payload extraction share one reading of a data URI."""
5953+
png_b64 = base64.b64encode(b"\x89PNG\r\n\x1a\n" + b"\x00" * 16).decode()
5954+
uri = f"data:image/png;charset=utf-8;base64,{png_b64}"
5955+
content = Content.from_uri(uri=uri)
5956+
assert content.media_type == "image/png"
5957+
assert _get_data_bytes_as_str(content) == png_b64
5958+
assert detect_media_type_from_base64(data_uri=uri) == "image/png"
5959+
payload_marker = f"data:image/png,x;base64,{png_b64}"
5960+
with pytest.raises(ValueError, match="Data URI must use base64 encoding."):
5961+
detect_media_type_from_base64(data_uri=payload_marker)

0 commit comments

Comments
 (0)