@ai-sdk/openai 3.0.106 → 3.0.108

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/dist/index.mjs CHANGED
@@ -76,6 +76,22 @@ function getGptVersion(modelId) {
76
76
 
77
77
  // src/openai-stream-error.ts
78
78
  import { APICallError } from "@ai-sdk/provider";
79
+ function createOpenAIProviderStreamError(frame) {
80
+ var _a2, _b;
81
+ const streamError = parseStreamError(frame);
82
+ if (streamError == null) {
83
+ return void 0;
84
+ }
85
+ const statusCode = getStatusCode(streamError);
86
+ return {
87
+ message: streamError.message,
88
+ type: (_a2 = streamError.type) != null ? _a2 : void 0,
89
+ code: (_b = streamError.code) != null ? _b : void 0,
90
+ statusCode,
91
+ isRetryable: isRetryableStreamError(streamError, statusCode),
92
+ data: frame
93
+ };
94
+ }
79
95
  async function throwIfOpenAIStreamErrorBeforeOutput({
80
96
  stream,
81
97
  getError,
@@ -147,16 +163,14 @@ function parseStreamError(frame) {
147
163
  return typeof (responseError == null ? void 0 : responseError.message) === "string" ? {
148
164
  message: responseError.message,
149
165
  code: getStringOrNumber(responseError.code),
150
- type: "response.failed",
151
- frame
166
+ type: "response.failed"
152
167
  } : void 0;
153
168
  }
154
169
  const error = (_a2 = asRecord(value.error)) != null ? _a2 : value;
155
170
  return typeof error.message === "string" && (asRecord(value.error) != null || typeof error.type === "string" || "code" in error || "param" in error) ? {
156
171
  message: error.message,
157
172
  code: getStringOrNumber(error.code),
158
- type: typeof error.type === "string" ? error.type : void 0,
159
- frame
173
+ type: typeof error.type === "string" ? error.type : void 0
160
174
  } : void 0;
161
175
  }
162
176
  function getStatusCode(error) {
@@ -196,6 +210,15 @@ function getStringOrNumber(value) {
196
210
  function isHttpErrorStatusCode(value) {
197
211
  return Number.isInteger(value) && value >= 400 && value <= 599;
198
212
  }
213
+ function isRetryableStatusCode(statusCode) {
214
+ return statusCode === 408 || statusCode === 409 || statusCode === 429 || statusCode >= 500;
215
+ }
216
+ function isRetryableStreamError(error, statusCode) {
217
+ if (error.code === "insufficient_quota" || error.type === "insufficient_quota") {
218
+ return false;
219
+ }
220
+ return isRetryableStatusCode(statusCode);
221
+ }
199
222
 
200
223
  // src/chat/convert-openai-chat-usage.ts
201
224
  function convertOpenAIChatUsage(usage) {
@@ -4109,6 +4132,31 @@ var jsonValueSchema2 = z22.lazy(
4109
4132
  z22.record(z22.string(), jsonValueSchema2.optional())
4110
4133
  ])
4111
4134
  );
4135
+ var jsonObjectSchema = z22.record(z22.string(), jsonValueSchema2.optional());
4136
+ var openaiResponsesUsageSchema = z22.intersection(
4137
+ jsonObjectSchema,
4138
+ z22.object({
4139
+ input_tokens: z22.number(),
4140
+ input_tokens_details: z22.intersection(
4141
+ jsonObjectSchema,
4142
+ z22.object({
4143
+ cached_tokens: z22.number().nullish(),
4144
+ cache_write_tokens: z22.number().nullish(),
4145
+ orchestration_input_tokens: z22.number().nullish(),
4146
+ orchestration_input_cached_tokens: z22.number().nullish()
4147
+ })
4148
+ ).nullish(),
4149
+ output_tokens: z22.number(),
4150
+ output_tokens_details: z22.intersection(
4151
+ jsonObjectSchema,
4152
+ z22.object({
4153
+ reasoning_tokens: z22.number().nullish(),
4154
+ orchestration_output_tokens: z22.number().nullish()
4155
+ })
4156
+ ).nullish(),
4157
+ total_tokens: z22.number().optional()
4158
+ })
4159
+ );
4112
4160
  function isRecord(value) {
4113
4161
  return value != null && typeof value === "object" && !Array.isArray(value);
4114
4162
  }
@@ -4220,20 +4268,7 @@ var openaiResponsesChunkSchema = lazySchema20(
4220
4268
  type: z22.enum(["response.completed", "response.incomplete"]),
4221
4269
  response: z22.object({
4222
4270
  incomplete_details: z22.object({ reason: z22.string() }).nullish(),
4223
- usage: z22.object({
4224
- input_tokens: z22.number(),
4225
- input_tokens_details: z22.object({
4226
- cached_tokens: z22.number().nullish(),
4227
- cache_write_tokens: z22.number().nullish(),
4228
- orchestration_input_tokens: z22.number().nullish(),
4229
- orchestration_input_cached_tokens: z22.number().nullish()
4230
- }).nullish(),
4231
- output_tokens: z22.number(),
4232
- output_tokens_details: z22.object({
4233
- reasoning_tokens: z22.number().nullish(),
4234
- orchestration_output_tokens: z22.number().nullish()
4235
- }).nullish()
4236
- }),
4271
+ usage: openaiResponsesUsageSchema.nullish(),
4237
4272
  reasoning: z22.object({
4238
4273
  context: z22.string().nullish()
4239
4274
  }).nullish(),
@@ -4249,20 +4284,7 @@ var openaiResponsesChunkSchema = lazySchema20(
4249
4284
  message: z22.string()
4250
4285
  }).nullish(),
4251
4286
  incomplete_details: z22.object({ reason: z22.string() }).nullish(),
4252
- usage: z22.object({
4253
- input_tokens: z22.number(),
4254
- input_tokens_details: z22.object({
4255
- cached_tokens: z22.number().nullish(),
4256
- cache_write_tokens: z22.number().nullish(),
4257
- orchestration_input_tokens: z22.number().nullish(),
4258
- orchestration_input_cached_tokens: z22.number().nullish()
4259
- }).nullish(),
4260
- output_tokens: z22.number(),
4261
- output_tokens_details: z22.object({
4262
- reasoning_tokens: z22.number().nullish(),
4263
- orchestration_output_tokens: z22.number().nullish()
4264
- }).nullish()
4265
- }).nullish(),
4287
+ usage: openaiResponsesUsageSchema.nullish(),
4266
4288
  reasoning: z22.object({
4267
4289
  context: z22.string().nullish()
4268
4290
  }).nullish(),
@@ -5028,20 +5050,7 @@ var openaiResponsesResponseSchema = lazySchema20(
5028
5050
  context: z22.string().nullish()
5029
5051
  }).nullish(),
5030
5052
  incomplete_details: z22.object({ reason: z22.string() }).nullish(),
5031
- usage: z22.object({
5032
- input_tokens: z22.number(),
5033
- input_tokens_details: z22.object({
5034
- cached_tokens: z22.number().nullish(),
5035
- cache_write_tokens: z22.number().nullish(),
5036
- orchestration_input_tokens: z22.number().nullish(),
5037
- orchestration_input_cached_tokens: z22.number().nullish()
5038
- }).nullish(),
5039
- output_tokens: z22.number(),
5040
- output_tokens_details: z22.object({
5041
- reasoning_tokens: z22.number().nullish(),
5042
- orchestration_output_tokens: z22.number().nullish()
5043
- }).nullish()
5044
- }).optional()
5053
+ usage: openaiResponsesUsageSchema.nullish()
5045
5054
  })
5046
5055
  )
5047
5056
  );
