Skip to content

Commit e61374f

Browse files
committed
Merge branch 'fix-proto-utils-metadata-serialization' of https://github.com/youngchannelforyou/a2a-python into pr/youngchannelforyou/420
2 parents ea0d978 + 8c249a7 commit e61374f

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/a2a/utils/proto_utils.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ def make_dict_serializable(value: Any) -> Any:
5757
Returns:
5858
A serializable value.
5959
"""
60+
if isinstance(value, (str, int, float, bool)) or value is None:
61+
return value
6062
if isinstance(value, dict):
6163
return {k: make_dict_serializable(v) for k, v in value.items()}
6264
if isinstance(value, list | tuple):
6365
return [make_dict_serializable(item) for item in value]
64-
if isinstance(value, str | int | float | bool) or value is None:
65-
return value
6666
return str(value)
6767

6868

@@ -81,19 +81,18 @@ def normalize_large_integers_to_strings(
8181
Returns:
8282
A normalized value.
8383
"""
84-
if isinstance(value, dict):
85-
return {
86-
k: normalize_large_integers_to_strings(v, max_safe_digits)
87-
for k, v in value.items()
88-
}
89-
if isinstance(value, list | tuple):
90-
return [
91-
normalize_large_integers_to_strings(item, max_safe_digits)
92-
for item in value
93-
]
94-
if isinstance(value, int) and abs(value) > (10**max_safe_digits - 1):
95-
return str(value)
96-
return value
84+
max_safe_int = 10**max_safe_digits - 1
85+
86+
def _normalize(item: Any) -> Any:
87+
if isinstance(item, int) and abs(item) > max_safe_int:
88+
return str(item)
89+
if isinstance(item, dict):
90+
return {k: _normalize(v) for k, v in item.items()}
91+
if isinstance(item, list | tuple):
92+
return [_normalize(i) for i in item]
93+
return item
94+
95+
return _normalize(value)
9796

9897

9998
def parse_string_integers_in_dict(value: Any, max_safe_digits: int = 15) -> Any:
@@ -119,12 +118,11 @@ def parse_string_integers_in_dict(value: Any, max_safe_digits: int = 15) -> Any:
119118
parse_string_integers_in_dict(item, max_safe_digits)
120119
for item in value
121120
]
122-
if (
123-
isinstance(value, str)
124-
and value.lstrip('-').isdigit()
125-
and len(value.lstrip('-')) > max_safe_digits
126-
):
127-
return int(value)
121+
if isinstance(value, str):
122+
# Handle potential negative numbers.
123+
stripped_value = value.lstrip('-')
124+
if stripped_value.isdigit() and len(stripped_value) > max_safe_digits:
125+
return int(value)
128126
return value
129127

130128

0 commit comments

Comments
 (0)