feat(client): add RetryTransport for automatic retry with exponential backoff#1018
Open
cchinchilla-dev wants to merge 3 commits intoa2aproject:mainfrom
Open
feat(client): add RetryTransport for automatic retry with exponential backoff#1018cchinchilla-dev wants to merge 3 commits intoa2aproject:mainfrom
cchinchilla-dev wants to merge 3 commits intoa2aproject:mainfrom
Conversation
🧪 Code Coverage (vs
|
| Base | PR | Delta | |
|---|---|---|---|
| src/a2a/client/transports/retry.py (new) | — | 96.43% | — |
| Total | 93.00% | 93.07% | 🟢 +0.07% |
Generated by coverage-comment.yml
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a RetryTransport decorator for the A2A client, enabling automatic retries with exponential backoff and jitter for transient failures. It includes a default retry predicate that handles specific HTTP status codes, gRPC error codes, and network timeouts. The implementation covers both standard and streaming RPC methods, ensuring that streaming calls only retry if the failure occurs before the first event is yielded. Comprehensive unit and integration tests have been added to verify the retry logic across different transport protocols. I have no feedback to provide.
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
bash scripts/format.shfrom the repository root to format).Closes #871 🦕
Summary
Adds a
RetryTransportdecorator wrapping anyClientTransport, following the existingTenantTransportDecoratorpattern.Public API
RetryTransport,default_retry_predicate,RetryPredicate, andOnRetryCallbackare exported froma2a.client.transports.Implementation details
Default retry classification
default_retry_predicateinspectserror.__cause__(falling back toerror.__context__when__cause__isNone) and retries on:A2AClientTimeoutError(always — covers HTTP timeouts and gRPCDEADLINE_EXCEEDED, which_map_grpc_errornormalizes to this type).A2AClientErrorchained fromhttpx.RequestError.A2AClientErrorchained fromhttpx.HTTPStatusError(408 | 429 | 502 | 503 | 504).A2AClientErrorchained fromgrpc.aio.AioRpcError(UNAVAILABLE | RESOURCE_EXHAUSTED).Domain errors (
TaskNotFoundError, etc., which inheritA2AErrornotA2AClientError), HTTP 5xx other than 502/503/504 (replaying server bugs is not safe), and JSON / SSE decode errors are never retried. Pluggable viaretry_predicate.Streaming, cancellation, resource safety
send_message_streamingandsubscriberetry only if the connection fails before the first event is yielded. Once any event has been emitted, subsequent failures propagate without retry — no partial-stream replay.aclose()-d on every exit path — success, retry, exception, and consumer-sidebreak. Both_execute_streaming_with_retryand the publicsend_message_streaming/subscribewrappers propagateaclosethrough the chain.asyncio.CancelledErroris re-raised explicitly from every retry path (_execute_with_retry,_execute_streaming_with_retry,_safe_aclose,_delay_and_notify) so cancellation during backoff is never swallowed.Backoff
Full-jitter exponential:
delay = random.uniform(0, min(base_delay * 2^attempt, max_delay)). Setjitter=Falsefor deterministic delays.Methods that bypass retry
close()delegates directly without retry — lifecycle operation, not data exchange.Validation
Tests. 57 unit tests in
tests/client/transports/test_retry.py(predicate classification, backoff math, streaming semantics, consumer-break cleanup, cancellation, sync/asyncon_retry, constructor validation, exports). 3 end-to-end tests intests/integration/test_retry_integration.pyexercisingClientFactory → BaseClient → RetryTransportagainst Starlette servers with transient 503 middleware.Smoke-tested end-to-end against real uvicorn and grpc.aio.server localhost servers with fault injection. HTTP and gRPC paths verified for retry, exhaustion, non-retryable propagation, streaming semantics, socket cleanup, and cancellation.
Scope
Purely additive:
src/a2a/client/transports/retry.pytests/client/transports/test_retry.pytests/integration/test_retry_integration.pysrc/a2a/client/transports/__init__.pyNo changes to existing transports, error types, or interceptor APIs. No new runtime dependencies — the
grpcimport is conditional, matching the SDK's existing pattern for optional dependencies.Deferred follow-ups
These came up during review but were intentionally kept out of scope to keep this PR focused. Happy to follow up if any of them are wanted:
ClientFactoryintegration (e.g.,ClientConfig.transport_decorators: list[Callable[[ClientTransport], ClientTransport]]) so users can wrap declaratively instead of composing transports manually.Retry-Afterheader support for HTTP 429/503 responses.Happy to iterate on any of this based on review.