ai-retry 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -170,11 +170,10 @@ const retryable = createRetryable({
170
170
 
171
171
  #### Retry After Delay
172
172
 
173
- Handle retryable errors with delays and respect `retry-after` headers from rate-limited responses. This is useful for handling 429 (Too Many Requests) and 503 (Service Unavailable) errors.
174
-
175
- > [!NOTE]
176
- > If the response contains a [`retry-after`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Retry-After) header, it will be prioritized over the configured delay.
173
+ If an error is retryable, such as 429 (Too Many Requests) or 503 (Service Unavailable) errors, it will be retried after a delay.
174
+ The delay and exponential backoff can be configured. If the response contains a [`retry-after`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Retry-After) header, it will be prioritized over the configured delay.
177
175
 
176
+ Note that this retryable does not accept a model parameter, it will always retry the model from the latest failed attempt.
178
177
 
179
178
  ```typescript
180
179
  import { retryAfterDelay } from 'ai-retry/retryables';
@@ -187,14 +186,14 @@ const retryableModel = createRetryable({
187
186
 
188
187
  // Or retry with exponential backoff (2s, 4s, 8s)
189
188
  retryAfterDelay({ delay: 2000, backoffFactor: 2, maxAttempts: 3 }),
190
-
191
- // Or switch to a different model after delay
192
- retryAfterDelay(openai('gpt-4-mini'), { delay: 1000 }),
189
+
190
+ // Or retry only if the response contains a retry-after header
191
+ retryAfterDelay({ maxAttempts: 3 }),
193
192
  ],
194
193
  });
195
194
  ```
196
195
 
197
- By default, if a [`retry-after-ms`](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/provisioned-get-started#what-should--i-do-when-i-receive-a-429-response) or `retry-after` header is present in the response, it will be prioritized over the configured delay. The delay from the header will be capped at 60 seconds for safety. If no headers are present, the configured delay or exponential backoff will be used.
196
+ By default, if a [`retry-after-ms`](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/provisioned-get-started#what-should--i-do-when-i-receive-a-429-response) or [`retry-after`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Retry-After) header is present in the response, it will be prioritized over the configured delay. The delay from the header will be capped at 60 seconds for safety.
198
197
 
199
198
  #### Fallbacks
200
199
 
@@ -370,7 +369,7 @@ There are several built-in retryables:
370
369
  - [`contentFilterTriggered`](./src/retryables/content-filter-triggered.ts): Content filter was triggered based on the prompt or completion.
371
370
  - [`requestTimeout`](./src/retryables/request-timeout.ts): Request timeout occurred.
372
371
  - [`requestNotRetryable`](./src/retryables/request-not-retryable.ts): Request failed with a non-retryable error.
373
- - [`retryAfterDelay`](./src/retryables/retry-after-delay.ts): Retry with exponential backoff and respect `retry-after` headers for rate limiting.
372
+ - [`retryAfterDelay`](./src/retryables/retry-after-delay.ts): Retry with delay and exponential backoff and respect `retry-after` headers.
374
373
  - [`serviceOverloaded`](./src/retryables/service-overloaded.ts): Response with status code 529 (service overloaded).
375
374
  - Use this retryable to handle Anthropic's overloaded errors.
376
375
 
@@ -23,11 +23,10 @@ declare function requestTimeout<MODEL extends LanguageModelV2 | EmbeddingModelV2
23
23
  //#endregion
24
24
  //#region src/retryables/retry-after-delay.d.ts
25
25
  /**
26
- * Retry with the same or a different model if the error is retryable with a delay.
26
+ * Retry the current failed attempt with the same model, if the error is retryable.
27
27
  * Uses the `Retry-After` or `Retry-After-Ms` headers if present.
28
- * Otherwise uses the specified `delay` with exponential backoff if `backoffFactor` is provided.
28
+ * Otherwise uses the specified `delay` and `backoffFactor` if provided.
29
29
  */
30
- declare function retryAfterDelay<MODEL extends LanguageModelV2 | EmbeddingModelV2>(model: MODEL, options?: RetryableOptions<MODEL>): Retryable<MODEL>;
31
30
  declare function retryAfterDelay<MODEL extends LanguageModelV2 | EmbeddingModelV2>(options: RetryableOptions<MODEL>): Retryable<MODEL>;
32
31
  //#endregion
33
32
  //#region src/retryables/service-overloaded.d.ts
@@ -1,4 +1,4 @@
1
- import { a as isModelV2, n as isErrorAttempt, o as isObject, s as isResultAttempt, u as isString } from "../utils-BlCGaP0E.js";
1
+ import { n as isErrorAttempt, o as isObject, s as isResultAttempt, u as isString } from "../utils-BlCGaP0E.js";
2
2
  import { isAbortError } from "@ai-sdk/provider-utils";
3
3
  import { APICallError } from "ai";
4
4
 
@@ -69,23 +69,6 @@ function requestTimeout(model, options) {
69
69
  };
70
70
  }
71
71
 
72
- //#endregion
73
- //#region src/internal/resolve-retryable-options.ts
74
- /**
75
- * Helper to resolve `RetryableOptions` from either a model and/or options object.
76
- * Used to support function overloads in retryables:
77
- * - `retryable(model)`
78
- * - `retryable(model, options)`
79
- * - `retryable(options)`
80
- */
81
- function resolveRetryableOptions(modelOrOptions, options) {
82
- if (isModelV2(modelOrOptions)) return {
83
- ...options,
84
- model: modelOrOptions
85
- };
86
- return modelOrOptions;
87
- }
88
-
89
72
  //#endregion
90
73
  //#region src/parse-retry-headers.ts
91
74
  function parseRetryHeaders(headers) {
@@ -108,23 +91,28 @@ function parseRetryHeaders(headers) {
108
91
  //#endregion
109
92
  //#region src/retryables/retry-after-delay.ts
110
93
  const MAX_RETRY_AFTER_MS = 6e4;
111
- function retryAfterDelay(modelOrOptions, options) {
112
- const resolvedOptions = resolveRetryableOptions(modelOrOptions, options);
94
+ /**
95
+ * Retry the current failed attempt with the same model, if the error is retryable.
96
+ * Uses the `Retry-After` or `Retry-After-Ms` headers if present.
97
+ * Otherwise uses the specified `delay` and `backoffFactor` if provided.
98
+ */
99
+ function retryAfterDelay(options) {
113
100
  return (context) => {
114
101
  const { current } = context;
115
102
  if (isErrorAttempt(current)) {
116
103
  const { error } = current;
117
104
  if (APICallError.isInstance(error) && error.isRetryable === true) {
118
- const model = resolvedOptions.model ?? current.model;
105
+ const model = current.model;
119
106
  const headerDelay = parseRetryHeaders(error.responseHeaders);
120
107
  if (headerDelay !== null) return {
121
108
  model,
122
- ...resolvedOptions,
123
- delay: Math.min(headerDelay, MAX_RETRY_AFTER_MS)
109
+ ...options,
110
+ delay: Math.min(headerDelay, MAX_RETRY_AFTER_MS),
111
+ backoffFactor: 1
124
112
  };
125
113
  return {
126
114
  model,
127
- ...resolvedOptions
115
+ ...options
128
116
  };
129
117
  }
130
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-retry",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "AI SDK Retry",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",