-
Notifications
You must be signed in to change notification settings - Fork 4.2k
AI Gateway breaks SDK retry: GatewayInternalServerError not recognized by maxRetries #14216
Copy link
Copy link
Labels
ai/corecore functions like generateText, streamText, etc. Provider utils, and provider spec.core functions like generateText, streamText, etc. Provider utils, and provider spec.ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` labelrelated to a provider package. Must be assigned together with at least one `provider/*` labelbugSomething isn't working as documentedSomething isn't working as documentedprovider/gatewayIssues related to the @ai-sdk/gateway providerIssues related to the @ai-sdk/gateway providerreproduction provided
Metadata
Metadata
Assignees
Labels
ai/corecore functions like generateText, streamText, etc. Provider utils, and provider spec.core functions like generateText, streamText, etc. Provider utils, and provider spec.ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` labelrelated to a provider package. Must be assigned together with at least one `provider/*` labelbugSomething isn't working as documentedSomething isn't working as documentedprovider/gatewayIssues related to the @ai-sdk/gateway providerIssues related to the @ai-sdk/gateway providerreproduction provided
Type
Fields
Give feedbackNo fields configured for issues without a type.
Description
The AI Gateway's error handling converts retryable
APICallErrorinstances intoGatewayInternalServerErrorinstances before they reach the SDK's retry logic. Since the retry function checksAPICallError.isInstance(error), Gateway errors are never retried regardless of themaxRetriessetting.Proof
Ran this in Node.js with the installed packages:
The error has the Gateway marker (
vercel.ai.gateway.error) but NOT the APICallError marker (vercel.ai.error.AI_APICallError), somaxRetriesnever activates.Root cause
In
@ai-sdk/gateway, thedoStream()anddoGenerate()methods catch errors and convert them:asGatewayError()(line ~433) takes a retryableAPICallError(statusCode 503, isRetryable: true) and converts it to aGatewayInternalServerErrorwhich extendsGatewayError(NOTAPICallError).The SDK's retry function at
ai/dist/internal/index.mjs:1207then fails:Impact
Any application using
@ai-sdk/gatewaywithmaxRetries > 0gets zero retries on transient errors (503, 429). The setting is silently ignored. This affectsstreamText,generateText,generateObject, andstreamObject.Applications using provider SDKs directly (
@ai-sdk/google,@ai-sdk/openai) are not affected because their errors pass through asAPICallError.Expected behavior
Gateway errors with retryable status codes (503, 429, 408) should be retried according to
maxRetries.Possible fixes
GatewayErrorextendAPICallError(or carry the marker)GatewayErrorsetisRetryablebased on status codeGatewayError.isInstance()APICallErrortoGatewayErrorin the catch blockEnvironment