ai 7.0.186 → 7.0.187

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/{default-stop-condition-CVw9jxWu.js → default-stop-condition-CeJWqvOg.js} +19 -13
  3. package/dist/default-stop-condition-CeJWqvOg.js.map +1 -0
  4. package/dist/{index-D1zb0Eh5.d.ts → index-Dg74caoY.d.ts} +39 -11
  5. package/dist/index-Dg74caoY.d.ts.map +1 -0
  6. package/dist/index.d.ts +2 -2
  7. package/dist/index.js +66 -14
  8. package/dist/index.js.map +1 -1
  9. package/dist/internal/index.d.ts +1 -1
  10. package/dist/internal/index.js +1 -1
  11. package/docs/03-agents/07-workflow-agent.mdx +22 -5
  12. package/docs/03-ai-sdk-core/18-tools.mdx +10 -8
  13. package/docs/03-ai-sdk-core/32-decisions.mdx +4 -0
  14. package/docs/03-ai-sdk-core/36-transcription.mdx +12 -13
  15. package/docs/03-ai-sdk-core/36-translation.mdx +18 -23
  16. package/docs/03-ai-sdk-core/50-error-handling.mdx +6 -0
  17. package/docs/03-ai-sdk-core/60-telemetry.mdx +14 -12
  18. package/docs/04-ai-sdk-ui/21-error-handling.mdx +11 -9
  19. package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +1 -1
  20. package/docs/07-reference/01-ai-sdk-core/{11-stream-translate.mdx → 11-stream-speech-translation.mdx} +9 -16
  21. package/docs/07-reference/01-ai-sdk-core/{11-stream-transcribe.mdx → 11-stream-transcription.mdx} +7 -14
  22. package/docs/07-reference/01-ai-sdk-core/index.mdx +4 -4
  23. package/docs/07-reference/04-ai-sdk-workflow/01-workflow-agent.mdx +14 -0
  24. package/docs/07-reference/05-ai-sdk-errors/ai-no-transcript-generated-error.mdx +1 -0
  25. package/docs/07-reference/05-ai-sdk-errors/ai-no-translation-generated-error.mdx +1 -0
  26. package/package.json +17 -13
  27. package/src/error/no-transcript-generated-error.ts +11 -2
  28. package/src/error/no-translation-generated-error.ts +13 -2
  29. package/src/generate-text/stream-text.ts +27 -0
  30. package/src/logger/deprecations.ts +2 -0
  31. package/src/model/resolve-model.ts +3 -3
  32. package/src/transcribe/index.ts +19 -1
  33. package/src/transcribe/stream-transcribe.ts +21 -11
  34. package/src/translate/index.ts +41 -3
  35. package/src/translate/stream-translate-result.ts +4 -4
  36. package/src/translate/stream-translate.ts +23 -13
  37. package/src/util/retry-with-exponential-backoff.ts +14 -9
  38. package/dist/default-stop-condition-CVw9jxWu.js.map +0 -1
  39. package/dist/index-D1zb0Eh5.d.ts.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "7.0.186",
3
+ "version": "7.0.187",
4
4
  "type": "module",
5
5
  "description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
6
6
  "license": "Apache-2.0",
@@ -43,31 +43,32 @@
43
43
  }
44
44
  },
45
45
  "dependencies": {
46
- "@ai-sdk/gateway": "4.0.159",
47
- "@ai-sdk/provider": "4.0.39",
48
- "@ai-sdk/provider-utils": "5.0.86"
46
+ "@ai-sdk/gateway": "4.0.160",
47
+ "@ai-sdk/provider": "4.0.40",
48
+ "@ai-sdk/provider-utils": "5.0.87"
49
49
  },
