Skip to content

fix: handle SSE errors occurred after stream started#894

Draft
ishymko wants to merge 3 commits into1.0-devfrom
ishymko/sse-interim-errors
Draft

fix: handle SSE errors occurred after stream started#894
ishymko wants to merge 3 commits into1.0-devfrom
ishymko/sse-interim-errors

Conversation

@ishymko
Copy link
Member

@ishymko ishymko commented Mar 24, 2026

The spec doesn't defined this behavior: a2aproject/A2A#1262, but currently it'd close the connection.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the robustness of streaming connections by introducing a comprehensive error handling mechanism for Server-Sent Events (SSE). Previously, errors occurring after a stream had started would lead to an abrupt connection termination. Now, both client and server components are updated to gracefully catch these mid-stream exceptions, serialize them into standardized SSE error events, and allow clients to process these errors without losing the entire connection. This ensures a more resilient and predictable streaming experience.

Highlights

  • Robust SSE Error Handling: Implemented a mechanism to catch and gracefully handle errors that occur mid-stream in Server-Sent Events (SSE), preventing abrupt connection closures.
  • Client-Side Error Propagation: Enhanced the client's send_http_stream_request to accept a custom SSE error handler, allowing domain-specific error mapping for JSON-RPC and REST transports.
  • Server-Side Error Emission: Modified server-side stream generators (for both REST and JSON-RPC) to catch exceptions during streaming and emit them as event: error SSE messages to the client.
  • Error Payload Refactoring: Introduced a reusable build_rest_error_payload function to standardize error response formatting across REST endpoints.
  • Comprehensive Testing: Added new integration and unit tests to validate the correct behavior of mid-stream SSE error handling on both client and server.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link

github-actions bot commented Mar 24, 2026

🧪 Code Coverage (vs 1.0-dev)

⬇️ Download Full Report

Base PR Delta
src/a2a/client/transports/http_helpers.py 94.64% 93.55% 🔴 -1.09%
src/a2a/client/transports/jsonrpc.py 86.71% 86.00% 🔴 -0.71%
src/a2a/client/transports/rest.py 91.52% 88.76% 🔴 -2.75%
src/a2a/server/apps/rest/rest_adapter.py 83.53% 83.33% 🔴 -0.20%
src/a2a/server/routes/jsonrpc_dispatcher.py 85.78% 84.23% 🔴 -1.55%
src/a2a/utils/error_handlers.py 95.65% 96.05% 🟢 +0.40%
Total 91.43% 91.29% 🔴 -0.14%

Generated by coverage-comment.yml

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly enhances error handling for streaming HTTP requests, specifically for Server-Sent Events (SSE) across both REST and JSON-RPC transports. The changes introduce a robust mechanism to catch exceptions that occur mid-stream and emit them as structured error events (SSE 'event: error') to the client, preventing stream crashes. This involved modifying the send_http_stream_request function to accept a dedicated SSE error handler, and implementing _handle_sse_error methods in both JsonRpcTransport and RestTransport to parse these incoming error events. On the server side, rest_adapter.py and jsonrpc_dispatcher.py were updated to catch exceptions during stream generation and format them into appropriate SSE error payloads. A new utility function, build_rest_error_payload, was added to standardize REST error responses. Comprehensive integration and unit tests were also added or updated to validate this new error handling behavior. A critical issue was identified where the JsonRpcTransport's _send_stream_request method passes None for a status_error_handler argument that was made mandatory in this PR, which will cause a TypeError at runtime.

Comment on lines 351 to +352
None,
self._handle_sse_error,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The call to send_http_stream_request passes None for the status_error_handler argument. However, the signature for send_http_stream_request in src/a2a/client/transports/http_helpers.py was changed in this PR to make this argument mandatory, no longer allowing None. This will result in a TypeError at runtime.

To fix this, you should provide a valid error handler. RestTransport defines a _handle_http_error method for this purpose. A similar method should be added to JsonRpcTransport and passed here instead of None.

Suggested change
None,
self._handle_sse_error,
self._handle_http_error,
self._handle_sse_error,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant