Skip to content

Commit a00a504

Browse files
committed
fix(ai): retry more temporary provider failures
These failures ended the turn without an automatic retry, because none of their messages matched the retryable patterns: - Anthropic's internal error, `{"type":"api_error",...}`. Before streaming it arrives as HTTP 500 and is retried by status; once the stream has started it arrives as an event without a status, so only its text can match. - OpenAI-compatible streams that end with `finish_reason: "error"`, which OpenRouter sends when its provider fails mid-stream. - OpenRouter's "Provider returned an empty response" and "...The model stopped before completing the response." - recursive's "Repeat the request later" and sference's queue "admission ceiling". Cancellations ("The operation was aborted"), other Anthropic error types and refusals stay non-retryable, and the quota exclusions are still checked first.
1 parent 25190b7 commit a00a504

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎packages/ai/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Fixed `retryAssistantCall()` retaining an abort listener after each completed backoff, causing listener buildup across repeated retries ([#10506](https://github.com/earendil-works/pi/issues/10506))
88
- Fixed Cloudflare's HTTP 525 (SSL handshake failed) not being retried like its 520 and 524 errors
99
- Fixed gateway errors such as "The provider is temporarily unavailable. Retry shortly.", "The provider ended the response before it was complete.", "The connection to the provider was reset.", and "The provider reported a failure while generating the response." not being retried ([#12227](https://github.com/earendil-works/pi/issues/12227))
10+
- Fixed temporary provider failures not being retried automatically: Anthropic `api_error` events during streaming, OpenRouter's "Provider returned an empty response" and "stopped before completing the response", streams that end with `finish_reason: "error"`, and "Repeat the request later" and "admission ceiling" responses
1011

1112
## [1.6.0] - 2026-11-20
1213

‎packages/ai/src/utils/retry.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([
4141
"servers are currently busy",
4242
"currently experiencing high demand",
4343
"model is at capacity",
44+
// A request that waited too long in the provider's queue, e.g. sference's "waited 73.4s in
45+
// queue, reaching this model's admission ceiling of 60s; retry after the hinted delay".
46+
"admission ceiling",
4447
"rate.?limit",
4548
"too many requests",
4649
// Status codes must not be part of a longer word or number, e.g. a request id or byte count (#11057).
@@ -59,6 +62,11 @@ const RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([
5962
// "Provider returned error" responses (#2264) and gateway wording such as
6063
// "The upstream provider returned an error" (#10842).
6164
"provider.?returned.?(?:an.?)?error",
65+
// Anthropic's internal error, which it sends as HTTP 500 before streaming and as an event
66+
// without a status once the stream has started, e.g. {"type":"error","error":{"type":
67+
// "api_error","message":"Unable to complete this request right now. Please try again."}}.
68+
// Gateways that copy Anthropic's format use it for their own upstream failures as well.
69+
'"type"\\s*:\\s*"api_error"',
6270
"exceeded request buffer limit while retrying upstream",
6371
// Gateway failure relaying the upstream response stream, sent as an OpenAI
6472
// Responses `response.failed` event without an error code (#10920).
@@ -76,8 +84,7 @@ const RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([
7684
"connection.?error",
7785
"connection.?refused",
7886
"connection.?lost",
79-
// Gateway mid-stream drop relayed as an Anthropic-style `api_error` event,
80-
// e.g. {"type":"error","error":{"type":"api_error","message":"Connection closed."}} (#12064).
87+
// A gateway's mid-stream drop, e.g. "Connection closed." (#12064).
8188
"connection.?closed",
8289
"other side closed",
8390
"fetch failed",
@@ -114,6 +121,13 @@ const RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([
114121
"stream ended before message_stop",
115122
"stream ended before a terminal response event",
116123
"http2 request did not get a response",
124+
// An OpenAI-compatible stream that ended with `finish_reason: "error"`, which OpenRouter
125+
// sends when its provider fails mid-stream; see `mapStopReason` in openai-completions.ts.
126+
"provider finish_reason: error",
127+
// OpenRouter: "Provider returned an empty response", and its wrapper for a provider that
128+
// gave up, e.g. "Upstream error from Relace: The model stopped before completing the response."
129+
"returned an empty response",
130+
"stopped before completing the response",
117131
// Codex CLI mid-stream disconnect, relayed by proxies that wrap the Codex CLI,
118132
// e.g. "stream disconnected before completion: stream closed before response.completed" (#11057).
119133
"stream disconnected before completion",
@@ -138,6 +152,8 @@ const RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([
138152
"you can retry your request",
139153
"try your request again",
140154
"please retry your request",
155+
// recursive: "Unable to process request. Repeat the request later."
156+
"repeat the request later",
141157

142158
// gRPC based providers (e.g. NVIDIA NIM)
143159
"ResourceExhausted",

‎packages/ai/test/retry.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,30 @@ describe("provider retry classification", () => {
240240
])("does not match status codes inside ids or longer numbers: %s", (errorMessage) => {
241241
expect(isRetryableAssistantError(fauxAssistantMessage("", { stopReason: "error", errorMessage }))).toBe(false);
242242
});
243+
244+
it.each([
245+
"Unable to process request. Repeat the request later.",
246+
"waited 73.4s in queue, reaching this model's admission ceiling of 60s; retry after the hinted delay",
247+
'{"type":"error","error":{"details":null,"type":"api_error","message":"Unable to complete this request right now. Please try again."},"request_id":"req_011CgfE1bTgPTDsor4TsApFv"}',
248+
"Provider finish_reason: error",
249+
"Provider returned an empty response",
250+
"Upstream error from Relace: The model stopped before completing the response.",
251+
])("retries temporary provider failures: %s", (errorMessage) => {
252+
expect(isRetryableAssistantError(fauxAssistantMessage("", { stopReason: "error", errorMessage }))).toBe(true);
253+
});
254+
255+
it.each([
256+
// A cancellation, usually the user's.
257+
"The operation was aborted",
258+
// Only Anthropic's internal error is retried, not other error types.
259+
'{"type":"error","error":{"type":"invalid_request_error","message":"messages: at least one message is required"}}',
260+
// A refusal ends the stream with a finish reason other than `error`.
261+
"Provider finish_reason: content_filter",
262+
// The quota exclusions are checked before the retryable patterns.
263+
'{"type":"error","error":{"type":"api_error","message":"Monthly quota exceeded"}}',
264+
])("does not retry: %s", (errorMessage) => {
265+
expect(isRetryableAssistantError(fauxAssistantMessage("", { stopReason: "error", errorMessage }))).toBe(false);
266+
});
243267
});
244268

245269
describe("retryDelayMs", () => {

0 commit comments

Comments
 (0)