50
50
  "devDependencies": {
51
- "@ai-sdk/amazon-bedrock": "5.0.151",
52
- "@ai-sdk/deepseek": "3.0.92",
53
- "@ai-sdk/google": "4.0.129",
54
- "@ai-sdk/groq": "4.0.89",
55
- "@ai-sdk/huggingface": "2.0.98",
56
- "@ai-sdk/moonshotai": "3.0.98",
57
- "@ai-sdk/openai": "4.0.126",
51
+ "@ai-sdk/amazon-bedrock": "5.0.152",
52
+ "@ai-sdk/deepseek": "3.0.93",
53
+ "@ai-sdk/google": "4.0.130",
54
+ "@ai-sdk/groq": "4.0.90",
55
+ "@ai-sdk/huggingface": "2.0.99",
56
+ "@ai-sdk/moonshotai": "3.0.99",
57
+ "@ai-sdk/openai": "4.0.127",
58
58
  "@ai-sdk/test-server": "2.0.4",
59
- "@ai-sdk/xai": "5.0.53",
59
+ "@ai-sdk/xai": "5.0.54",
60
60
  "@edge-runtime/vm": "^5.0.0",
61
61
  "@smithy/eventstream-codec": "^4.3.3",
62
62
  "@smithy/util-utf8": "^4.3.3",
63
63
  "@types/json-schema": "7.0.15",
64
64
  "@types/node": "22.19.19",
65
+ "@typescript/native": "npm:typescript@7.0.2",
65
66
  "@vercel/ai-tsconfig": "0.0.0",
66
67
  "@vercel/ai-tsdown-config": "0.0.0",
67
68
  "esbuild": "^0.28.2",
68
69
  "tsdown": "^0.23.0",
69
70
  "tsx": "4.23.12",
70
- "typescript": "6.0.3",
71
+ "typescript": "npm:@typescript/typescript6@6.0.2",
71
72
  "zod": "4.6.7"
72
73
  },
