Skip to content

Commit 075a7b7

Browse files
committed
Updates
1 parent 8730c51 commit 075a7b7

7 files changed

Lines changed: 10 additions & 57 deletions

File tree

src/a2a/client/transports/http_helpers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
from a2a.client.errors import A2AClientError, A2AClientTimeoutError
1313

1414

15+
def _default_sse_error_handler(sse_data: str) -> NoReturn:
16+
raise A2AClientError(f'SSE stream error event received: {sse_data}')
17+
18+
1519
@contextmanager
1620
def handle_http_exceptions(
1721
status_error_handler: Callable[[httpx.HTTPStatusError], NoReturn]
@@ -70,7 +74,7 @@ async def send_http_stream_request(
7074
method: str,
7175
url: str,
7276
status_error_handler: Callable[[httpx.HTTPStatusError], NoReturn],
73-
sse_error_handler: Callable[[str], NoReturn],
77+
sse_error_handler: Callable[[str], NoReturn] = _default_sse_error_handler,
7478
**kwargs: Any,
7579
) -> AsyncGenerator[str]:
7680
"""Sends a streaming HTTP request, yielding SSE data strings and handling exceptions.

src/a2a/compat/v0_3/jsonrpc_adapter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,9 @@ async def event_generator(
306306
)
307307
)
308308
yield {
309-
'event': 'error',
310309
'data': err_resp.model_dump_json(
311310
by_alias=True, exclude_none=True
312-
),
311+
)
313312
}
314313

315314
return EventSourceResponse(event_generator(stream_gen))

src/a2a/compat/v0_3/jsonrpc_transport.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,6 @@ def _handle_http_error(self, e: httpx.HTTPStatusError) -> NoReturn:
415415
"""Handles HTTP errors for standard requests."""
416416
raise A2AClientError(f'HTTP Error: {e.response.status_code}') from e
417417

418-
def _handle_sse_error(self, sse_data: str) -> NoReturn:
419-
"""Handles SSE error events by parsing JSON-RPC error payload and raising the appropriate domain error."""
420-
data = json.loads(sse_data)
421-
if 'error' in data:
422-
raise self._create_jsonrpc_error(data['error'])
423-
raise A2AClientError(f'SSE stream error: {sse_data}')
424-
425418
async def _send_stream_request(
426419
self,
427420
json_data: dict[str, Any],
@@ -437,7 +430,6 @@ async def _send_stream_request(
437430
'POST',
438431
self.url,
439432
self._handle_http_error,
440-
self._handle_sse_error,
441433
json=json_data,
442434
**http_kwargs,
443435
):

src/a2a/compat/v0_3/rest_adapter.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88

99
if TYPE_CHECKING:
10-
from sse_starlette.event import ServerSentEvent
1110
from sse_starlette.sse import EventSourceResponse
1211
from starlette.requests import Request
1312
from starlette.responses import JSONResponse, Response
@@ -18,7 +17,6 @@
1817
_package_starlette_installed = True
1918
else:
2019
try:
21-
from sse_starlette.event import ServerSentEvent
2220
from sse_starlette.sse import EventSourceResponse
2321
from starlette.requests import Request
2422
from starlette.responses import JSONResponse, Response
@@ -29,7 +27,6 @@
2927
Request = Any
3028
JSONResponse = Any
3129
Response = Any
32-
ServerSentEvent = Any
3330

3431
_package_starlette_installed = False
3532

@@ -40,7 +37,6 @@
4037
from a2a.server.context import ServerCallContext
4138
from a2a.server.routes import CallContextBuilder, DefaultCallContextBuilder
4239
from a2a.utils.error_handlers import (
43-
build_rest_error_payload,
4440
rest_error_handler,
4541
rest_stream_error_handler,
4642
)
@@ -105,16 +101,9 @@ async def _handle_streaming_request(
105101

106102
async def event_generator(
107103
stream: AsyncIterable[Any],
108-
) -> AsyncIterator[str | ServerSentEvent]:
109-
try:
110-
async for item in stream:
111-
yield json.dumps(item)
112-
except Exception as e:
113-
logger.exception('Error during v0.3 REST SSE stream')
114-
yield ServerSentEvent(
115-
data=json.dumps(build_rest_error_payload(e)),
116-
event='error',
117-
)
104+
) -> AsyncIterator[str]:
105+
async for item in stream:
106+
yield json.dumps(item)
118107

119108
return EventSourceResponse(
120109
event_generator(method(request, call_context))

src/a2a/compat/v0_3/rest_transport.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@
4444
TaskPushNotificationConfig,
4545
)
4646
from a2a.utils.constants import PROTOCOL_VERSION_0_3, VERSION_HEADER
47-
from a2a.utils.errors import (
48-
A2A_REASON_TO_ERROR,
49-
JSON_RPC_ERROR_CODE_MAP,
50-
MethodNotFoundError,
51-
)
47+
from a2a.utils.errors import JSON_RPC_ERROR_CODE_MAP, MethodNotFoundError
5248
from a2a.utils.telemetry import SpanKind, trace_class
5349

5450

@@ -373,30 +369,6 @@ def _handle_http_error(self, e: httpx.HTTPStatusError) -> NoReturn:
373369

374370
raise A2AClientError(f'HTTP Error {status_code}: {e}') from e
375371

376-
def _handle_sse_error(self, sse_data: str) -> NoReturn:
377-
"""Handles SSE error events by parsing the REST error payload and raising the appropriate A2AError."""
378-
error_payload = json.loads(sse_data)
379-
error_data = error_payload.get('error', {})
380-
381-
message = error_data.get('message', sse_data)
382-
details = error_data.get('details', [])
383-
if not isinstance(details, list):
384-
details = []
385-
386-
for d in details:
387-
if (
388-
isinstance(d, dict)
389-
and d.get('@type') == 'type.googleapis.com/google.rpc.ErrorInfo'
390-
):
391-
reason = d.get('reason')
392-
if isinstance(reason, str):
393-
exception_cls = A2A_REASON_TO_ERROR.get(reason)
394-
if exception_cls:
395-
raise exception_cls(message)
396-
break
397-
398-
raise A2AClientError(message)
399-
400372
async def _send_stream_request(
401373
self,
402374
method: str,
@@ -414,7 +386,6 @@ async def _send_stream_request(
414386
method,
415387
f'{self.url}{path}',
416388
self._handle_http_error,
417-
self._handle_sse_error,
418389
json=json,
419390
**http_kwargs,
420391
):

tests/compat/v0_3/test_jsonrpc_transport.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ async def mock_generator(*args, **kwargs):
480480
'POST',
481481
'http://example.com',
482482
transport._handle_http_error,
483-
transport._handle_sse_error,
484483
json={'some': 'data'},
485484
headers={'a2a-version': '0.3'},
486485
)

tests/compat/v0_3/test_rest_transport.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,6 @@ async def mock_generator(*args, **kwargs):
634634
'POST',
635635
'http://example.com/test',
636636
transport._handle_http_error,
637-
transport._handle_sse_error,
638637
json=None,
639638
headers={'a2a-version': '0.3'},
640639
)

0 commit comments

Comments
 (0)