fix(gateway): preserve retry semantics when APICallError is converted to GatewayError#14254
Open
giulio-leone wants to merge 1 commit intovercel:mainfrom
Open
fix(gateway): preserve retry semantics when APICallError is converted to GatewayError#14254giulio-leone wants to merge 1 commit intovercel:mainfrom
giulio-leone wants to merge 1 commit intovercel:mainfrom
Conversation
… to GatewayError When `@ai-sdk/gateway` converts an `APICallError` into a `GatewayError` subclass (via `asGatewayError`), the SDK retry logic no longer recognizes the error as retryable because `APICallError.isInstance()` returns false for `GatewayError`. This causes `maxRetries` to be silently ignored for transient 503, 429, and 408 gateway failures. Changes: - Add `isRetryable` field to `GatewayError` base class with the same default logic as `APICallError` (408/429/500+ are retryable) - Propagate `isRetryable` from the original `APICallError` through `asGatewayError()` → `createGatewayErrorFromResponse()` - Update retry logic to recognize both `APICallError` and `GatewayError` as retryable error types - Extract retry-after headers from GatewayError's cause chain so provider rate-limit headers still work after error conversion Fixes vercel#14216 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
Fixes #14216
When
@ai-sdk/gatewayconverts anAPICallErrorinto aGatewayErrorsubclass (viaasGatewayError()), the SDK retry logic no longer recognizes the error as retryable becauseAPICallError.isInstance()returnsfalseforGatewayError. This causesmaxRetriesto be silently ignored for transient 503, 429, and 408 gateway failures.Root Cause
The retry logic in
retry-with-exponential-backoff.tsonly checkedAPICallError.isInstance(error) && error.isRetryable. AfterasGatewayError()wraps the originalAPICallErrorinto aGatewayErrorsubclass,APICallError.isInstance()returnsfalse, so the error is never retried.Changes
@ai-sdk/gatewayisRetryablefield toGatewayErrorbase class with the same default logic asAPICallError(408/429/500+ are retryable)GatewayErrorsubclasses to accept and forwardisRetryablecreateGatewayErrorFromResponse()to accept and forwardisRetryableasGatewayError()to propagateisRetryablefrom the originalAPICallErrorai(core)APICallErrorandGatewayErroras retryable error typesgetResponseHeaders()helper to extract retry-after headers fromGatewayError's cause chain (the originalAPICallErroris preserved ascause)Test Coverage
isRetryabledefaults across allGatewayErrorsubclassesisRetryablepropagation fromAPICallErrorthroughasGatewayError()