73
74
  "peerDependencies": {
@@ -120,6 +121,9 @@
120
121
  "clean": "del-cli docs skills",
121
122
  "type-check": "tsc --noEmit",
122
123
  "test": "pnpm test:node && pnpm test:edge",
124
+ "test:node22": "node ../../tools/with-node.mjs 22 pnpm test",
125
+ "test:node24": "node ../../tools/with-node.mjs 24 pnpm test",
126
+ "test:node26": "node ../../tools/with-node.mjs 26 pnpm test",
123
127
  "test:update": "pnpm test:node -u",
124
128
  "test:watch": "vitest --config vitest.node.config.js",
125
129
  "test:edge": "vitest --config vitest.edge.config.js --run",
@@ -13,15 +13,24 @@ export class NoTranscriptGeneratedError extends AISDKError {
13
13
 
14
14
  readonly responses: Array<TranscriptionModelResponseMetadata>;
15
15
 
16
- constructor(options: {
16
+ constructor({
17
+ cause,
18
+ responses,
19
+ }: {
20
+ /**
21
+ * The provider error reported in the stream before it ended without a
22
+ * transcript, if any.
23
+ */
24
+ cause?: unknown;
17
25
  responses: Array<TranscriptionModelResponseMetadata>;
18
26
  }) {
19
27
  super({
20
28
  name,
21
29
  message: 'No transcript generated.',
30
+ cause,
22
31
  });
23
32
 
24
- this.responses = options.responses;
33
+ this.responses = responses;
25
34
  }
26
35
 
27
36
  static isInstance(error: unknown): error is NoTranscriptGeneratedError {
@@ -13,13 +13,24 @@ export class NoTranslationGeneratedError extends AISDKError {
13
13
 
14
14
  readonly response: SpeechTranslationModelResponseMetadata;
15
15
 
16
- constructor(options: { response: SpeechTranslationModelResponseMetadata }) {
16
+ constructor({
17
+ cause,
18
+ response,
19
+ }: {
20
+ /**
21
+ * The provider error reported in the stream before it ended without a
22
+ * translation, if any.
23
+ */
24
+ cause?: unknown;
25
+ response: SpeechTranslationModelResponseMetadata;
26
+ }) {
17
27
  super({
18
28
  name,
19
29
  message: 'No translation generated.',
30
+ cause,
20
31
  });
21
32
 
22
- this.response = options.response;
33
+ this.response = response;
23
34
  }
24
35
 
25
36
  static isInstance(error: unknown): error is NoTranslationGeneratedError {
@@ -8,6 +8,7 @@ import {
8
8
  import {
9
9
  asArray,
10
10
  createIdGenerator,
11
+ delay,
11
12
  DelayedPromise,
12
13
  filterNullable,
13
14
  isAbortError,
@@ -88,6 +89,7 @@ import { mergeObjects } from '../util/merge-objects';
88
89
  import { notify } from '../util/notify';
89
90
  import { now as originalNow } from '../util/now';
90
91
  import { prepareRetries } from '../util/prepare-retries';
92
+ import { getRetryDelayInMs } from '../util/retry-with-exponential-backoff';
91
93
  import { setAbortTimeout } from '../util/set-abort-timeout';
92
94
  import type { ActiveTools } from './active-tools';
93
95
  import { collectToolApprovals } from './collect-tool-approvals';
@@ -2946,6 +2948,13 @@ class DefaultStreamTextResult<
2946
2948
  initialLanguageModelCall.stream.getReader();
2947
2949
  let automaticStreamRetryCount = 0;
2948
2950
  let callbackStreamRetryCount = 0;
2951
+ const streamRetryAbortController = new AbortController();
2952
+ // the retry wait ends on a caller abort, a steering interrupt, or
2953
+ // when the consumer cancels the stream:
2954
+ const streamRetryAbortSignal = mergeAbortSignals(
2955
+ modelCallAbortSignal,
2956
+ streamRetryAbortController.signal,
2957
+ );
2949
2958
  let bufferedAttemptParts: LanguageModelStreamPart<TOOLS>[] = [];
2950
2959
  const openTextParts = new Set<string>();
2951
2960
  const openReasoningParts = new Set<string>();
@@ -3160,8 +3169,25 @@ class DefaultStreamTextResult<
3160
3169
  ReturnType<typeof callLanguageModel>
3161
3170
  >;
3162
3171
  try {
3172
+ await delay(
3173
+ getRetryDelayInMs({
3174
+ error,
3175
+ responseHeaders: response?.headers,
3176
+ exponentialBackoffDelay:
3177
+ 2000 *
3178
+ 2 **
3179
+ (automaticStreamRetryCount +
3180
+ callbackStreamRetryCount -
3181
+ 1),
3182
+ }),
3183
+ { abortSignal: streamRetryAbortSignal },
3184
+ );
3185
+ streamRetryAbortSignal?.throwIfAborted();
3163
3186
  retryLanguageModelCall = await callLanguageModel();
3164
3187
  } catch (retryError) {
3188
+ if (streamRetryAbortController.signal.aborted) {
3189
+ return;
3190
+ }
3165
3191
  if (!interruptWindowClosed && isInterrupted()) {
3166
3192
  await interruptAttempt();
3167
3193
  return;
@@ -3182,6 +3208,7 @@ class DefaultStreamTextResult<
3182
3208
  }
3183
3209
  },
3184
3210
  cancel(reason) {
3211
+ streamRetryAbortController.abort(reason);
3185
3212
  clearModelOutputTimeouts();
3186
3213
  return languageModelStreamReader.cancel(reason);
3187
3214
  },
@@ -4,6 +4,8 @@ const deprecationCodes = new Map<string, string>([
4
4
  ['streamObject', 'AISDK_DEP_STREAM_OBJECT'],
5
5
  ['experimental_generateSpeech', 'AISDK_DEP_EXPERIMENTAL_GENERATE_SPEECH'],
6
6
  ['experimental_transcribe', 'AISDK_DEP_EXPERIMENTAL_TRANSCRIBE'],
7
+ ['experimental_streamTranscribe', 'AISDK_DEP_EXPERIMENTAL_STREAM_TRANSCRIBE'],
8
+ ['experimental_streamTranslate', 'AISDK_DEP_EXPERIMENTAL_STREAM_TRANSLATE'],
7
9
  ['"image" content part', 'AISDK_DEP_IMAGE_CONTENT_PART'],
8
10
  [
9
11
  '"tool-result" content of type "file-data"',
@@ -111,7 +111,7 @@ export function resolveSpeechTranslationModel(
111
111
  );
112
112
  }
113
113
 
114
- return speechTranslationModel(model);
114
+ return speechTranslationModel.call(provider, model);
115
115
  }
116
116
 
117
117
  if (model.specificationVersion !== 'v4') {
@@ -180,7 +180,7 @@ export function resolveVideoModel(
180
180
  );
181
181
  }
182
182
 
183
- return videoModel(model);
183
+ return videoModel.call(provider, model);
184
184
  }
185
185
 
186
186
  if (!['v4', 'v3'].includes(model.specificationVersion)) {
@@ -207,7 +207,7 @@ export function resolveRerankingModel(model: RerankingModel): RerankingModelV4 {
207
207
  );
208
208
  }
209
209
 
210
- return rerankingModel(model);
210
+ return rerankingModel.call(provider, model);
211
211
  }
212
212
 
213
213
  if (
@@ -1,9 +1,10 @@
1
1
  import type { TranscriptionResult } from './transcribe-result';
2
2
  import { transcribe } from './transcribe';
3
+ import { streamTranscription } from './stream-transcribe';
3
4
  import { logWarnings } from '../logger/log-warnings';
4
5
 
5
6
  export { transcribe } from './transcribe';
6
- export { streamTranscribe as experimental_streamTranscribe } from './stream-transcribe';
7
+ export { streamTranscription } from './stream-transcribe';
7
8
  export type {
8
9
  StreamTranscriptionResult,
9
10
  TranscriptionStreamPart,
@@ -35,6 +36,23 @@ const experimental_transcribe: typeof transcribe = options => {
35
36
  };
36
37
  export { experimental_transcribe };
37
38
 
39
+ /**
40
+ * @deprecated Use `streamTranscription` instead.
41
+ */
42
+ const experimental_streamTranscribe: typeof streamTranscription = options => {
43
+ logWarnings({
44
+ warnings: [
45
+ {
46
+ type: 'deprecated',
47
+ setting: 'experimental_streamTranscribe',
48
+ message: 'Use streamTranscription instead.',
49
+ },
50
+ ],
51
+ });
52
+ return streamTranscription(options);
53
+ };
54
+ export { experimental_streamTranscribe };
55
+
38
56
  /**
39
57
  * @deprecated Use `TranscriptionResult` instead.
40
58
  */
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  UnsupportedFunctionalityError,
3
- type Experimental_TranscriptionModelV4StreamPart,
3
+ type TranscriptionModelV4StreamPart,
4
4
  type JSONObject,
5
5
  type SharedV4AudioFormat,
6
6
  } from '@ai-sdk/provider';
@@ -55,7 +55,7 @@ const originalGenerateCallId = createIdGenerator({
55
55
  *
56
56
  * @returns A result object that contains the streaming transcript and final transcript metadata.
57
57
  */
58
- export function streamTranscribe({
58
+ export function streamTranscription({
59
59
  model,
60
60
  audio,
61
61
  inputAudioFormat,
@@ -215,6 +215,15 @@ export function streamTranscribe({
215
215
  });
216
216
  };
217
217
 
218
+ // The latest provider error part, attached as the cause when the stream ends
219
+ // without a transcript so that result promise consumers can inspect it.
220
+ let providerError: unknown;
221
+ const createNoTranscriptGeneratedError = () =>
222
+ new NoTranscriptGeneratedError({
223
+ cause: providerError,
224
+ responses: [currentResponseMetadata()],
225
+ });
226
+
218
227
  // When the consumer cancels `fullStream` early, we abort the model pipe below
219
228
  // with a defined reason via this controller. Relying on the default cancel
220
229
  // cascade aborts the pipe with an `undefined` reason, which surfaces as a
@@ -224,7 +233,7 @@ export function streamTranscribe({
224
233
  // `Transformer.cancel` is part of the Streams spec (and supported at runtime),
225
234
  // but not yet reflected in the ambient `Transformer` type, so widen it here.
226
235
  const transformer: Transformer<
227
- Experimental_TranscriptionModelV4StreamPart,
236
+ TranscriptionModelV4StreamPart,
228
237
  TranscriptionStreamPart
229
238
  > & { cancel?: (reason?: unknown) => void } = {
230
239
  async transform(value, controller) {
@@ -246,8 +255,13 @@ export function streamTranscribe({
246
255
  case 'transcript-delta':
247
256
  case 'transcript-partial':
248
257
  case 'transcript-final':
249
- case 'raw':
258
+ case 'raw': {
259
+ controller.enqueue(value);
260
+ break;
261
+ }
262
+
250
263
  case 'error': {
264
+ providerError = value.error;
251
265
  controller.enqueue(value);
252
266
  break;
253
267
  }
@@ -258,9 +272,7 @@ export function streamTranscribe({
258
272
  }
259
273
 
260
274
  if (!value.text) {
261
- throw new NoTranscriptGeneratedError({
262
- responses: [currentResponseMetadata()],
263
- });
275
+ throw createNoTranscriptGeneratedError();
264
276
  }
265
277
 
266
278
  textPromise.resolve(value.text);
@@ -307,9 +319,7 @@ export function streamTranscribe({
307
319
 
308
320
  flush() {
309
321
  if (textPromise.isPending()) {
310
- throw new NoTranscriptGeneratedError({
311
- responses: [currentResponseMetadata()],
312
- });
322
+ throw createNoTranscriptGeneratedError();
313
323
  }
314
324
  },
315
325
 
@@ -321,7 +331,7 @@ export function streamTranscribe({
321
331
  };
322
332
 
323
333
  const transform = new TransformStream<
324
- Experimental_TranscriptionModelV4StreamPart,
334
+ TranscriptionModelV4StreamPart,
325
335
  TranscriptionStreamPart
326
336
  >(transformer);
327
337
 
@@ -1,5 +1,43 @@
1
- export { streamTranslate as experimental_streamTranslate } from './stream-translate';
1
+ import type {
2
+ SpeechTranslationStreamPart,
3
+ StreamSpeechTranslationResult,
4
+ } from './stream-translate-result';
5
+ import { streamSpeechTranslation } from './stream-translate';
6
+ import { logWarnings } from '../logger/log-warnings';
7
+
8
+ export { streamSpeechTranslation } from './stream-translate';
2
9
  export type {
3
- StreamTranslationResult as Experimental_StreamTranslationResult,
4
- TranslationStreamPart as Experimental_TranslationStreamPart,
10
+ SpeechTranslationStreamPart,
11
+ StreamSpeechTranslationResult,
5
12
  } from './stream-translate-result';
13
+
14
+ // deprecated exports
15
+
16
+ /**
17
+ * @deprecated Use `streamSpeechTranslation` instead.
18
+ */
19
+ const experimental_streamTranslate: typeof streamSpeechTranslation =
20
+ options => {
21
+ logWarnings({
22
+ warnings: [
23
+ {
24
+ type: 'deprecated',
25
+ setting: 'experimental_streamTranslate',
26
+ message: 'Use streamSpeechTranslation instead.',
27
+ },
28
+ ],
29
+ });
30
+ return streamSpeechTranslation(options);
31
+ };
32
+ export { experimental_streamTranslate };
33
+
34
+ /**
35
+ * @deprecated Use `StreamSpeechTranslationResult` instead.
36
+ */
37
+ export type Experimental_StreamTranslationResult =
38
+ StreamSpeechTranslationResult;
39
+
40
+ /**
41
+ * @deprecated Use `SpeechTranslationStreamPart` instead.
42
+ */
43
+ export type Experimental_TranslationStreamPart = SpeechTranslationStreamPart;
@@ -8,13 +8,13 @@ import type { Warning } from '../types/warning';
8
8
  import type { AsyncIterableStream } from '../util/async-iterable-stream';
9
9
 
10
10
  /**
11
- * Stream parts emitted by `experimental_streamTranslate`.
11
+ * Stream parts emitted by `streamSpeechTranslation`.
12
12
  *
13
13
  * Speech translation model stream parts are passed through unchanged, except
14
14
  * for `stream-start`, `response-metadata`, and `finish`, which are consumed
15
15
  * internally and surfaced via the result promises.
16
16
  */
17
- export type TranslationStreamPart =
17
+ export type SpeechTranslationStreamPart =
18
18
  | {
19
19
  /**
20
20
  * Translated audio chunk in the target language.
@@ -93,7 +93,7 @@ export type TranslationStreamPart =
93
93
  error: unknown;
94
94
  };
95
95
 
96
- export interface StreamTranslationResult {
96
+ export interface StreamSpeechTranslationResult {
97
97
  /**
98
98
  * The final source-language transcript of the input audio.
99
99
  */
@@ -142,5 +142,5 @@ export interface StreamTranslationResult {
142
142
  * results are needed; accessing a result promise first consumes the stream
143
143
  * internally and makes `fullStream` unavailable.
144
144
  */
145
- readonly fullStream: AsyncIterableStream<TranslationStreamPart>;
145
+ readonly fullStream: AsyncIterableStream<SpeechTranslationStreamPart>;
146
146
  }
@@ -19,8 +19,8 @@ import { asAsyncIterableStream } from '../util/async-iterable-stream';
19
19
  import { mergeAbortSignals } from '../util/merge-abort-signals';
20
20
  import { VERSION } from '../version';
21
21
  import type {
22
- StreamTranslationResult,
23
- TranslationStreamPart,
22
+ SpeechTranslationStreamPart,
23
+ StreamSpeechTranslationResult,
24
24
  } from './stream-translate-result';
25
25
 
26
26
  /**
@@ -39,7 +39,7 @@ import type {
39
39
  *
40
40
  * @returns A result object that contains the streaming translation and final translation metadata.
41
41
  */
42
- export function streamTranslate({
42
+ export function streamSpeechTranslation({
43
43
  model,
44
44
  audio,
45
45
  inputAudioFormat,
@@ -112,7 +112,7 @@ export function streamTranslate({
112
112
  _internal?: {
113
113
  currentDate?: () => Date;
114
114
  };
115
- }): StreamTranslationResult {
115
+ }): StreamSpeechTranslationResult {
116
116
  const resolvedModel = resolveSpeechTranslationModel(model);
117
117
 
118
118
  const doStream = resolvedModel.doStream.bind(resolvedModel);
@@ -176,11 +176,20 @@ export function streamTranslate({
176
176
  // `outputText`).
177
177
  let hasAudioOutput = false;
178
178
 
179
+ // The latest provider error part, attached as the cause when the stream ends
180
+ // without a translation so that result promise consumers can inspect it.
181
+ let providerError: unknown;
182
+ const createNoTranslationGeneratedError = () =>
183
+ new NoTranslationGeneratedError({
184
+ cause: providerError,
185
+ response: currentResponseMetadata(),
186
+ });
187
+
179
188
  // `Transformer.cancel` is part of the Streams spec (and supported at runtime),
180
189
  // but not yet reflected in the ambient `Transformer` type, so widen it here.
181
190
  const transformer: Transformer<
182
191
  Experimental_SpeechTranslationModelV4StreamPart,
183
- TranslationStreamPart
192
+ SpeechTranslationStreamPart
184
193
  > & { cancel?: (reason?: unknown) => void } = {
185
194
  transform(value, controller) {
186
195
  switch (value.type) {
@@ -209,8 +218,13 @@ export function streamTranslate({
209
218
  case 'source-transcript-delta':
210
219
  case 'source-transcript-partial':
211
220
  case 'source-transcript-final':
212
- case 'raw':
221
+ case 'raw': {
222
+ controller.enqueue(value);
223
+ break;
224
+ }
225
+
213
226
  case 'error': {
227
+ providerError = value.error;
214
228
  controller.enqueue(value);
215
229
  break;
216
230
  }
@@ -221,9 +235,7 @@ export function streamTranslate({
221
235
  }
222
236
 
223
237
  if (!hasAudioOutput && !value.outputText) {
224
- throw new NoTranslationGeneratedError({
225
- response: currentResponseMetadata(),
226
- });
238
+ throw createNoTranslationGeneratedError();
227
239
  }
228
240
 
229
241
  sourceTextPromise.resolve(value.sourceText);
@@ -244,9 +256,7 @@ export function streamTranslate({
244
256
 
245
257
  flush() {
246
258
  if (translationTextPromise.isPending()) {
247
- throw new NoTranslationGeneratedError({
248
- response: currentResponseMetadata(),
249
- });
259
+ throw createNoTranslationGeneratedError();
250
260
  }
251
261
  },
252
262
 
@@ -259,7 +269,7 @@ export function streamTranslate({
259
269
 
260
270
  const transform = new TransformStream<
261
271
  Experimental_SpeechTranslationModelV4StreamPart,
262
- TranslationStreamPart
272
+ SpeechTranslationStreamPart
263
273
  >(transformer);
264
274
 
265
275
  // Piping (instead of an eager read loop) preserves consumer backpressure
@@ -7,25 +7,30 @@ import {
7
7
  } from '@ai-sdk/provider-utils';
8
8
  import { RetryError } from './retry-error';
9
9
 
10
- function getRetryDelayInMs({
10
+ export function getRetryDelayInMs({
11
11
  error,
12
+ responseHeaders,
12
13
  exponentialBackoffDelay,
13
14
  }: {
14
- error: APICallError | GatewayError;
15
+ error: unknown;
16
+ responseHeaders?: Record<string, string>;
15
17
  exponentialBackoffDelay: number;
16
18
  }): number {
17
19
  const headers = APICallError.isInstance(error)
18
- ? error.responseHeaders
19
- : APICallError.isInstance(error.cause)
20
- ? (error.cause as APICallError).responseHeaders
21
- : undefined;
20
+ ? (error.responseHeaders ?? responseHeaders)
21
+ : GatewayError.isInstance(error) && APICallError.isInstance(error.cause)
22
+ ? (error.cause.responseHeaders ?? responseHeaders)
23
+ : responseHeaders;
22
24
 
23
25
  if (!headers) return exponentialBackoffDelay;
24
26
 
27
+ const normalizedHeaders = Object.fromEntries(
28
+ Object.entries(headers).map(([key, value]) => [key.toLowerCase(), value]),
29
+ );
25
30
  let ms: number | undefined;
26
31
 
27
32
  // retry-ms is more precise than retry-after and used by e.g. OpenAI
28
- const retryAfterMs = headers['retry-after-ms'];
33
+ const retryAfterMs = normalizedHeaders['retry-after-ms'];
29
34
  if (retryAfterMs) {
30
35
  const timeoutMs = parseFloat(retryAfterMs);
31
36
  if (!Number.isNaN(timeoutMs)) {
@@ -34,7 +39,7 @@ function getRetryDelayInMs({
34
39
  }
35
40
 
36
41
  // About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
37
- const retryAfter = headers['retry-after'];
42
+ const retryAfter = normalizedHeaders['retry-after'];
38
43
  if (retryAfter && ms === undefined) {
39
44
  const timeoutSeconds = parseFloat(retryAfter);
40
45
  if (!Number.isNaN(timeoutSeconds)) {
@@ -88,7 +93,7 @@ export const retryWithExponentialBackoffRespectingRetryHeaders = ({
88
93
  (await additionalRetryableError(error))),
89
94
  getDelayInMs: ({ error, exponentialBackoffDelay }) =>
90
95
  getRetryDelayInMs({
91
- error: error as APICallError | GatewayError,
96
+ error,
92
97
  exponentialBackoffDelay,
93
98
  }),
94
99
  createRetryError: ({ message, reason, errors }) =>