Skip to content

Commit f8e8262

Browse files
Update proto conversion utilities
1 parent e91b31c commit f8e8262

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

src/a2a/utils/proto_utils.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def message(cls, message: a2a_pb2.Message) -> types.Message:
489489
def metadata(cls, metadata: struct_pb2.Struct) -> dict[str, Any]:
490490
if not metadata.fields:
491491
return {}
492-
return struct_to_dict(metadata)
492+
return json_format.MessageToDict(metadata)
493493

494494
@classmethod
495495
def part(cls, part: a2a_pb2.Part) -> types.Part:
@@ -798,7 +798,7 @@ def agent_extension(
798798
return types.AgentExtension(
799799
uri=extension.uri,
800800
description=extension.description,
801-
params=struct_to_dict(extension.params),
801+
params=json_format.MessageToDict(extension.params),
802802
required=extension.required,
803803
)
804804

@@ -941,29 +941,19 @@ def role(cls, role: a2a_pb2.Role) -> types.Role:
941941
return types.Role.agent
942942

943943

944-
def struct_to_dict(struct: struct_pb2.Struct) -> dict[str, Any]:
945-
"""Converts a Struct proto to a Python dict."""
946-
947-
def convert(value: struct_pb2.Value) -> Any:
948-
if value.HasField('list_value'):
949-
return [convert(v) for v in value.list_value.values]
950-
if value.HasField('struct_value'):
951-
return {k: convert(v) for k, v in value.struct_value.fields.items()}
952-
if value.HasField('number_value'):
953-
return value.number_value
954-
if value.HasField('string_value'):
955-
return value.string_value
956-
if value.HasField('bool_value'):
957-
return value.bool_value
958-
if value.HasField('null_value'):
959-
return None
960-
raise ValueError(f'Unsupported type: {value}')
944+
def dict_to_struct(dictionary: dict[str, Any]) -> struct_pb2.Struct:
945+
"""Converts a Python dict to a Struct proto.
961946
962-
return {k: convert(v) for k, v in struct.fields.items()}
947+
Unforunately, using the json_format.ParseDict does not work because this
948+
wants the dictionary to be an exact match of the Struct proto with fields
949+
and keys and values, not the traditional python dict struture.
963950
951+
Args:
952+
dictionary: The Python dict to convert.
964953
965-
def dict_to_struct(dictionary: dict[str, Any]) -> struct_pb2.Struct:
966-
"""Converts a Python dict to a Struct proto."""
954+
Returns:
955+
The Struct proto.
956+
"""
967957
struct = struct_pb2.Struct()
968958
for key, val in dictionary.items():
969959
if isinstance(val, dict):

0 commit comments

Comments
 (0)