@ai-sdk/openai 4.0.55 → 4.0.57

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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @ai-sdk/openai
2
2
 
3
+ ## 4.0.57
4
+
5
+ ### Patch Changes
6
+
7
+ - 048ce06: feat(batch): surface the uploaded input file on the batch start result (`providerMetadata.<provider>.inputFileId` / `inputFileExpiresAt`) and accept an `inputFileExpiresAfter` provider option on the OpenAI and xAI batch input file upload
8
+ - 7243530: fix(openai): preserve complete Responses API raw usage objects
9
+
10
+ ## 4.0.56
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies [6bcc0f8]
15
+ - @ai-sdk/provider-utils@5.0.36
16
+
3
17
  ## 4.0.55
4
18
 
5
19
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -255,20 +255,21 @@ declare const openaiResponsesChunkSchema: _ai_sdk_provider_utils.LazySchema<{
255
255
  incomplete_details?: {
256
256
  reason: string;
257
257
  } | null | undefined;
258
- usage?: {
258
+ usage?: (Record<string, JSONValue | undefined> & {
259
259
  input_tokens: number;
260
260
  output_tokens: number;
261
- input_tokens_details?: {
261
+ input_tokens_details?: (Record<string, JSONValue | undefined> & {
262
262
  cached_tokens?: number | null | undefined;
263
263
  cache_write_tokens?: number | null | undefined;
264
264
  orchestration_input_tokens?: number | null | undefined;
265
265
  orchestration_input_cached_tokens?: number | null | undefined;
266
- } | null | undefined;
267
- output_tokens_details?: {
266
+ }) | null | undefined;
267
+ output_tokens_details?: (Record<string, JSONValue | undefined> & {
268
268
  reasoning_tokens?: number | null | undefined;
269
269
  orchestration_output_tokens?: number | null | undefined;
270
- } | null | undefined;
271
- } | null | undefined;
270
+ }) | null | undefined;
271
+ total_tokens?: number | undefined;
272
+ }) | null | undefined;
272
273
  reasoning?: {
273
274
  context?: string | null | undefined;
274
275
  } | null | undefined;
@@ -285,20 +286,21 @@ declare const openaiResponsesChunkSchema: _ai_sdk_provider_utils.LazySchema<{
285
286
  incomplete_details?: {
286
287
  reason: string;
287
288
  } | null | undefined;
288
- usage?: {
289
+ usage?: (Record<string, JSONValue | undefined> & {
289
290
  input_tokens: number;
290
291
  output_tokens: number;
291
- input_tokens_details?: {
292
+ input_tokens_details?: (Record<string, JSONValue | undefined> & {
292
293
  cached_tokens?: number | null | undefined;
293
294
  cache_write_tokens?: number | null | undefined;
294
295
  orchestration_input_tokens?: number | null | undefined;
295
296
  orchestration_input_cached_tokens?: number | null | undefined;
296
- } | null | undefined;
297
- output_tokens_details?: {
297
+ }) | null | undefined;
298
+ output_tokens_details?: (Record<string, JSONValue | undefined> & {
298
299
  reasoning_tokens?: number | null | undefined;
299
300
  orchestration_output_tokens?: number | null | undefined;
300
- } | null | undefined;
301
- } | null | undefined;
301
+ }) | null | undefined;
302
+ total_tokens?: number | undefined;
303
+ }) | null | undefined;
302
304
  reasoning?: {
303
305
  context?: string | null | undefined;
304
306
  } | null | undefined;
package/dist/index.js CHANGED
@@ -3529,6 +3529,7 @@ import {
3529
3529
  getFromApi as getFromApi2,
3530
3530
  lazySchema as lazySchema26,
3531
3531
  normalizeBatchRequestCounts,
3532
+ parseProviderOptions as parseProviderOptions8,
3532
3533
  postJsonToApi as postJsonToApi6,
3533
3534
  postToApi,
3534
3535
  safeValidateTypes,
@@ -3623,6 +3624,31 @@ var jsonValueSchema2 = z25.lazy(
3623
3624
  z25.record(z25.string(), jsonValueSchema2.optional())
3624
3625
  ])
3625
3626
  );
3627
+ var jsonObjectSchema = z25.record(z25.string(), jsonValueSchema2.optional());
3628
+ var openaiResponsesUsageSchema = z25.intersection(
3629
+ jsonObjectSchema,
3630
+ z25.object({
3631
+ input_tokens: z25.number(),
3632
+ input_tokens_details: z25.intersection(
3633
+ jsonObjectSchema,
3634
+ z25.object({
3635
+ cached_tokens: z25.number().nullish(),
3636
+ cache_write_tokens: z25.number().nullish(),
3637
+ orchestration_input_tokens: z25.number().nullish(),
3638
+ orchestration_input_cached_tokens: z25.number().nullish()
3639
+ })
3640
+ ).nullish(),
3641
+ output_tokens: z25.number(),
3642
+ output_tokens_details: z25.intersection(
3643
+ jsonObjectSchema,
3644
+ z25.object({
3645
+ reasoning_tokens: z25.number().nullish(),
3646
+ orchestration_output_tokens: z25.number().nullish()
3647
+ })
3648
+ ).nullish(),
3649
+ total_tokens: z25.number().optional()
3650
+ })
3651
+ );
3626
3652
  var openaiResponsesComputerSafetyCheckSchema = z25.object({
3627
3653
  id: z25.string(),
3628
3654
  code: z25.string().nullish(),
@@ -3819,20 +3845,7 @@ var openaiResponsesChunkSchema = lazySchema24(
3819
3845
  type: z25.enum(["response.completed", "response.incomplete"]),
3820
3846
  response: z25.object({
3821
3847
  incomplete_details: z25.object({ reason: z25.string() }).nullish(),
3822
- usage: z25.object({
3823
- input_tokens: z25.number(),
3824
- input_tokens_details: z25.object({
3825
- cached_tokens: z25.number().nullish(),
3826
- cache_write_tokens: z25.number().nullish(),
3827
- orchestration_input_tokens: z25.number().nullish(),
3828
- orchestration_input_cached_tokens: z25.number().nullish()
3829
- }).nullish(),
3830
- output_tokens: z25.number(),
3831
- output_tokens_details: z25.object({
3832
- reasoning_tokens: z25.number().nullish(),
3833
- orchestration_output_tokens: z25.number().nullish()
3834
- }).nullish()
3835
- }).nullish(),
3848
+ usage: openaiResponsesUsageSchema.nullish(),
3836
3849
  reasoning: z25.object({
3837
3850
  context: z25.string().nullish()
3838
3851
  }).nullish(),
@@ -3848,20 +3861,7 @@ var openaiResponsesChunkSchema = lazySchema24(
3848
3861
  message: z25.string()
3849
3862
  }).nullish(),
3850
3863
  incomplete_details: z25.object({ reason: z25.string() }).nullish(),
3851
- usage: z25.object({
3852
- input_tokens: z25.number(),
3853
- input_tokens_details: z25.object({
3854
- cached_tokens: z25.number().nullish(),
3855
- cache_write_tokens: z25.number().nullish(),
3856
- orchestration_input_tokens: z25.number().nullish(),
3857
- orchestration_input_cached_tokens: z25.number().nullish()
3858
- }).nullish(),
3859
- output_tokens: z25.number(),
3860
- output_tokens_details: z25.object({
3861
- reasoning_tokens: z25.number().nullish(),
3862
- orchestration_output_tokens: z25.number().nullish()
3863
- }).nullish()
3864
- }).nullish(),
3864
+ usage: openaiResponsesUsageSchema.nullish(),
3865
3865
  reasoning: z25.object({
3866
3866
  context: z25.string().nullish()
3867
3867
  }).nullish(),
@@ -4651,20 +4651,7 @@ var openaiResponsesResponseSchema = lazySchema24(
4651
4651
  context: z25.string().nullish()
4652
4652
  }).nullish(),
4653
4653
  incomplete_details: z25.object({ reason: z25.string() }).nullish(),
4654
- usage: z25.object({
4655
- input_tokens: z25.number(),
4656
- input_tokens_details: z25.object({
4657
- cached_tokens: z25.number().nullish(),
4658
- cache_write_tokens: z25.number().nullish(),
4659
- orchestration_input_tokens: z25.number().nullish(),
4660
- orchestration_input_cached_tokens: z25.number().nullish()
4661
- }).nullish(),
4662
- output_tokens: z25.number(),
4663
- output_tokens_details: z25.object({
4664
- reasoning_tokens: z25.number().nullish(),
4665
- orchestration_output_tokens: z25.number().nullish()
4666
- }).nullish()
4667
- }).nullish()
4654
+ usage: openaiResponsesUsageSchema.nullish()
4668
4655
  })
4669
4656
  )
4670
4657
  );
