Skip to content
Merged
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
24 changes: 20 additions & 4 deletions src/a2a/utils/proto_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# mypy: disable-error-code="arg-type"
"""Utils for converting between proto and Python types."""

Expand Down Expand Up @@ -80,8 +80,16 @@
cls, file: types.FileWithUri | types.FileWithBytes
) -> a2a_pb2.FilePart:
if isinstance(file, types.FileWithUri):
return a2a_pb2.FilePart(file_with_uri=file.uri)
return a2a_pb2.FilePart(file_with_bytes=file.bytes.encode('utf-8'))
return a2a_pb2.FilePart(
file_with_uri=file.uri,
mime_type=file.mime_type,
name=file.name
)
return a2a_pb2.FilePart(
file_with_bytes=file.bytes.encode('utf-8'),
mime_type=file.mime_type,
name=file.name,
)

@classmethod
def task(cls, task: types.Task) -> a2a_pb2.Task:
Expand Down Expand Up @@ -501,8 +509,16 @@
cls, file: a2a_pb2.FilePart
) -> types.FileWithUri | types.FileWithBytes:
if file.HasField('file_with_uri'):
return types.FileWithUri(uri=file.file_with_uri)
return types.FileWithBytes(bytes=file.file_with_bytes.decode('utf-8'))
return types.FileWithUri(
uri=file.file_with_uri,
mime_type=file.mime_type,
name=file.name,
)
return types.FileWithBytes(
bytes=file.file_with_bytes.decode('utf-8'),
mime_type=file.mime_type,
name=file.name,
)

@classmethod
def task_or_message(
Expand Down
8 changes: 7 additions & 1 deletion tests/utils/test_proto_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from unittest import mock

import pytest
Expand All @@ -22,7 +22,11 @@
types.Part(root=types.TextPart(text='Hello')),
types.Part(
root=types.FilePart(
file=types.FileWithUri(uri='file:///test.txt')
file=types.FileWithUri(
uri='file:///test.txt',
name='test.txt',
mime_type="text/plain",
),
)
),
types.Part(root=types.DataPart(data={'key': 'value'})),
Expand Down Expand Up @@ -148,6 +152,8 @@

# Test file part handling
assert proto_msg.content[1].file.file_with_uri == 'file:///test.txt'
assert proto_msg.content[1].file.mime_type == 'text/plain'
assert proto_msg.content[1].file.name == 'test.txt'

roundtrip_msg = proto_utils.FromProto.message(proto_msg)
assert roundtrip_msg == sample_message
Expand Down
Loading