@ai-sdk/openai 3.0.106 → 3.0.107

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.
@@ -68,6 +68,22 @@ function getGptVersion(modelId) {
68
68
 
69
69
  // src/openai-stream-error.ts
70
70
  import { APICallError } from "@ai-sdk/provider";
71
+ function createOpenAIProviderStreamError(frame) {
72
+ var _a2, _b;
73
+ const streamError = parseStreamError(frame);
74
+ if (streamError == null) {
75
+ return void 0;
76
+ }
77
+ const statusCode = getStatusCode(streamError);
78
+ return {
79
+ message: streamError.message,
80
+ type: (_a2 = streamError.type) != null ? _a2 : void 0,
81
+ code: (_b = streamError.code) != null ? _b : void 0,
82
+ statusCode,
83
+ isRetryable: isRetryableStreamError(streamError, statusCode),
84
+ data: frame
85
+ };
86
+ }
71
87
  async function throwIfOpenAIStreamErrorBeforeOutput({
72
88
  stream,
73
89
  getError,
@@ -139,16 +155,14 @@ function parseStreamError(frame) {
139
155
  return typeof (responseError == null ? void 0 : responseError.message) === "string" ? {
140
156
  message: responseError.message,
141
157
  code: getStringOrNumber(responseError.code),
142
- type: "response.failed",
143
- frame
158
+ type: "response.failed"
144
159
  } : void 0;
145
160
  }
146
161
  const error = (_a2 = asRecord(value.error)) != null ? _a2 : value;
147
162
  return typeof error.message === "string" && (asRecord(value.error) != null || typeof error.type === "string" || "code" in error || "param" in error) ? {
148
163
  message: error.message,
149
164
  code: getStringOrNumber(error.code),
150
- type: typeof error.type === "string" ? error.type : void 0,
151
- frame
165
+ type: typeof error.type === "string" ? error.type : void 0
152
166
  } : void 0;
153
167
  }
154
168
  function getStatusCode(error) {
@@ -188,6 +202,15 @@ function getStringOrNumber(value) {
188
202
  function isHttpErrorStatusCode(value) {
189
203
  return Number.isInteger(value) && value >= 400 && value <= 599;
190
204
  }
205
+ function isRetryableStatusCode(statusCode) {
206
+ return statusCode === 408 || statusCode === 409 || statusCode === 429 || statusCode >= 500;
207
+ }
208
+ function isRetryableStreamError(error, statusCode) {
209
+ if (error.code === "insufficient_quota" || error.type === "insufficient_quota") {
210
+ return false;
211
+ }
212
+ return isRetryableStatusCode(statusCode);
213
+ }
191
214
 
192
215
  // src/chat/convert-openai-chat-usage.ts
193
216
  function convertOpenAIChatUsage(usage) {
@@ -4000,6 +4023,31 @@ var jsonValueSchema = z18.lazy(
4000
4023
  z18.record(z18.string(), jsonValueSchema.optional())
4001
4024
  ])
4002
4025
  );
4026
+ var jsonObjectSchema = z18.record(z18.string(), jsonValueSchema.optional());
4027
+ var openaiResponsesUsageSchema = z18.intersection(
4028
+ jsonObjectSchema,
4029
+ z18.object({
4030
+ input_tokens: z18.number(),
4031
+ input_tokens_details: z18.intersection(
4032
+ jsonObjectSchema,
4033
+ z18.object({
4034
+ cached_tokens: z18.number().nullish(),
4035
+ cache_write_tokens: z18.number().nullish(),
4036
+ orchestration_input_tokens: z18.number().nullish(),
4037
+ orchestration_input_cached_tokens: z18.number().nullish()
4038
+ })
4039
+ ).nullish(),
4040
+ output_tokens: z18.number(),
4041
+ output_tokens_details: z18.intersection(
4042
+ jsonObjectSchema,
4043
+ z18.object({
4044
+ reasoning_tokens: z18.number().nullish(),
4045
+ orchestration_output_tokens: z18.number().nullish()
4046
+ })
4047
+ ).nullish(),
4048
+ total_tokens: z18.number().optional()
4049
+ })
4050
+ );
4003
4051
  function isRecord(value) {
4004
4052
  return value != null && typeof value === "object" && !Array.isArray(value);
4005
4053
  }
@@ -4111,20 +4159,7 @@ var openaiResponsesChunkSchema = lazySchema16(
4111
4159
  type: z18.enum(["response.completed", "response.incomplete"]),
4112
4160
  response: z18.object({
4113
4161
  incomplete_details: z18.object({ reason: z18.string() }).nullish(),
4114
- usage: z18.object({
4115
- input_tokens: z18.number(),
4116
- input_tokens_details: z18.object({
4117
- cached_tokens: z18.number().nullish(),
4118
- cache_write_tokens: z18.number().nullish(),
4119
- orchestration_input_tokens: z18.number().nullish(),
4120
- orchestration_input_cached_tokens: z18.number().nullish()
4121
- }).nullish(),
4122
- output_tokens: z18.number(),
4123
- output_tokens_details: z18.object({
4124
- reasoning_tokens: z18.number().nullish(),
4125
- orchestration_output_tokens: z18.number().nullish()
4126
- }).nullish()
4127
- }),
4162
+ usage: openaiResponsesUsageSchema.nullish(),
4128
4163
  reasoning: z18.object({
4129
4164
  context: z18.string().nullish()
4130
4165
  }).nullish(),
@@ -4140,20 +4175,7 @@ var openaiResponsesChunkSchema = lazySchema16(
4140
4175
  message: z18.string()
4141
4176
  }).nullish(),
4142
4177
  incomplete_details: z18.object({ reason: z18.string() }).nullish(),
4143
- usage: z18.object({
4144
- input_tokens: z18.number(),
4145
- input_tokens_details: z18.object({
4146
- cached_tokens: z18.number().nullish(),
4147
- cache_write_tokens: z18.number().nullish(),
4148
- orchestration_input_tokens: z18.number().nullish(),
4149
- orchestration_input_cached_tokens: z18.number().nullish()
4150
- }).nullish(),
4151
- output_tokens: z18.number(),
4152
- output_tokens_details: z18.object({
4153
- reasoning_tokens: z18.number().nullish(),
4154
- orchestration_output_tokens: z18.number().nullish()
4155
- }).nullish()
4156
- }).nullish(),
4178
+ usage: openaiResponsesUsageSchema.nullish(),
4157
4179
  reasoning: z18.object({
4158
4180
  context: z18.string().nullish()
4159
4181
  }).nullish(),
@@ -4919,20 +4941,7 @@ var openaiResponsesResponseSchema = lazySchema16(
4919
4941
  context: z18.string().nullish()
4920
4942
  }).nullish(),
4921
4943
  incomplete_details: z18.object({ reason: z18.string() }).nullish(),
4922
- usage: z18.object({
4923
- input_tokens: z18.number(),
4924
- input_tokens_details: z18.object({
4925
- cached_tokens: z18.number().nullish(),
4926
- cache_write_tokens: z18.number().nullish(),
4927
- orchestration_input_tokens: z18.number().nullish(),
4928
- orchestration_input_cached_tokens: z18.number().nullish()
4929
- }).nullish(),
4930
- output_tokens: z18.number(),
4931
- output_tokens_details: z18.object({
4932
- reasoning_tokens: z18.number().nullish(),
4933
- orchestration_output_tokens: z18.number().nullish()
4934
- }).nullish()
4935
- }).optional()
4944
+ usage: openaiResponsesUsageSchema.nullish()
4936
4945
  })
4937
4946
  )
4938
4947
  );