@@ -8924,7 +8911,19 @@ function escapeJSONDelta(delta) {
8924
8911
 
8925
8912
  // src/openai-responses-batch.ts
8926
8913
  var openaiBatchEndpoint = "/v1/responses";
8927
- var openaiBatchInputFileExpiresAfterSeconds = 48 * 60 * 60;
8914
+ var openaiBatchInputFileDefaultExpiresAfterSeconds = 48 * 60 * 60;
8915
+ var openaiBatchProviderOptionsSchema = lazySchema26(
8916
+ () => zodSchema26(
8917
+ z28.object({
8918
+ /**
8919
+ * TTL in seconds for the uploaded batch input file, measured from
8920
+ * upload time. OpenAI accepts integers between 3600 (1 hour) and
8921
+ * 2592000 (30 days) inclusive. Defaults to 48 hours.
8922
+ */
8923
+ inputFileExpiresAfter: z28.number().int().min(3600).max(2592e3).optional()
8924
+ })
8925
+ )
8926
+ );
8928
8927
  var openaiBatchResponseSchema = lazySchema26(
8929
8928
  () => zodSchema26(
8930
8929
  z28.object({
@@ -8971,7 +8970,7 @@ var OpenAIResponsesBatch = class {
8971
8970
  this.options = options;
8972
8971
  }
8973
8972
  async startBatch(options) {
8974
- var _a2, _b, _c, _d, _e;
8973
+ var _a2, _b, _c, _d, _e, _f;
8975
8974
  const fileParts = [];
8976
8975
  const warnings = options.webhookUrl == null ? [] : [
8977
8976
  {
@@ -8982,6 +8981,10 @@ var OpenAIResponsesBatch = class {
8982
8981
  }
8983
8982
  }
8984
8983
  ];
8984
+ const batchOptions = await this.parseBatchProviderOptions(
8985
+ options.providerOptions
8986
+ );
8987
+ const inputFileExpiresAfterSeconds = (_a2 = batchOptions == null ? void 0 : batchOptions.inputFileExpiresAfter) != null ? _a2 : openaiBatchInputFileDefaultExpiresAfterSeconds;
8985
8988
  for (const request of options.requests) {
8986
8989
  const preparedRequest = await this.options.prepareRequest(request);
8987
8990
  fileParts.push(
@@ -8996,7 +8999,7 @@ var OpenAIResponsesBatch = class {
8996
8999
  for (const warning of preparedRequest.warnings) {
8997
9000
  warnings.push({ requestId: request.id, warning });
8998
9001
  }
8999
- for (const tool of (_a2 = request.options.tools) != null ? _a2 : []) {
9002
+ for (const tool of (_b = request.options.tools) != null ? _b : []) {
9000
9003
  if (tool.type === "provider" && !openAIBatchConvertibleProviderToolIds.has(tool.id)) {
9001
9004
  warnings.push({
9002
9005
  requestId: request.id,
@@ -9020,19 +9023,17 @@ var OpenAIResponsesBatch = class {
9020
9023
  formData.append("expires_after[anchor]", "created_at");
9021
9024
  formData.append(
9022
9025
  "expires_after[seconds]",
9023
- String(openaiBatchInputFileExpiresAfterSeconds)
9026
+ String(inputFileExpiresAfterSeconds)
9024
9027
  );
9025
9028
  const { value: uploadedFile } = await postToApi({
9026
9029
  url: this.getUrl("/files"),
9027
- headers: combineHeaders7((_c = (_b = this.options.config).headers) == null ? void 0 : _c.call(_b), options.headers),
9030
+ headers: combineHeaders7((_d = (_c = this.options.config).headers) == null ? void 0 : _d.call(_c), options.headers),
9028
9031
  body: {
9029
9032
  content: formData,
9030
9033
  values: {
9031
9034
  purpose: "batch",
9032
9035
  "expires_after[anchor]": "created_at",
9033
- "expires_after[seconds]": String(
9034
- openaiBatchInputFileExpiresAfterSeconds
9035
- ),
9036
+ "expires_after[seconds]": String(inputFileExpiresAfterSeconds),
9036
9037
  file: {
9037
9038
  name: filename,
9038
9039
  type: file.type,
@@ -9049,7 +9050,7 @@ var OpenAIResponsesBatch = class {
9049
9050
  });
9050
9051
  const { value: batch } = await postJsonToApi6({
9051
9052
  url: this.getUrl("/batches"),
9052
- headers: combineHeaders7((_e = (_d = this.options.config).headers) == null ? void 0 : _e.call(_d), options.headers),
9053
+ headers: combineHeaders7((_f = (_e = this.options.config).headers) == null ? void 0 : _f.call(_e), options.headers),
9053
9054
  body: {
9054
9055
  input_file_id: uploadedFile.id,
9055
9056
  endpoint: openaiBatchEndpoint,
@@ -9062,12 +9063,35 @@ var OpenAIResponsesBatch = class {
9062
9063
  abortSignal: options.abortSignal,
9063
9064
  fetch: this.options.config.fetch
9064
9065
  });
9066
+ const inputFileExpiresAt = convertUnixTimestamp(uploadedFile.expires_at);
9065
9067
  return {
9066
9068
  batchId: batch.id,
9067
9069
  ...convertOpenAIBatchStatus(batch),
9070
+ providerMetadata: {
9071
+ openai: {
9072
+ inputFileId: uploadedFile.id,
9073
+ ...inputFileExpiresAt != null ? { inputFileExpiresAt } : {}
9074
+ }
9075
+ },
9068
9076
  warnings
9069
9077
  };
9070
9078
  }
9079
+ async parseBatchProviderOptions(providerOptions) {
9080
+ const providerOptionsName = this.options.config.provider.includes("azure") ? "azure" : "openai";
9081
+ let batchOptions = await parseProviderOptions8({
9082
+ provider: providerOptionsName,
9083
+ providerOptions,
9084
+ schema: openaiBatchProviderOptionsSchema
9085
+ });
9086
+ if (batchOptions == null && providerOptionsName !== "openai") {
9087
+ batchOptions = await parseProviderOptions8({
9088
+ provider: "openai",
9089
+ providerOptions,
9090
+ schema: openaiBatchProviderOptionsSchema
9091
+ });
9092
+ }
9093
+ return batchOptions;
9094
+ }
9071
9095
  async getBatchStatus(options) {
9072
9096
  const batch = await this.retrieveBatch(options);
9073
9097
  return convertOpenAIBatchStatus(batch);
@@ -9896,7 +9920,7 @@ var OpenAIRealtimeModel = class {
9896
9920
  import {
9897
9921
  combineHeaders as combineHeaders8,
9898
9922
  createBinaryResponseHandler,
9899
- parseProviderOptions as parseProviderOptions8,
9923
+ parseProviderOptions as parseProviderOptions9,
9900
9924
  postJsonToApi as postJsonToApi7,
9901
9925
  serializeModelOptions as serializeModelOptions6,
9902
9926
  WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE7,
@@ -9947,7 +9971,7 @@ var OpenAISpeechModel = class _OpenAISpeechModel {
9947
9971
  providerOptions
9948
9972
  }) {
9949
9973
  const warnings = [];
9950
- const openAIOptions = await parseProviderOptions8({
9974
+ const openAIOptions = await parseProviderOptions9({
9951
9975
  provider: "openai",
9952
9976
  providerOptions,
9953
9977
  schema: openaiSpeechModelOptionsSchema
@@ -10039,7 +10063,7 @@ import {
10039
10063
  createJsonResponseHandler as createJsonResponseHandler8,
10040
10064
  connectToWebSocket,
10041
10065
  mediaTypeToExtension,
10042
- parseProviderOptions as parseProviderOptions9,
10066
+ parseProviderOptions as parseProviderOptions10,
10043
10067
  postFormDataToApi as postFormDataToApi3,
10044
10068
  safeParseJSON as safeParseJSON2,
10045
10069
  serializeModelOptions as serializeModelOptions7,
@@ -10218,7 +10242,7 @@ var OpenAITranscriptionModel = class _OpenAITranscriptionModel {
10218
10242
  providerOptions
10219
10243
  }) {
10220
10244
  const warnings = [];
10221
- const openAIOptions = await parseProviderOptions9({
10245
+ const openAIOptions = await parseProviderOptions10({
10222
10246
  provider: "openai",
10223
10247
  providerOptions,
10224
10248
  schema: openAITranscriptionModelOptions
@@ -10327,7 +10351,7 @@ var OpenAITranscriptionModel = class _OpenAITranscriptionModel {
10327
10351
  });
10328
10352
  }
10329
10353
  const currentDate = (_c = (_b = (_a2 = this.config._internal) == null ? void 0 : _a2.currentDate) == null ? void 0 : _b.call(_a2)) != null ? _c : /* @__PURE__ */ new Date();
10330
- const openAIOptions = await parseProviderOptions9({
10354
+ const openAIOptions = await parseProviderOptions10({
10331
10355
  provider: "openai",
10332
10356
  providerOptions: options.providerOptions,
10333
10357
  schema: openAITranscriptionModelOptions
@@ -10569,7 +10593,7 @@ import {
10569
10593
  combineHeaders as combineHeaders10,
10570
10594
  connectToWebSocket as connectToWebSocket2,
10571
10595
  convertToBase64 as convertToBase644,
10572
- parseProviderOptions as parseProviderOptions10,
10596
+ parseProviderOptions as parseProviderOptions11,
10573
10597
  safeParseJSON as safeParseJSON3,
10574
10598
  serializeModelOptions as serializeModelOptions8,
10575
10599
  toWebSocketUrl as toWebSocketUrl2,
@@ -10616,7 +10640,7 @@ var OpenAISpeechTranslationModel = class _OpenAISpeechTranslationModel {
10616
10640
  });
10617
10641
  }
10618
10642
  const currentDate = (_c = (_b = (_a2 = this.config._internal) == null ? void 0 : _a2.currentDate) == null ? void 0 : _b.call(_a2)) != null ? _c : /* @__PURE__ */ new Date();
10619
- await parseProviderOptions10({
10643
+ await parseProviderOptions11({
10620
10644
  provider: "openai",
10621
10645
  providerOptions: options.providerOptions,
10622
10646
  schema: openAISpeechTranslationModelOptions
@@ -10963,7 +10987,7 @@ var OpenAISkills = class {
10963
10987
  };
10964
10988
 
10965
10989
  // src/version.ts
10966
- var VERSION = true ? "4.0.55" : "0.0.0-test";
10990
+ var VERSION = true ? "4.0.57" : "0.0.0-test";
10967
10991
 
10968
10992
  // src/openai-provider.ts
10969
10993
  function createOpenAI(options = {}) {