@ai-sdk/openai 4.0.0-beta.40 → 4.0.0-beta.42

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 (47) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/index.d.ts +6 -6
  3. package/dist/index.js +182 -124
  4. package/dist/index.js.map +1 -1
  5. package/dist/internal/index.d.ts +2 -2
  6. package/dist/internal/index.js +168 -113
  7. package/dist/internal/index.js.map +1 -1
  8. package/docs/03-openai.mdx +18 -13
  9. package/package.json +4 -4
  10. package/src/chat/convert-openai-chat-usage.ts +1 -1
  11. package/src/chat/convert-to-openai-chat-messages.ts +85 -72
  12. package/src/chat/map-openai-finish-reason.ts +1 -1
  13. package/src/chat/openai-chat-api.ts +6 -2
  14. package/src/chat/{openai-chat-options.ts → openai-chat-language-model-options.ts} +5 -1
  15. package/src/chat/openai-chat-language-model.ts +7 -7
  16. package/src/chat/openai-chat-prepare-tools.ts +3 -3
  17. package/src/completion/convert-openai-completion-usage.ts +1 -1
  18. package/src/completion/convert-to-openai-completion-prompt.ts +1 -2
  19. package/src/completion/map-openai-finish-reason.ts +1 -1
  20. package/src/completion/openai-completion-api.ts +5 -2
  21. package/src/completion/{openai-completion-options.ts → openai-completion-language-model-options.ts} +5 -1
  22. package/src/completion/openai-completion-language-model.ts +7 -8
  23. package/src/embedding/{openai-embedding-options.ts → openai-embedding-model-options.ts} +5 -1
  24. package/src/embedding/openai-embedding-model.ts +4 -4
  25. package/src/files/openai-files-options.ts +5 -1
  26. package/src/files/openai-files.ts +5 -7
  27. package/src/image/openai-image-model.ts +3 -4
  28. package/src/index.ts +6 -6
  29. package/src/internal/index.ts +5 -5
  30. package/src/openai-config.ts +1 -1
  31. package/src/openai-provider.ts +9 -9
  32. package/src/responses/convert-openai-responses-usage.ts +1 -1
  33. package/src/responses/convert-to-openai-responses-input.ts +72 -60
  34. package/src/responses/map-openai-responses-finish-reason.ts +1 -1
  35. package/src/responses/openai-responses-api.ts +6 -2
  36. package/src/responses/{openai-responses-options.ts → openai-responses-language-model-options.ts} +5 -1
  37. package/src/responses/openai-responses-language-model.ts +36 -36
  38. package/src/responses/openai-responses-prepare-tools.ts +5 -5
  39. package/src/responses/openai-responses-provider-metadata.ts +2 -2
  40. package/src/skills/openai-skills.ts +4 -8
  41. package/src/speech/{openai-speech-options.ts → openai-speech-model-options.ts} +5 -1
  42. package/src/speech/openai-speech-model.ts +5 -6
  43. package/src/tool/file-search.ts +1 -1
  44. package/src/tool/mcp.ts +1 -1
  45. package/src/tool/tool-search.ts +2 -2
  46. package/src/transcription/{openai-transcription-options.ts → openai-transcription-model-options.ts} +5 -1
  47. package/src/transcription/openai-transcription-model.ts +5 -6
@@ -981,7 +981,7 @@ declare const codeInterpreterToolFactory: _ai_sdk_provider_utils.ProviderExecute
981
981
  url: string;
982
982
  }> | null;
983
983
  }, CodeInterpreterArgs, {}>;
