@@ -46,6 +46,88 @@ def dict_to_struct(dictionary: dict[str, Any]) -> struct_pb2.Struct:
4646 return struct
4747
4848
49+ def make_dict_serializable (value : Any ) -> Any :
50+ """Dict pre-processing utility: converts non-serializable values to serializable form.
51+
52+ Use this when you want to normalize a dictionary before dict->Struct conversion.
53+
54+ Args:
55+ value: The value to convert.
56+
57+ Returns:
58+ A serializable value.
59+ """
60+ if isinstance (value , dict ):
61+ return {k : make_dict_serializable (v ) for k , v in value .items ()}
62+ if isinstance (value , list | tuple ):
63+ return [make_dict_serializable (item ) for item in value ]
64+ if isinstance (value , str | int | float | bool ) or value is None :
65+ return value
66+ return str (value )
67+
68+
69+ def normalize_large_integers_to_strings (
70+ value : Any , max_safe_digits : int = 15
71+ ) -> Any :
72+ """Integer preprocessing utility: converts large integers to strings.
73+
74+ Use this when you want to convert large integers to strings considering
75+ JavaScript's MAX_SAFE_INTEGER (2^53 - 1) limitation.
76+
77+ Args:
78+ value: The value to convert.
79+ max_safe_digits: Maximum safe integer digits (default: 15).
80+
81+ Returns:
82+ A normalized value.
83+ """
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
97+
98+
99+ def parse_string_integers_in_dict (value : Any , max_safe_digits : int = 15 ) -> Any :
100+ """String post-processing utility: converts large integer strings back to integers.
101+
102+ Use this when you want to restore large integer strings to integers
103+ after Struct->dict conversion.
104+
105+ Args:
106+ value: The value to convert.
107+ max_safe_digits: Maximum safe integer digits (default: 15).
108+
109+ Returns:
110+ A parsed value.
111+ """
112+ if isinstance (value , dict ):
113+ return {
114+ k : parse_string_integers_in_dict (v , max_safe_digits )
115+ for k , v in value .items ()
116+ }
117+ if isinstance (value , list | tuple ):
118+ return [
119+ parse_string_integers_in_dict (item , max_safe_digits )
120+ for item in value
121+ ]
122+ if (
123+ isinstance (value , str )
124+ and value .lstrip ('-' ).isdigit ()
125+ and len (value .lstrip ('-' )) > max_safe_digits
126+ ):
127+ return int (value )
128+ return value
129+
130+
49131class ToProto :
50132 """Converts Python types to proto types."""
51133
@@ -980,107 +1062,3 @@ def role(cls, role: a2a_pb2.Role) -> types.Role:
9801062 return types .Role .agent
9811063 case _:
9821064 return types .Role .agent
983-
984-
985- def dict_to_struct (dictionary : dict [str , Any ]) -> struct_pb2 .Struct :
986- """Converts a Python dict to a Struct proto.
987-
988- Unfortunately, using `json_format.ParseDict` does not work because this
989- wants the dictionary to be an exact match of the Struct proto with fields
990- and keys and values, not the traditional Python dict structure.
991-
992- Args:
993- dictionary: The Python dict to convert.
994-
995- Returns:
996- The Struct proto.
997- """
998- struct = struct_pb2 .Struct ()
999- for key , val in dictionary .items ():
1000- if isinstance (val , dict ):
1001- struct [key ] = dict_to_struct (val )
1002- else :
1003- struct [key ] = val
1004- return struct
1005-
1006-
1007- def make_dict_serializable (value : Any ) -> Any :
1008- """Dict preprocessing utility: converts non-serializable values to serializable form.
1009-
1010- Use this when you want to normalize a dictionary before dict->Struct conversion.
1011-
1012- Args:
1013- value: The value to convert.
1014-
1015- Returns:
1016- A serializable value.
1017- """
1018- if isinstance (value , dict ):
1019- return {k : make_dict_serializable (v ) for k , v in value .items ()}
1020- if isinstance (value , list | tuple ):
1021- return [make_dict_serializable (item ) for item in value ]
1022- if isinstance (value , str | int | float | bool ) or value is None :
1023- return value
1024- return str (value )
1025-
1026-
1027- def normalize_large_integers_to_strings (
1028- value : Any , max_safe_digits : int = 15
1029- ) -> Any :
1030- """Integer preprocessing utility: converts large integers to strings.
1031-
1032- Use this when you want to convert large integers to strings considering
1033- JavaScript's MAX_SAFE_INTEGER (2^53 - 1) limitation.
1034-
1035- Args:
1036- value: The value to convert.
1037- max_safe_digits: Maximum safe integer digits (default: 15).
1038-
1039- Returns:
1040- A normalized value.
1041- """
1042- if isinstance (value , dict ):
1043- return {
1044- k : normalize_large_integers_to_strings (v , max_safe_digits )
1045- for k , v in value .items ()
1046- }
1047- if isinstance (value , list | tuple ):
1048- return [
1049- normalize_large_integers_to_strings (item , max_safe_digits )
1050- for item in value
1051- ]
1052- if isinstance (value , int ) and abs (value ) > (10 ** max_safe_digits - 1 ):
1053- return str (value )
1054- return value
1055-
1056-
1057- def parse_string_integers_in_dict (value : Any , max_safe_digits : int = 15 ) -> Any :
1058- """String post-processing utility: converts large integer strings back to integers.
1059-
1060- Use this when you want to restore large integer strings to integers
1061- after Struct->dict conversion.
1062-
1063- Args:
1064- value: The value to convert.
1065- max_safe_digits: Maximum safe integer digits (default: 15).
1066-
1067- Returns:
1068- A parsed value.
1069- """
1070- if isinstance (value , dict ):
1071- return {
1072- k : parse_string_integers_in_dict (v , max_safe_digits )
1073- for k , v in value .items ()
1074- }
1075- if isinstance (value , list | tuple ):
1076- return [
1077- parse_string_integers_in_dict (item , max_safe_digits )
1078- for item in value
1079- ]
1080- if (
1081- isinstance (value , str )
1082- and value .lstrip ('-' ).isdigit ()
1083- and len (value .lstrip ('-' )) > max_safe_digits
1084- ):
1085- return int (value )
1086- return value
0 commit comments