@@ -6876,7 +6885,7 @@ var OpenAIResponsesLanguageModel = class {
6876
6885
  controller.enqueue({ type: "stream-start", warnings });
6877
6886
  },
6878
6887
  transform(chunk, controller) {
6879
- 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;
6888
+ 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;
6880
6889
  if (options.includeRawChunks) {
6881
6890
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
6882
6891
  }
@@ -7697,15 +7706,15 @@ var OpenAIResponsesLanguageModel = class {
7697
7706
  raw: (_z = (_y = value.response.incomplete_details) == null ? void 0 : _y.reason) != null ? _z : void 0
7698
7707
  };
7699
7708
  }
7700
- usage = value.response.usage;
7709
+ usage = (_A = value.response.usage) != null ? _A : void 0;
7701
7710
  if (typeof value.response.service_tier === "string") {
7702
7711
  serviceTier = value.response.service_tier;
7703
7712
  }
7704
- if (((_A = value.response.reasoning) == null ? void 0 : _A.context) != null) {
7713
+ if (((_B = value.response.reasoning) == null ? void 0 : _B.context) != null) {
7705
7714
  reasoningContext = value.response.reasoning.context;
7706
7715
  }
7707
7716
  } else if (isResponseFailedChunk(value)) {
7708
- const incompleteReason = (_B = value.response.incomplete_details) == null ? void 0 : _B.reason;
7717
+ const incompleteReason = (_C = value.response.incomplete_details) == null ? void 0 : _C.reason;
7709
7718
  finishReason = {
7710
7719
  unified: incompleteReason ? mapOpenAIResponseFinishReason({
7711
7720
  finishReason: incompleteReason,
@@ -7713,23 +7722,24 @@ var OpenAIResponsesLanguageModel = class {
7713
7722
  }) : "error",
7714
7723
  raw: incompleteReason != null ? incompleteReason : "error"
7715
7724
  };
7716
- usage = (_C = value.response.usage) != null ? _C : void 0;
7717
- if (((_D = value.response.reasoning) == null ? void 0 : _D.context) != null) {
7725
+ usage = (_D = value.response.usage) != null ? _D : void 0;
7726
+ if (((_E = value.response.reasoning) == null ? void 0 : _E.context) != null) {
7718
7727
  reasoningContext = value.response.reasoning.context;
7719
7728
  }
7720
7729
  if (!encounteredStreamError && value.response.error != null) {
7721
7730
  encounteredStreamError = true;
7731
+ const error = {
7732
+ type: "response.failed",
7733
+ sequence_number: value.sequence_number,
7734
+ response: {
7735
+ error: value.response.error,
7736
+ incomplete_details: value.response.incomplete_details,
7737
+ service_tier: value.response.service_tier
7738
+ }
7739
+ };
7722
7740
  controller.enqueue({
7723
7741
  type: "error",
7724
- error: {
7725
- type: "response.failed",
7726
- sequence_number: value.sequence_number,
7727
- response: {
7728
- error: value.response.error,
7729
- incomplete_details: value.response.incomplete_details,
7730
- service_tier: value.response.service_tier
7731
- }
7732
- }
7742
+ error: (_F = createOpenAIProviderStreamError(error)) != null ? _F : error
7733
7743
  });
7734
7744
  }
7735
7745
  } else if (isResponseAnnotationAddedChunk(value)) {
@@ -7738,7 +7748,7 @@ var OpenAIResponsesLanguageModel = class {
7738
7748
  controller.enqueue({
7739
7749
  type: "source",
7740
7750
  sourceType: "url",
7741
- id: (_G = (_F = (_E = self.config).generateId) == null ? void 0 : _F.call(_E)) != null ? _G : generateId2(),
7751
+ id: (_I = (_H = (_G = self.config).generateId) == null ? void 0 : _H.call(_G)) != null ? _I : generateId2(),
7742
7752
  url: value.annotation.url,
7743
7753
  title: value.annotation.title
7744
7754
  });
@@ -7746,7 +7756,7 @@ var OpenAIResponsesLanguageModel = class {
7746
7756
  controller.enqueue({
7747
7757
  type: "source",
7748
7758
  sourceType: "document",
7749
- id: (_J = (_I = (_H = self.config).generateId) == null ? void 0 : _I.call(_H)) != null ? _J : generateId2(),
7759
+ id: (_L = (_K = (_J = self.config).generateId) == null ? void 0 : _K.call(_J)) != null ? _L : generateId2(),
7750
7760
  mediaType: "text/plain",
7751
7761
  title: value.annotation.filename,
7752
7762
  filename: value.annotation.filename,
@@ -7762,7 +7772,7 @@ var OpenAIResponsesLanguageModel = class {
7762
7772
  controller.enqueue({
7763
7773
  type: "source",
7764
7774
  sourceType: "document",
7765
- id: (_M = (_L = (_K = self.config).generateId) == null ? void 0 : _L.call(_K)) != null ? _M : generateId2(),
7775
+ id: (_O = (_N = (_M = self.config).generateId) == null ? void 0 : _N.call(_M)) != null ? _O : generateId2(),
7766
7776
  mediaType: "text/plain",
7767
7777
  title: value.annotation.filename,
7768
7778
  filename: value.annotation.filename,
@@ -7778,7 +7788,7 @@ var OpenAIResponsesLanguageModel = class {
7778
7788
  controller.enqueue({
7779
7789
  type: "source",
7780
7790
  sourceType: "document",
7781
- id: (_P = (_O = (_N = self.config).generateId) == null ? void 0 : _O.call(_N)) != null ? _P : generateId2(),
7791
+ id: (_R = (_Q = (_P = self.config).generateId) == null ? void 0 : _Q.call(_P)) != null ? _R : generateId2(),
7782
7792
  mediaType: "application/octet-stream",
7783
7793
  title: value.annotation.file_id,
7784
7794
  filename: value.annotation.file_id,
@@ -7794,7 +7804,10 @@ var OpenAIResponsesLanguageModel = class {
7794
7804
  } else if (isErrorChunk(value)) {
7795
7805
  encounteredStreamError = true;
7796
7806
  finishReason = { unified: "error", raw: "error" };
7797
- controller.enqueue({ type: "error", error: value });
7807
+ controller.enqueue({
7808
+ type: "error",
7809
+ error: (_S = createOpenAIProviderStreamError(value)) != null ? _S : value
7810
+ });
7798
7811
  }
7799
7812
  },
7800
7813
  flush(controller) {