@@ -5095,7 +5104,8 @@ var openaiResponsesReasoningModelIds = [
5095
5104
  "gpt-5.6",
5096
5105
  "gpt-5.6-luna",
5097
5106
  "gpt-5.6-sol",
5098
- "gpt-5.6-terra"
5107
+ "gpt-5.6-terra",
5108
+ "gpt-6-astra"
5099
5109
  ];
5100
5110
  var openaiResponsesModelIds = [
5101
5111
  "gpt-4.1",
@@ -6633,7 +6643,7 @@ var OpenAIResponsesLanguageModel = class {
6633
6643
  controller.enqueue({ type: "stream-start", warnings });
6634
6644
  },
6635
6645
  transform(chunk, controller) {
6636
- var _a3, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P;
6646
+ var _a3, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S;
6637
6647
  if (options.includeRawChunks) {
6638
6648
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
6639
6649
  }
@@ -7454,15 +7464,15 @@ var OpenAIResponsesLanguageModel = class {
7454
7464
  raw: (_z = (_y = value.response.incomplete_details) == null ? void 0 : _y.reason) != null ? _z : void 0
7455
7465
  };
7456
7466
  }
7457
- usage = value.response.usage;
7467
+ usage = (_A = value.response.usage) != null ? _A : void 0;
7458
7468
  if (typeof value.response.service_tier === "string") {
7459
7469
  serviceTier = value.response.service_tier;
7460
7470
  }
7461
- if (((_A = value.response.reasoning) == null ? void 0 : _A.context) != null) {
7471
+ if (((_B = value.response.reasoning) == null ? void 0 : _B.context) != null) {
7462
7472
  reasoningContext = value.response.reasoning.context;
7463
7473
  }
7464
7474
  } else if (isResponseFailedChunk(value)) {
7465
- const incompleteReason = (_B = value.response.incomplete_details) == null ? void 0 : _B.reason;
7475
+ const incompleteReason = (_C = value.response.incomplete_details) == null ? void 0 : _C.reason;
7466
7476
  finishReason = {
7467
7477
  unified: incompleteReason ? mapOpenAIResponseFinishReason({
7468
7478
  finishReason: incompleteReason,
@@ -7470,23 +7480,24 @@ var OpenAIResponsesLanguageModel = class {
7470
7480
  }) : "error",
7471
7481
  raw: incompleteReason != null ? incompleteReason : "error"
7472
7482
  };
7473
- usage = (_C = value.response.usage) != null ? _C : void 0;
7474
- if (((_D = value.response.reasoning) == null ? void 0 : _D.context) != null) {
7483
+ usage = (_D = value.response.usage) != null ? _D : void 0;
7484
+ if (((_E = value.response.reasoning) == null ? void 0 : _E.context) != null) {
7475
7485
  reasoningContext = value.response.reasoning.context;
7476
7486
  }
7477
7487
  if (!encounteredStreamError && value.response.error != null) {
7478
7488
  encounteredStreamError = true;
7489
+ const error = {
7490
+ type: "response.failed",
7491
+ sequence_number: value.sequence_number,
7492
+ response: {
7493
+ error: value.response.error,
7494
+ incomplete_details: value.response.incomplete_details,
7495
+ service_tier: value.response.service_tier
7496
+ }
7497
+ };
7479
7498
  controller.enqueue({
7480
7499
  type: "error",
7481
- error: {
7482
- type: "response.failed",
7483
- sequence_number: value.sequence_number,
7484
- response: {
7485
- error: value.response.error,
7486
- incomplete_details: value.response.incomplete_details,
7487
- service_tier: value.response.service_tier
7488
- }
7489
- }
7500
+ error: (_F = createOpenAIProviderStreamError(error)) != null ? _F : error
7490
7501
  });
7491
7502
  }
7492
7503
  } else if (isResponseAnnotationAddedChunk(value)) {
@@ -7495,7 +7506,7 @@ var OpenAIResponsesLanguageModel = class {
7495
7506
  controller.enqueue({
7496
7507
  type: "source",
7497
7508
  sourceType: "url",
7498
- id: (_G = (_F = (_E = self.config).generateId) == null ? void 0 : _F.call(_E)) != null ? _G : generateId2(),
7509
+ id: (_I = (_H = (_G = self.config).generateId) == null ? void 0 : _H.call(_G)) != null ? _I : generateId2(),
7499
7510
  url: value.annotation.url,
7500
7511
  title: value.annotation.title
7501
7512
  });
@@ -7503,7 +7514,7 @@ var OpenAIResponsesLanguageModel = class {
7503
7514
  controller.enqueue({
7504
7515
  type: "source",
7505
7516
  sourceType: "document",
7506
- id: (_J = (_I = (_H = self.config).generateId) == null ? void 0 : _I.call(_H)) != null ? _J : generateId2(),
7517
+ id: (_L = (_K = (_J = self.config).generateId) == null ? void 0 : _K.call(_J)) != null ? _L : generateId2(),
7507
7518
  mediaType: "text/plain",
7508
7519
  title: value.annotation.filename,
7509
7520
  filename: value.annotation.filename,
@@ -7519,7 +7530,7 @@ var OpenAIResponsesLanguageModel = class {
7519
7530
  controller.enqueue({
7520
7531
  type: "source",
7521
7532
  sourceType: "document",
7522
- id: (_M = (_L = (_K = self.config).generateId) == null ? void 0 : _L.call(_K)) != null ? _M : generateId2(),
7533
+ id: (_O = (_N = (_M = self.config).generateId) == null ? void 0 : _N.call(_M)) != null ? _O : generateId2(),
7523
7534
  mediaType: "text/plain",
7524
7535
  title: value.annotation.filename,
7525
7536
  filename: value.annotation.filename,
@@ -7535,7 +7546,7 @@ var OpenAIResponsesLanguageModel = class {
7535
7546
  controller.enqueue({
7536
7547
  type: "source",
7537
7548
  sourceType: "document",
7538
- id: (_P = (_O = (_N = self.config).generateId) == null ? void 0 : _O.call(_N)) != null ? _P : generateId2(),
7549
+ id: (_R = (_Q = (_P = self.config).generateId) == null ? void 0 : _Q.call(_P)) != null ? _R : generateId2(),
7539
7550
  mediaType: "application/octet-stream",
7540
7551
  title: value.annotation.file_id,
7541
7552
  filename: value.annotation.file_id,
@@ -7551,7 +7562,10 @@ var OpenAIResponsesLanguageModel = class {
7551
7562
  } else if (isErrorChunk(value)) {
7552
7563
  encounteredStreamError = true;
7553
7564
  finishReason = { unified: "error", raw: "error" };
7554
- controller.enqueue({ type: "error", error: value });
7565
+ controller.enqueue({
7566
+ type: "error",
7567
+ error: (_S = createOpenAIProviderStreamError(value)) != null ? _S : value
7568
+ });
7555
7569
  }
7556
7570
  },
7557
7571
  flush(controller) {
@@ -8072,7 +8086,7 @@ var OpenAITranscriptionModel = class {
8072
8086
  };
8073
8087
 
8074
8088
  // src/version.ts
8075
- var VERSION = true ? "3.0.106" : "0.0.0-test";
8089
+ var VERSION = true ? "3.0.108" : "0.0.0-test";
8076
8090
 
8077
8091
  // src/openai-provider.ts
8078
8092
  function createOpenAI(options = {}) {