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
8 changes: 7 additions & 1 deletion python/packages/core/agent_framework/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,13 @@ def from_dict(cls: type[ContentT], data: Mapping[str, Any]) -> ContentT:
# Special handling for DataContent with data and media_type
if content_type == "data" and "data" in remaining and "media_type" in remaining:
# Use from_data() to properly create the DataContent with URI
return cls.from_data(remaining["data"], remaining["media_type"])
return cls.from_data(
remaining["data"],
remaining["media_type"],
annotations=annotations,
additional_properties=additional_properties,
raw_representation=raw_representation,
)

# Handle nested Content objects (e.g., function_call in function_approval_request)
if (function_call := remaining.get("function_call")) and isinstance(function_call, dict):
Expand Down
24 changes: 24 additions & 0 deletions python/packages/core/tests/core/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2564,6 +2564,30 @@ def test_content_from_dict_roundtrip_via_json() -> None:
assert restored.arguments == {"key": "value"}


def test_content_from_dict_data_preserves_metadata() -> None:
"""Test Content.from_dict forwards annotations, additional_properties, and raw_representation
for raw-data content mappings, matching Content.from_data behavior (see GH issue #8632)."""
raw = object()
data = {
"type": "data",
"data": b"test data",
"media_type": "text/plain",
"annotations": [{"type": "citation", "title": "Source document", "file_id": "file-123"}],
"additional_properties": {"filename": "document.txt"},
"raw_representation": raw,
}

content = Content.from_dict(data)
assert content.annotations == data["annotations"]
assert content.additional_properties == data["additional_properties"]
assert content.raw_representation is raw

message_content = Message("user", [data]).contents[0]
assert message_content.annotations == data["annotations"]
assert message_content.additional_properties == data["additional_properties"]
assert message_content.raw_representation is raw


def test_content_to_dict_exclude_none() -> None:
"""Test Content.to_dict excludes None fields by default."""
content = Content.from_text("Hello")
Expand Down
Loading