Skip to content

Commit 33318f5

Browse files
committed
fix: Handle JSON-unserializable objects in metadata conversion
- Add _make_dict_serializable helper method to recursively convert dict values - Convert custom objects to strings using str() for JSON serialization - Fix test_metadata_with_custom_objects test failure - Improve code style with modern Python union syntax (| operator)
1 parent b551530 commit 33318f5

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

.github/actions/spelling/allow.txt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
1+
ACMRTUXB
12
ACard
23
AClient
3-
ACMRTUXB
4-
aconnect
5-
adk
64
AError
75
AFast
8-
agentic
96
AGrpc
10-
aio
11-
aiomysql
12-
amannn
13-
aproject
147
ARequest
158
ARun
169
AServer
1710
AServers
1811
AService
1912
AStarlette
2013
AUser
14+
DSNs
15+
EUR
16+
GBP
17+
GVsb
18+
INR
19+
JPY
20+
JSONRPCt
21+
JWS
22+
Llm
23+
POSTGRES
24+
RUF
25+
SLF
26+
Tful
27+
aconnect
28+
adk
29+
agentic
30+
aio
31+
aiomysql
32+
amannn
33+
aproject
2134
autouse
2235
backticks
2336
cla
@@ -27,32 +40,23 @@ codegen
2740
coro
2841
datamodel
2942
drivername
30-
DSNs
3143
dunders
3244
euo
33-
EUR
3445
excinfo
3546
fernet
3647
fetchrow
3748
fetchval
38-
GBP
3949
genai
4050
getkwargs
4151
gle
42-
GVsb
4352
ietf
4453
initdb
4554
inmemory
46-
INR
4755
isready
48-
JPY
49-
JSONRPCt
50-
JWS
5156
kwarg
5257
langgraph
5358
lifecycles
5459
linting
55-
Llm
5660
lstrips
5761
mikeas
5862
mockurl
@@ -62,7 +66,6 @@ oidc
6266
opensource
6367
otherurl
6468
postgres
65-
POSTGRES
6669
postgresql
6770
protoc
6871
pyi
@@ -72,13 +75,10 @@ pyversions
7275
redef
7376
respx
7477
resub
75-
RUF
76-
SLF
7778
socio
7879
sse
7980
tagwords
8081
taskupdate
8182
testuuid
82-
Tful
8383
typeerror
8484
vulnz

src/a2a/utils/proto_utils.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,52 @@ def metadata(
5656
}
5757
)
5858

59+
@classmethod
60+
def _make_dict_serializable(cls, value: Any) -> Any:
61+
"""재귀적으로 값을 JSON 직렬화 가능한 형태로 변환합니다."""
62+
if isinstance(value, dict):
63+
return {k: cls._make_dict_serializable(v) for k, v in value.items()}
64+
if isinstance(value, (list | tuple)):
65+
return [cls._make_dict_serializable(item) for item in value]
66+
if isinstance(value, (str | int | float | bool)) or value is None:
67+
return value
68+
return str(value)
69+
5970
@classmethod
6071
def _convert_value_to_proto(cls, value: Any) -> struct_pb2.Value:
61-
"""Convert Python value to protobuf Value."""
6272
if value is None:
63-
return struct_pb2.Value(null_value=struct_pb2.NULL_VALUE)
73+
proto_value = struct_pb2.Value()
74+
proto_value.null_value = 0
75+
return proto_value
76+
6477
if isinstance(value, bool):
6578
return struct_pb2.Value(bool_value=value)
79+
6680
if isinstance(value, int):
6781
if abs(value) > (2**53 - 1):
6882
return struct_pb2.Value(string_value=str(value))
6983
return struct_pb2.Value(number_value=float(value))
84+
7085
if isinstance(value, float):
7186
return struct_pb2.Value(number_value=value)
87+
7288
if isinstance(value, str):
7389
return struct_pb2.Value(string_value=value)
90+
7491
if isinstance(value, dict):
75-
return struct_pb2.Value(
76-
struct_value=struct_pb2.Struct(
77-
fields={
78-
k: cls._convert_value_to_proto(v)
79-
for k, v in value.items()
80-
}
81-
)
82-
)
83-
if isinstance(value, list | tuple):
84-
return struct_pb2.Value(
85-
list_value=struct_pb2.ListValue(
86-
values=[cls._convert_value_to_proto(item) for item in value]
87-
)
88-
)
92+
serializable_dict = cls._make_dict_serializable(value)
93+
json_data = json.dumps(serializable_dict, ensure_ascii=False)
94+
struct_value = struct_pb2.Struct()
95+
json_format.Parse(json_data, struct_value)
96+
return struct_pb2.Value(struct_value=struct_value)
97+
98+
if isinstance(value, (list | tuple)):
99+
list_value = struct_pb2.ListValue()
100+
for item in value:
101+
converted_item = cls._convert_value_to_proto(item)
102+
list_value.values.append(converted_item)
103+
return struct_pb2.Value(list_value=list_value)
104+
89105
return struct_pb2.Value(string_value=str(value))
90106

91107
@classmethod

0 commit comments

Comments
 (0)