Skip to content

Commit 67ba0a4

Browse files
authored
fix(deps): support protobuf 7 (a2aproject#1260)
Removes the protobuf/types-protobuf < 7 upper bound so the SDK and makes the code type-check cleanly against the protobuf 7 stubs. __Context:__ The <7 cap was added in a2aproject#1019 because protobuf 7 removed FieldDescriptor.label, crashing validate_proto_required_fields(). a2aproject#1158 already made the runtime version-agnostic ("is_repeated" with a "label" fallback), so the ceiling is no longer needed. __Changes:__ - pyproject.toml: drop <7 from protobuf + drop <7 from types-protobuf. - re-locked uv.lock - Static-typing fixes for the stricter types-protobuf 7 stubs (all runtime behavior unchanged; full test suite passes) - field.is_repeated usage fixed in src/a2a/server/routes/_proto_schema.py __Validation:__ - Full test suite passes on protobuf 5.29.5, 6.33.6, 7.36.2 - `ty check`, `ruff check` all pass
1 parent 9a00f9b commit 67ba0a4

6 files changed

Lines changed: 188 additions & 148 deletions

File tree

‎pyproject.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ keywords = ["A2A", "A2A SDK", "A2A Protocol", "Agent2Agent", "Agent 2 Agent"]
1010
dependencies = [
1111
"httpx>=0.28.1",
1212
"pydantic>=2.11.3",
13-
"protobuf>=5.29.5,<7",
13+
"protobuf>=5.29.5",
1414
"google-api-core>=1.26.0",
1515
"json-rpc>=1.15.0",
1616
"googleapis-common-protos>=1.70.0",
@@ -120,7 +120,7 @@ dev = [
120120
"rfc8785>=0.1.4",
121121
"ruff>=0.12.8",
122122
"uv-dynamic-versioning>=0.8.2",
123-
"types-protobuf<7",
123+
"types-protobuf",
124124
"types-requests",
125125
"pre-commit",
126126
"trio",

‎src/a2a/client/transports/grpc.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ def _map_grpc_error(e: grpc.aio.AioRpcError) -> NoReturn:
6464
if status is not None:
6565
exception_cls: type[A2AError] | None = None
6666
for detail in status.details:
67-
if detail.Is(error_details_pb2.ErrorInfo.DESCRIPTOR):
67+
# `.DESCRIPTOR` is typed as a py/C union; `.Is` wants a Descriptor.
68+
if detail.Is(error_details_pb2.ErrorInfo.DESCRIPTOR): # ty: ignore[invalid-argument-type]
6869
error_info = error_details_pb2.ErrorInfo()
6970
detail.Unpack(error_info)
7071
if error_info.domain == 'a2a-protocol.org':
7172
exception_cls = A2A_REASON_TO_ERROR.get(error_info.reason)
72-
elif detail.Is(error_details_pb2.BadRequest.DESCRIPTOR):
73+
elif detail.Is(error_details_pb2.BadRequest.DESCRIPTOR): # ty: ignore[invalid-argument-type]
7374
bad_request = error_details_pb2.BadRequest()
7475
detail.Unpack(bad_request)
7576
data = {'errors': bad_request_to_validation_errors(bad_request)}

‎src/a2a/server/routes/_proto_schema.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from google.protobuf.message import Message
88

99
from a2a.types.a2a_pb2 import SendMessageRequest, TaskPushNotificationConfig
10+
from a2a.utils.proto_utils import _field_is_repeated
1011

1112

1213
REST_BODY_TYPES: dict[tuple[str, str], type[Message]] = {
@@ -68,9 +69,13 @@ def field_schema(
6869
# nullable — they're already inlined as their JSON-Schema equivalent.
6970
# Repeated fields must not return early here — they fall through to the
7071
# array-wrapping block below.
71-
if not field.is_repeated and not _is_required(field) and '$ref' in item:
72+
if (
73+
not _field_is_repeated(field)
74+
and not _is_required(field)
75+
and '$ref' in item
76+
):
7277
return {'oneOf': [item, {'type': 'null'}], 'example': None}
73-
elif field.type == FieldDescriptor.TYPE_ENUM:
78+
elif field.type == FieldDescriptor.TYPE_ENUM and field.enum_type:
7479
values = [v.name for v in field.enum_type.values]
7580
example = next(
7681
(
@@ -92,7 +97,7 @@ def field_schema(
9297
elif field.type == FieldDescriptor.TYPE_BOOL:
9398
item['example'] = False
9499

95-
if field.is_repeated:
100+
if _field_is_repeated(field):
96101
array_schema: dict[str, Any] = {'type': 'array', 'items': item}
97102
# Propagate the item example to the array so Swagger pre-fills one entry
98103
# instead of generating one entry per oneOf branch.

‎src/a2a/server/tasks/database_task_store.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22

33
from collections.abc import Callable
4+
from typing import Any, cast
45

56

67
try:
@@ -151,16 +152,20 @@ def _from_orm(self, task_model: TaskModel) -> Task:
151152
id=task_model.id,
152153
context_id=task_model.context_id,
153154
)
155+
# These JSON columns are annotated with proto types but hold plain
156+
# dicts at rest; ParseDict wants the dict view.
154157
if task_model.status:
155-
ParseDict(task_model.status, task.status)
158+
ParseDict(
159+
cast('dict[str, Any]', task_model.status), task.status
160+
)
156161
if task_model.artifacts:
157162
for art_dict in task_model.artifacts:
158163
art = task.artifacts.add()
159-
ParseDict(art_dict, art)
164+
ParseDict(cast('dict[str, Any]', art_dict), art)
160165
if task_model.history:
161166
for msg_dict in task_model.history:
162167
msg = task.history.add()
163-
ParseDict(msg_dict, msg)
168+
ParseDict(cast('dict[str, Any]', msg_dict), msg)
164169
if task_model.task_metadata:
165170
task.metadata.update(task_model.task_metadata)
166171
return task

‎src/a2a/utils/proto_utils.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
This module provides helper functions for common proto type operations.
1818
"""
1919

20-
from typing import TYPE_CHECKING, Any, TypedDict
20+
from typing import TYPE_CHECKING, Any, TypedDict, cast
2121

2222
from google.api.field_behavior_pb2 import FieldBehavior, field_behavior
2323
from google.protobuf.descriptor import FieldDescriptor
@@ -155,7 +155,8 @@ def _field_is_repeated(field: FieldDescriptor) -> bool:
155155
is_repeated = getattr(field, 'is_repeated', None)
156156
if is_repeated is not None:
157157
return is_repeated
158-
return field.label == FieldDescriptor.LABEL_REPEATED
158+
# protobuf 7 removed `label`; only reached on the <6.31 fallback path.
159+
return getattr(field, 'label', None) == FieldDescriptor.LABEL_REPEATED
159160

160161

161162
def parse_params(params: QueryParams, message: ProtobufMessage) -> None:
@@ -170,7 +171,9 @@ def parse_params(params: QueryParams, message: ProtobufMessage) -> None:
170171
https://a2a-protocol.org/latest/specification/#115-query-parameter-naming-for-request-parameters
171172
"""
172173
descriptor = message.DESCRIPTOR
173-
fields = {f.camelcase_name: f for f in descriptor.fields}
174+
fields = {
175+
f.camelcase_name: cast('FieldDescriptor', f) for f in descriptor.fields
176+
}
174177
processed: dict[str, Any] = {}
175178

176179
keys = params.keys()
@@ -261,7 +264,10 @@ def _recurse_validation(
261264
if msg.HasField(field.name):
262265
sub_errs = _validate_proto_required_fields_internal(val)
263266
_append_nested_errors(errors, field.name, sub_errs)
264-
elif field.message_type.GetOptions().map_entry:
267+
elif (
268+
field.message_type is not None
269+
and field.message_type.GetOptions().map_entry
270+
):
265271
for k, v in val.items():
266272
if isinstance(v, ProtobufMessage):
267273
sub_errs = _validate_proto_required_fields_internal(v)
@@ -280,9 +286,12 @@ def _validate_proto_required_fields_internal(
280286
desc = msg.DESCRIPTOR
281287
errors: list[ValidationDetail] = []
282288

283-
for field in desc.fields:
289+
for raw_field in desc.fields:
290+
# `desc.fields` is typed as a union of the pure-Python and C-extension
291+
# FieldDescriptor; narrow to the canonical one for the helpers below.
292+
field = cast('FieldDescriptor', raw_field)
284293
options = field.GetOptions()
285-
if FieldBehavior.REQUIRED in options.Extensions[field_behavior]:
294+
if FieldBehavior.REQUIRED in options.Extensions[field_behavior]: # type: ignore[index] # ty: ignore[invalid-argument-type]
286295
violation = _check_required_field_violation(msg, field)
287296
if violation:
288297
errors.append(violation)

0 commit comments

Comments
 (0)