Skip to content
Merged
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
17 changes: 7 additions & 10 deletions python/packages/core/agent_framework/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ def from_data(
content = Content.from_data(data=image_bytes, media_type="image/png")

# If you have a base64 string and need to detect media type
base64_string = "iVBORw0KGgo..."
base64_string = "iVBORw0KGgo="
media_type = detect_media_type_from_base64(data_str=base64_string)
if media_type is None:
raise ValueError("Unknown media type")
Expand Down Expand Up @@ -1023,7 +1023,7 @@ def from_uri(
"""Create content from a URI, can be both data URI or external URI.

Use this when you already have a properly formed data URI
(e.g., "data:image/png;base64,iVBORw0KGgo...").
(e.g., "data:image/png;base64,iVBORw0KGgo=").
Or when you receive a link to a online resource (e.g., "https://example.com/image.png").

Args:
Expand All @@ -1049,23 +1049,20 @@ def from_uri(
Examples:
.. code-block:: python

from agent_framework import Content
from agent_framework import Content, detect_media_type_from_base64

# Create from a data URI
content = Content.from_uri(uri="data:image/png;base64,iVBORw0KGgo...", media_type="image/png")
content = Content.from_uri(uri="data:image/png;base64,iVBORw0KGgo=", media_type="image/png")
assert content.type == "data"

# Create from an external URI
content = Content.from_uri(uri="https://example.com/image.png", media_type="image/png")
assert content.type == "uri"

# When receiving a raw already encode data string, you can do this:
raw_base64_string = "iVBORw0KGgo..."
content = Content.from_uri(
uri=f"data:{(detect_media_type_from_base64(data_str=raw_base64_string) or 'image/png')};base64,{
raw_base64_string
}"
)
raw_base64_string = "iVBORw0KGgo="
media_type = detect_media_type_from_base64(data_str=raw_base64_string) or "image/png"
content = Content.from_uri(uri=f"data:{media_type};base64,{raw_base64_string}")
"""
return cls(
**_validate_uri(uri, media_type),
Expand Down
Loading