984
- declare const codeInterpreter: (args?: CodeInterpreterArgs) => _ai_sdk_provider_utils.Tool<{
984
+ declare const codeInterpreter: (args?: CodeInterpreterArgs) => _ai_sdk_provider_utils.ProviderExecutedTool<{
985
985
  /**
986
986
  * The code to run, or null if not available.
987
987
  */
@@ -1167,7 +1167,7 @@ type ImageGenerationArgs = {
1167
1167
  */
1168
1168
  size?: 'auto' | '1024x1024' | '1024x1536' | '1536x1024';
1169
1169
  };
1170
- declare const imageGeneration: (args?: ImageGenerationArgs) => _ai_sdk_provider_utils.Tool<{}, {
1170
+ declare const imageGeneration: (args?: ImageGenerationArgs) => _ai_sdk_provider_utils.ProviderExecutedTool<{}, {
1171
1171
  /**
1172
1172
  * The generated image encoded in base64.
1173
1173
  */
@@ -93,7 +93,8 @@ import {
93
93
  } from "@ai-sdk/provider";
94
94
  import {
95
95
  convertToBase64,
96
- isProviderReference,
96
+ getTopLevelMediaType,
97
+ resolveFullMediaType,
97
98
  resolveProviderReference
98
99
  } from "@ai-sdk/provider-utils";
99
100
  function serializeToolCallArguments(input) {
@@ -148,75 +149,89 @@ function convertToOpenAIChatMessages({
148
149
  return { type: "text", text: part.text };
149
150
  }
150
151
  case "file": {
151
- if (isProviderReference(part.data)) {
152
- return {
153
- type: "file",
154
- file: {
155
- file_id: resolveProviderReference({
156
- reference: part.data,
157
- provider: "openai"
158
- })
159
- }
160
- };
161
- }
162
- if (part.mediaType.startsWith("image/")) {
163
- const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
164
- return {
165
- type: "image_url",
166
- image_url: {
167
- url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`,
168
- detail: (_b = (_a2 = part.providerOptions) == null ? void 0 : _a2.openai) == null ? void 0 : _b.imageDetail
169
- }
170
- };
171
- } else if (part.mediaType.startsWith("audio/")) {
172
- if (part.data instanceof URL) {
152
+ switch (part.data.type) {
153
+ case "reference": {
154
+ return {
155
+ type: "file",
156
+ file: {
157
+ file_id: resolveProviderReference({
158
+ reference: part.data.reference,
159
+ provider: "openai"
160
+ })
161
+ }
162
+ };
163
+ }
164
+ case "text": {
173
165
  throw new UnsupportedFunctionalityError({
174
- functionality: "audio file parts with URLs"
166
+ functionality: "text file parts"
175
167
  });
176
168
  }
177
- switch (part.mediaType) {
178
- case "audio/wav": {
169
+ case "url":
170
+ case "data": {
171
+ const topLevel = getTopLevelMediaType(part.mediaType);
172
+ if (topLevel === "image") {
179
173
  return {
180
- type: "input_audio",
181
- input_audio: {
182
- data: convertToBase64(part.data),
183
- format: "wav"
174
+ type: "image_url",
175
+ image_url: {
176
+ url: part.data.type === "url" ? part.data.url.toString() : convertToBase64(part.data.data),
177
+ detail: (_b = (_a2 = part.providerOptions) == null ? void 0 : _a2.openai) == null ? void 0 : _b.imageDetail
184
178
  }
185
179
  };
180
+ } else if (topLevel === "audio") {
181
+ if (part.data.type === "url") {
182
+ throw new UnsupportedFunctionalityError({
183
+ functionality: "audio file parts with URLs"
184
+ });
185
+ }
186
+ const fullMediaType = resolveFullMediaType({ part });
187
+ switch (fullMediaType) {
188
+ case "audio/wav": {
189
+ return {
190
+ type: "input_audio",
191
+ input_audio: {
192
+ data: convertToBase64(part.data.data),
193
+ format: "wav"
194
+ }
195
+ };
196
+ }
197
+ case "audio/mp3":
198
+ case "audio/mpeg": {
199
+ return {
200
+ type: "input_audio",
201
+ input_audio: {
202
+ data: convertToBase64(part.data.data),
203
+ format: "mp3"
204
+ }
205
+ };
206
+ }
207
+ default: {
208
+ throw new UnsupportedFunctionalityError({
209
+ functionality: `audio content parts with media type ${fullMediaType}`
210
+ });
211
+ }
212
+ }
186
213
  }
187
- case "audio/mp3":
188
- case "audio/mpeg": {
214
+ {
215
+ const fullMediaType = resolveFullMediaType({ part });
216
+ if (fullMediaType !== "application/pdf") {
217
+ throw new UnsupportedFunctionalityError({
218
+ functionality: `file part media type ${fullMediaType}`
219
+ });
220
+ }
221
+ if (part.data.type === "url") {
222
+ throw new UnsupportedFunctionalityError({
223
+ functionality: "PDF file parts with URLs"
224
+ });
225
+ }
189
226
  return {
190
- type: "input_audio",
191
- input_audio: {
192
- data: convertToBase64(part.data),
193
- format: "mp3"
227
+ type: "file",
228
+ file: {
229
+ filename: (_c = part.filename) != null ? _c : `part-${index}.pdf`,
230
+ file_data: `data:application/pdf;base64,${convertToBase64(part.data.data)}`
194
231
  }
195
232
  };
196
233
  }
197
- default: {
198
- throw new UnsupportedFunctionalityError({
199
- functionality: `audio content parts with media type ${part.mediaType}`
200
- });
201
- }
202
234
  }
203
- } else if (part.mediaType === "application/pdf") {
204
- if (part.data instanceof URL) {
205
- throw new UnsupportedFunctionalityError({
206
- functionality: "PDF file parts with URLs"
207
- });
208
- }
209
- return {
210
- type: "file",
211
- file: {
212
- filename: (_c = part.filename) != null ? _c : `part-${index}.pdf`,
213
- file_data: `data:application/pdf;base64,${convertToBase64(part.data)}`
214
- }
215
- };
216
- } else {
217
- throw new UnsupportedFunctionalityError({
218
- functionality: `file part media type ${part.mediaType}`
219
- });
220
235
  }
221
236
  }
222
237
  }
@@ -322,7 +337,10 @@ function mapOpenAIFinishReason(finishReason) {
322
337
  }
323
338
 
324
339
  // src/chat/openai-chat-api.ts
325
- import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
340
+ import {
341
+ lazySchema,
342
+ zodSchema
343
+ } from "@ai-sdk/provider-utils";
326
344
  import { z as z2 } from "zod/v4";
327
345
  var openaiChatResponseSchema = lazySchema(
328
346
  () => zodSchema(
@@ -463,8 +481,11 @@ var openaiChatChunkSchema = lazySchema(
463
481
  )
464
482
  );
465
483
 
466
- // src/chat/openai-chat-options.ts
467
- import { lazySchema as lazySchema2, zodSchema as zodSchema2 } from "@ai-sdk/provider-utils";
484
+ // src/chat/openai-chat-language-model-options.ts
485
+ import {
486
+ lazySchema as lazySchema2,
487
+ zodSchema as zodSchema2
488
+ } from "@ai-sdk/provider-utils";
468
489
  import { z as z3 } from "zod/v4";
469
490
  var openaiLanguageModelChatOptions = lazySchema2(
470
491
  () => zodSchema2(
@@ -1222,7 +1243,10 @@ function mapOpenAIFinishReason2(finishReason) {
1222
1243
 
1223
1244
  // src/completion/openai-completion-api.ts
1224
1245
  import { z as z4 } from "zod/v4";
1225
- import { lazySchema as lazySchema3, zodSchema as zodSchema3 } from "@ai-sdk/provider-utils";
1246
+ import {
1247
+ lazySchema as lazySchema3,
1248
+ zodSchema as zodSchema3
1249
+ } from "@ai-sdk/provider-utils";
1226
1250
  var openaiCompletionResponseSchema = lazySchema3(
1227
1251
  () => zodSchema3(
1228
1252
  z4.object({
@@ -1278,8 +1302,11 @@ var openaiCompletionChunkSchema = lazySchema3(
1278
1302
  )
1279
1303
  );
1280
1304
 
1281
- // src/completion/openai-completion-options.ts
1282
- import { lazySchema as lazySchema4, zodSchema as zodSchema4 } from "@ai-sdk/provider-utils";
1305
+ // src/completion/openai-completion-language-model-options.ts
1306
+ import {
1307
+ lazySchema as lazySchema4,
1308
+ zodSchema as zodSchema4
1309
+ } from "@ai-sdk/provider-utils";
1283
1310
  import { z as z5 } from "zod/v4";
1284
1311
  var openaiLanguageModelCompletionOptions = lazySchema4(
1285
1312
  () => zodSchema4(
@@ -1579,8 +1606,11 @@ import {
1579
1606
  WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE3
1580
1607
  } from "@ai-sdk/provider-utils";
1581
1608
 
1582
- // src/embedding/openai-embedding-options.ts
1583
- import { lazySchema as lazySchema5, zodSchema as zodSchema5 } from "@ai-sdk/provider-utils";
1609
+ // src/embedding/openai-embedding-model-options.ts
1610
+ import {
1611
+ lazySchema as lazySchema5,
1612
+ zodSchema as zodSchema5
1613
+ } from "@ai-sdk/provider-utils";
1584
1614
  import { z as z6 } from "zod/v4";
1585
1615
  var openaiEmbeddingModelOptions = lazySchema5(
1586
1616
  () => zodSchema5(
@@ -2001,8 +2031,11 @@ var openaiTranscriptionResponseSchema = lazySchema8(
2001
2031
  )
2002
2032
  );
2003
2033
 
2004
- // src/transcription/openai-transcription-options.ts
2005
- import { lazySchema as lazySchema9, zodSchema as zodSchema9 } from "@ai-sdk/provider-utils";
2034
+ // src/transcription/openai-transcription-model-options.ts
2035
+ import {
2036
+ lazySchema as lazySchema9,
2037
+ zodSchema as zodSchema9
2038
+ } from "@ai-sdk/provider-utils";
2006
2039
  import { z as z10 } from "zod/v4";
2007
2040
  var openAITranscriptionModelOptions = lazySchema9(
2008
2041
  () => zodSchema9(
@@ -2220,8 +2253,11 @@ import {
2220
2253
  WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE6
2221
2254
  } from "@ai-sdk/provider-utils";
2222
2255
 
2223
- // src/speech/openai-speech-options.ts
2224
- import { lazySchema as lazySchema10, zodSchema as zodSchema10 } from "@ai-sdk/provider-utils";
2256
+ // src/speech/openai-speech-model-options.ts
2257
+ import {
2258
+ lazySchema as lazySchema10,
2259
+ zodSchema as zodSchema10
2260
+ } from "@ai-sdk/provider-utils";
2225
2261
  import { z as z11 } from "zod/v4";
2226
2262
  var openaiSpeechModelOptionsSchema = lazySchema10(
2227
2263
  () => zodSchema10(
@@ -2405,10 +2441,11 @@ import {
2405
2441
  } from "@ai-sdk/provider";
2406
2442
  import {
2407
2443
  convertToBase64 as convertToBase642,
2444
+ getTopLevelMediaType as getTopLevelMediaType2,
2408
2445
  isNonNullable,
2409
- isProviderReference as isProviderReference2,
2410
2446
  parseJSON,
2411
2447
  parseProviderOptions as parseProviderOptions6,
2448
+ resolveFullMediaType as resolveFullMediaType2,
2412
2449
  resolveProviderReference as resolveProviderReference2,
2413
2450
  validateTypes
2414
2451
  } from "@ai-sdk/provider-utils";
@@ -2691,50 +2728,62 @@ async function convertToOpenAIResponsesInput({
2691
2728
  return { type: "input_text", text: part.text };
2692
2729
  }
2693
2730
  case "file": {
2694
- if (isProviderReference2(part.data)) {
2695
- const fileId = resolveProviderReference2({
2696
- reference: part.data,
2697
- provider: providerOptionsName
2698
- });
2699
- if (part.mediaType.startsWith("image/")) {
2700
- return {
2701
- type: "input_image",
2702
- file_id: fileId,
2703
- detail: (_b2 = (_a2 = part.providerOptions) == null ? void 0 : _a2[providerOptionsName]) == null ? void 0 : _b2.imageDetail
2704
- };
2705
- }
2706
- return {
2707
- type: "input_file",
2708
- file_id: fileId
2709
- };
2710
- }
2711
- if (part.mediaType.startsWith("image/")) {
2712
- const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
2713
- return {
2714
- type: "input_image",
2715
- ...part.data instanceof URL ? { image_url: part.data.toString() } : typeof part.data === "string" && isFileId(part.data, fileIdPrefixes) ? { file_id: part.data } : {
2716
- image_url: `data:${mediaType};base64,${convertToBase642(part.data)}`
2717
- },
2718
- detail: (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2[providerOptionsName]) == null ? void 0 : _d2.imageDetail
2719
- };
2720
- } else if (part.mediaType === "application/pdf") {
2721
- if (part.data instanceof URL) {
2731
+ switch (part.data.type) {
2732
+ case "reference": {
2733
+ const fileId = resolveProviderReference2({
2734
+ reference: part.data.reference,
2735
+ provider: providerOptionsName
2736
+ });
2737
+ if (getTopLevelMediaType2(part.mediaType) === "image") {
2738
+ return {
2739
+ type: "input_image",
2740
+ file_id: fileId,
2741
+ detail: (_b2 = (_a2 = part.providerOptions) == null ? void 0 : _a2[providerOptionsName]) == null ? void 0 : _b2.imageDetail
2742
+ };
2743
+ }
2722
2744
  return {
2723
2745
  type: "input_file",
2724
- file_url: part.data.toString()
2746
+ file_id: fileId
2725
2747
  };
2726
2748
  }
2727
- return {
2728
- type: "input_file",
2729
- ...typeof part.data === "string" && isFileId(part.data, fileIdPrefixes) ? { file_id: part.data } : {
2730
- filename: (_e2 = part.filename) != null ? _e2 : `part-${index}.pdf`,
2731
- file_data: `data:application/pdf;base64,${convertToBase642(part.data)}`
2749
+ case "text": {
2750
+ throw new UnsupportedFunctionalityError4({
2751
+ functionality: "text file parts"
2752
+ });
2753
+ }
2754
+ case "url":
2755
+ case "data": {
2756
+ const topLevel = getTopLevelMediaType2(part.mediaType);
2757
+ if (topLevel === "image") {
2758
+ return {
2759
+ type: "input_image",
2760
+ ...part.data.type === "url" ? { image_url: part.data.url.toString() } : typeof part.data.data === "string" && isFileId(part.data.data, fileIdPrefixes) ? { file_id: part.data.data } : {
2761
+ image_url: `data:${resolveFullMediaType2({ part })};base64,${convertToBase642(part.data.data)}`
2762
+ },
2763
+ detail: (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2[providerOptionsName]) == null ? void 0 : _d2.imageDetail
2764
+ };
2765
+ } else {
2766
+ if (part.data.type === "url") {
2767
+ return {
2768
+ type: "input_file",
2769
+ file_url: part.data.url.toString()
2770
+ };
2771
+ }
2772
+ const fullMediaType = resolveFullMediaType2({ part });
2773
+ if (fullMediaType !== "application/pdf") {
2774
+ throw new UnsupportedFunctionalityError4({
2775
+ functionality: `file part media type ${fullMediaType}`
2776
+ });
2777
+ }
2778
+ return {
2779
+ type: "input_file",
2780
+ ...typeof part.data.data === "string" && isFileId(part.data.data, fileIdPrefixes) ? { file_id: part.data.data } : {
2781
+ filename: (_e2 = part.filename) != null ? _e2 : `part-${index}.pdf`,
2782
+ file_data: `data:application/pdf;base64,${convertToBase642(part.data.data)}`
2783
+ }
2784
+ };
2732
2785
  }
2733
- };
2734
- } else {
2735
- throw new UnsupportedFunctionalityError4({
2736
- functionality: `file part media type ${part.mediaType}`
2737
- });
2786
+ }
2738
2787
  }
2739
2788
  }
2740
2789
  }
@@ -3297,7 +3346,10 @@ function mapOpenAIResponseFinishReason({
3297
3346
  }
3298
3347
 
3299
3348
  // src/responses/openai-responses-api.ts
3300
- import { lazySchema as lazySchema15, zodSchema as zodSchema15 } from "@ai-sdk/provider-utils";
3349
+ import {
3350
+ lazySchema as lazySchema15,
3351
+ zodSchema as zodSchema15
3352
+ } from "@ai-sdk/provider-utils";
3301
3353
  import { z as z17 } from "zod/v4";
3302
3354
  var jsonValueSchema = z17.lazy(
3303
3355
  () => z17.union([
@@ -4156,8 +4208,11 @@ var openaiResponsesResponseSchema = lazySchema15(
4156
4208
  )
4157
4209
  );
4158
4210
 
4159
- // src/responses/openai-responses-options.ts
4160
- import { lazySchema as lazySchema16, zodSchema as zodSchema16 } from "@ai-sdk/provider-utils";
4211
+ // src/responses/openai-responses-language-model-options.ts
4212
+ import {
4213
+ lazySchema as lazySchema16,
4214
+ zodSchema as zodSchema16
4215
+ } from "@ai-sdk/provider-utils";
4161
4216
  import { z as z18 } from "zod/v4";
4162
4217
  var TOP_LOGPROBS_MAX = 20;
4163
4218
  var openaiResponsesReasoningModelIds = [