Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions python/packages/core/agent_framework/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def detect_media_type_from_base64(
if data is not None:
raise ValueError("Provide exactly one of data_bytes, data_str, or data_uri.")
# Remove data URI prefix if present
if ";base64," not in data_uri:
raise ValueError("Invalid data URI format.")
data_str = data_uri.split(";base64,", 1)[1]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, addressed in 630243a. The helper now distinguishes malformed data URI shape from well-formed non-base64 data URIs: missing data:/comma raises ValueError("Invalid data URI format."), while a data URI without a ;base64 marker raises ValueError("Data URI must use base64 encoding."). The regression test now asserts the per-case messages.

if data_str is not None:
if data is not None:
Expand Down
14 changes: 14 additions & 0 deletions python/packages/core/tests/core/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ def test_data_content_detect_image_format_from_base64():
detect_media_type_from_base64(data_str="data", data_uri="data:application/octet-stream;base64,AAA")


@pytest.mark.parametrize(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed. I switched the new parametrized test to the existing file style with @mark.parametrize and raises(...).

"data_uri",
[
"data:text/plain,hello",
"data:image/png;base64",
"not-a-data-uri",
],
)
def test_detect_media_type_from_base64_rejects_malformed_data_uri(data_uri: str):
"""Test malformed data URI inputs raise the documented ValueError."""
with pytest.raises(ValueError, match="Invalid data URI format."):
detect_media_type_from_base64(data_uri=data_uri)


def test_data_content_create_data_uri_from_base64():
"""Test the create_data_uri_from_base64 class method."""
# Test with PNG data
Expand Down