@ai-sdk/openai 0.0.67 → 0.0.70

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.
@@ -1,7 +1,7 @@
1
1
  // src/openai-chat-language-model.ts
2
2
  import {
3
3
  InvalidResponseDataError,
4
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
4
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
5
5
  } from "@ai-sdk/provider";
6
6
  import {
7
7
  combineHeaders,
@@ -53,9 +53,31 @@ function convertToOpenAIChatMessages({
53
53
  };
54
54
  }
55
55
  case "file": {
56
- throw new UnsupportedFunctionalityError({
57
- functionality: "File content parts in user messages"
58
- });
56
+ if (part.data instanceof URL) {
57
+ throw new UnsupportedFunctionalityError({
58
+ functionality: "'File content parts with URL data' functionality not supported."
59
+ });
60
+ }
61
+ switch (part.mimeType) {
62
+ case "audio/wav": {
63
+ return {
64
+ type: "input_audio",
65
+ input_audio: { data: part.data, format: "wav" }
66
+ };
67
+ }
68
+ case "audio/mp3":
69
+ case "audio/mpeg": {
70
+ return {
71
+ type: "input_audio",
72
+ input_audio: { data: part.data, format: "mp3" }
73
+ };
74
+ }
75
+ default: {
76
+ throw new UnsupportedFunctionalityError({
77
+ functionality: `File content part type ${part.mimeType} in user messages`
78
+ });
79
+ }
80
+ }
59
81
  }
60
82
  }
61
83
  })
@@ -197,6 +219,107 @@ function getResponseMetadata({
197
219
  };
198
220
  }
199
221
 
222
+ // src/openai-prepare-tools.ts
223
+ import {
224
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
225
+ } from "@ai-sdk/provider";
226
+ function prepareTools({
227
+ mode,
228
+ useLegacyFunctionCalling = false,
229
+ structuredOutputs = false
230
+ }) {
231
+ var _a;
232
+ const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
233
+ const toolWarnings = [];
234
+ if (tools == null) {
235
+ return { tools: void 0, tool_choice: void 0, toolWarnings };
236
+ }
237
+ const toolChoice = mode.toolChoice;
238
+ if (useLegacyFunctionCalling) {
239
+ const openaiFunctions = [];
240
+ for (const tool of tools) {
241
+ if (tool.type === "provider-defined") {
242
+ toolWarnings.push({ type: "unsupported-tool", tool });
243
+ } else {
244
+ openaiFunctions.push({
245
+ name: tool.name,
246
+ description: tool.description,
247
+ parameters: tool.parameters
248
+ });
249
+ }
250
+ }
251
+ if (toolChoice == null) {
252
+ return {
253
+ functions: openaiFunctions,
254
+ function_call: void 0,
255
+ toolWarnings
256
+ };
257
+ }
258
+ const type2 = toolChoice.type;
259
+ switch (type2) {
260
+ case "auto":
261
+ case "none":
262
+ case void 0:
263
+ return {
264
+ functions: openaiFunctions,
265
+ function_call: void 0,
266
+ toolWarnings
267
+ };
268
+ case "required":
269
+ throw new UnsupportedFunctionalityError2({
270
+ functionality: "useLegacyFunctionCalling and toolChoice: required"
271
+ });
272
+ default:
273
+ return {
274
+ functions: openaiFunctions,
275
+ function_call: { name: toolChoice.toolName },
276
+ toolWarnings
277
+ };
278
+ }
279
+ }
280
+ const openaiTools = [];
281
+ for (const tool of tools) {
282
+ if (tool.type === "provider-defined") {
283
+ toolWarnings.push({ type: "unsupported-tool", tool });
284
+ } else {
285
+ openaiTools.push({
286
+ type: "function",
287
+ function: {
288
+ name: tool.name,
289
+ description: tool.description,
290
+ parameters: tool.parameters,
291
+ strict: structuredOutputs === true ? true : void 0
292
+ }
293
+ });
294
+ }
295
+ }
296
+ if (toolChoice == null) {
297
+ return { tools: openaiTools, tool_choice: void 0, toolWarnings };
298
+ }
299
+ const type = toolChoice.type;
300
+ switch (type) {
301
+ case "auto":
302
+ case "none":
303
+ case "required":
304
+ return { tools: openaiTools, tool_choice: type, toolWarnings };
305
+ case "tool":
306
+ return {
307
+ tools: openaiTools,
308
+ tool_choice: {
309
+ type: "function",
310
+ function: {
311
+ name: toolChoice.toolName
312
+ }
313
+ },
314
+ toolWarnings
315
+ };
316
+ default: {
317
+ const _exhaustiveCheck = type;
318
+ throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
319
+ }
320
+ }
321
+ }
322
+
200
323
  // src/openai-chat-language-model.ts
201
324
  var OpenAIChatLanguageModel = class {
202
325
  constructor(modelId, settings, config) {
@@ -209,6 +332,9 @@ var OpenAIChatLanguageModel = class {
209
332
  return this.settings.structuredOutputs === true;
210
333
  }
211
334
  get defaultObjectGenerationMode() {
335
+ if (isAudioModel(this.modelId)) {
336
+ return "tool";
337
+ }
212
338
  return this.supportsStructuredOutputs ? "json" : "tool";
213
339
  }
214
340
  get provider() {
@@ -249,12 +375,12 @@ var OpenAIChatLanguageModel = class {
249
375
  }
250
376
  const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
251
377
  if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
252
- throw new UnsupportedFunctionalityError2({
378
+ throw new UnsupportedFunctionalityError3({
253
379
  functionality: "useLegacyFunctionCalling with parallelToolCalls"
254
380
  });
255
381
  }
256
382
  if (useLegacyFunctionCalling && this.settings.structuredOutputs === true) {
257
- throw new UnsupportedFunctionalityError2({
383
+ throw new UnsupportedFunctionalityError3({
258
384
  functionality: "structuredOutputs with useLegacyFunctionCalling"
259
385
  });
260
386
  }
@@ -295,16 +421,20 @@ var OpenAIChatLanguageModel = class {
295
421
  }
296
422
  switch (type) {
297
423
  case "regular": {
424
+ const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({
425
+ mode,
426
+ useLegacyFunctionCalling,
427
+ structuredOutputs: this.settings.structuredOutputs
428
+ });
298
429
  return {
299
430
  args: {
300
431
  ...baseArgs,
301
- ...prepareToolsAndToolChoice({
302
- mode,
303
- useLegacyFunctionCalling,
304
- structuredOutputs: this.settings.structuredOutputs
305
- })
432
+ tools,
433
+ tool_choice,
434
+ functions,
435
+ function_call
306
436
  },
307
- warnings
437
+ warnings: [...warnings, ...toolWarnings]
308
438
  };
309
439
  }
310
440
  case "object-json": {
@@ -367,14 +497,14 @@ var OpenAIChatLanguageModel = class {
367
497
  }
368
498
  async doGenerate(options) {
369
499
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
370
- const { args, warnings } = this.getArgs(options);
500
+ const { args: body, warnings } = this.getArgs(options);
371
501
  const { responseHeaders, value: response } = await postJsonToApi({
372
502
  url: this.config.url({
373
503
  path: "/chat/completions",
374
504
  modelId: this.modelId
375
505
  }),
376
506
  headers: combineHeaders(this.config.headers(), options.headers),
377
- body: args,
507
+ body,
378
508
  failedResponseHandler: openaiFailedResponseHandler,
379
509
  successfulResponseHandler: createJsonResponseHandler(
380
510
  openAIChatResponseSchema
@@ -382,7 +512,7 @@ var OpenAIChatLanguageModel = class {
382
512
  abortSignal: options.abortSignal,
383
513
  fetch: this.config.fetch
384
514
  });
385
- const { messages: rawPrompt, ...rawSettings } = args;
515
+ const { messages: rawPrompt, ...rawSettings } = body;
386
516
  const choice = response.choices[0];
387
517
  let providerMetadata;
388
518
  if (((_b = (_a = response.usage) == null ? void 0 : _a.completion_tokens_details) == null ? void 0 : _b.reasoning_tokens) != null || ((_d = (_c = response.usage) == null ? void 0 : _c.prompt_tokens_details) == null ? void 0 : _d.cached_tokens) != null) {
@@ -419,6 +549,7 @@ var OpenAIChatLanguageModel = class {
419
549
  },
420
550
  rawCall: { rawPrompt, rawSettings },
421
551
  rawResponse: { headers: responseHeaders },
552
+ request: { body: JSON.stringify(body) },
422
553
  response: getResponseMetadata(response),
423
554
  warnings,
424
555
  logprobs: mapOpenAIChatLogProbsOutput(choice.logprobs),
@@ -463,18 +594,19 @@ var OpenAIChatLanguageModel = class {
463
594
  };
464
595
  }
465
596
  const { args, warnings } = this.getArgs(options);
597
+ const body = {
598
+ ...args,
599
+ stream: true,
600
+ // only include stream_options when in strict compatibility mode:
601
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
602
+ };
466
603
  const { responseHeaders, value: response } = await postJsonToApi({
467
604
  url: this.config.url({
468
605
  path: "/chat/completions",
469
606
  modelId: this.modelId
470
607
  }),
471
608
  headers: combineHeaders(this.config.headers(), options.headers),
472
- body: {
473
- ...args,
474
- stream: true,
475
- // only include stream_options when in strict compatibility mode:
476
- stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
477
- },
609
+ body,
478
610
  failedResponseHandler: openaiFailedResponseHandler,
479
611
  successfulResponseHandler: createEventSourceResponseHandler(
480
612
  openaiChatChunkSchema
@@ -651,6 +783,7 @@ var OpenAIChatLanguageModel = class {
651
783
  ),
652
784
  rawCall: { rawPrompt, rawSettings },
653
785
  rawResponse: { headers: responseHeaders },
786
+ request: { body: JSON.stringify(body) },
654
787
  warnings
655
788
  };
656
789
  }
@@ -757,87 +890,16 @@ var openaiChatChunkSchema = z2.union([
757
890
  }),
758
891
  openAIErrorDataSchema
759
892
  ]);
760
- function prepareToolsAndToolChoice({
761
- mode,
762
- useLegacyFunctionCalling = false,
763
- structuredOutputs = false
764
- }) {
765
- var _a;
766
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
767
- if (tools == null) {
768
- return { tools: void 0, tool_choice: void 0 };
769
- }
770
- const toolChoice = mode.toolChoice;
771
- if (useLegacyFunctionCalling) {
772
- const mappedFunctions = tools.map((tool) => ({
773
- name: tool.name,
774
- description: tool.description,
775
- parameters: tool.parameters
776
- }));
777
- if (toolChoice == null) {
778
- return { functions: mappedFunctions, function_call: void 0 };
779
- }
780
- const type2 = toolChoice.type;
781
- switch (type2) {
782
- case "auto":
783
- case "none":
784
- case void 0:
785
- return {
786
- functions: mappedFunctions,
787
- function_call: void 0
788
- };
789
- case "required":
790
- throw new UnsupportedFunctionalityError2({
791
- functionality: "useLegacyFunctionCalling and toolChoice: required"
792
- });
793
- default:
794
- return {
795
- functions: mappedFunctions,
796
- function_call: { name: toolChoice.toolName }
797
- };
798
- }
799
- }
800
- const mappedTools = tools.map((tool) => ({
801
- type: "function",
802
- function: {
803
- name: tool.name,
804
- description: tool.description,
805
- parameters: tool.parameters,
806
- strict: structuredOutputs === true ? true : void 0
807
- }
808
- }));
809
- if (toolChoice == null) {
810
- return { tools: mappedTools, tool_choice: void 0 };
811
- }
812
- const type = toolChoice.type;
813
- switch (type) {
814
- case "auto":
815
- case "none":
816
- case "required":
817
- return { tools: mappedTools, tool_choice: type };
818
- case "tool":
819
- return {
820
- tools: mappedTools,
821
- tool_choice: {
822
- type: "function",
823
- function: {
824
- name: toolChoice.toolName
825
- }
826
- }
827
- };
828
- default: {
829
- const _exhaustiveCheck = type;
830
- throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
831
- }
832
- }
833
- }
834
893
  function isReasoningModel(modelId) {
835
894
  return modelId.startsWith("o1-");
836
895
  }
896
+ function isAudioModel(modelId) {
897
+ return modelId.startsWith("gpt-4o-audio-preview");
898
+ }
837
899
 
838
900
  // src/openai-completion-language-model.ts
839
901
  import {
840
- UnsupportedFunctionalityError as UnsupportedFunctionalityError4
902
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError5
841
903
  } from "@ai-sdk/provider";
842
904
  import {
843
905
  combineHeaders as combineHeaders2,
@@ -850,7 +912,7 @@ import { z as z3 } from "zod";
850
912
  // src/convert-to-openai-completion-prompt.ts
851
913
  import {
852
914
  InvalidPromptError,
853
- UnsupportedFunctionalityError as UnsupportedFunctionalityError3
915
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError4
854
916
  } from "@ai-sdk/provider";
855
917
  function convertToOpenAICompletionPrompt({
856
918
  prompt,
@@ -883,7 +945,7 @@ function convertToOpenAICompletionPrompt({
883
945
  return part.text;
884
946
  }
885
947
  case "image": {
886
- throw new UnsupportedFunctionalityError3({
948
+ throw new UnsupportedFunctionalityError4({
887
949
  functionality: "images"
888
950
  });
889
951
  }
@@ -902,7 +964,7 @@ ${userMessage}
902
964
  return part.text;
903
965
  }
904
966
  case "tool-call": {
905
- throw new UnsupportedFunctionalityError3({
967
+ throw new UnsupportedFunctionalityError4({
906
968
  functionality: "tool-call messages"
907
969
  });
908
970
  }
@@ -915,7 +977,7 @@ ${assistantMessage}
915
977
  break;
916
978
  }
917
979
  case "tool": {
918
- throw new UnsupportedFunctionalityError3({
980
+ throw new UnsupportedFunctionalityError4({
919
981
  functionality: "tool messages"
920
982
  });
921
983
  }
@@ -1016,24 +1078,24 @@ var OpenAICompletionLanguageModel = class {
1016
1078
  switch (type) {
1017
1079
  case "regular": {
1018
1080
  if ((_a = mode.tools) == null ? void 0 : _a.length) {
1019
- throw new UnsupportedFunctionalityError4({
1081
+ throw new UnsupportedFunctionalityError5({
1020
1082
  functionality: "tools"
1021
1083
  });
1022
1084
  }
1023
1085
  if (mode.toolChoice) {
1024
- throw new UnsupportedFunctionalityError4({
1086
+ throw new UnsupportedFunctionalityError5({
1025
1087
  functionality: "toolChoice"
1026
1088
  });
1027
1089
  }
1028
1090
  return { args: baseArgs, warnings };
1029
1091
  }
1030
1092
  case "object-json": {
1031
- throw new UnsupportedFunctionalityError4({
1093
+ throw new UnsupportedFunctionalityError5({
1032
1094
  functionality: "object-json mode"
1033
1095
  });
1034
1096
  }
1035
1097
  case "object-tool": {
1036
- throw new UnsupportedFunctionalityError4({
1098
+ throw new UnsupportedFunctionalityError5({
1037
1099
  functionality: "object-tool mode"
1038
1100
  });
1039
1101
  }
@@ -1072,23 +1134,25 @@ var OpenAICompletionLanguageModel = class {
1072
1134
  rawCall: { rawPrompt, rawSettings },
1073
1135
  rawResponse: { headers: responseHeaders },
1074
1136
  response: getResponseMetadata(response),
1075
- warnings
1137
+ warnings,
1138
+ request: { body: JSON.stringify(args) }
1076
1139
  };
1077
1140
  }
1078
1141
  async doStream(options) {
1079
1142
  const { args, warnings } = this.getArgs(options);
1143
+ const body = {
1144
+ ...args,
1145
+ stream: true,
1146
+ // only include stream_options when in strict compatibility mode:
1147
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
1148
+ };
1080
1149
  const { responseHeaders, value: response } = await postJsonToApi2({
1081
1150
  url: this.config.url({
1082
1151
  path: "/completions",
1083
1152
  modelId: this.modelId
1084
1153
  }),
1085
1154
  headers: combineHeaders2(this.config.headers(), options.headers),
1086
- body: {
1087
- ...args,
1088
- stream: true,
1089
- // only include stream_options when in strict compatibility mode:
1090
- stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
1091
- },
1155
+ body,
1092
1156
  failedResponseHandler: openaiFailedResponseHandler,
1093
1157
  successfulResponseHandler: createEventSourceResponseHandler2(
1094
1158
  openaiCompletionChunkSchema
@@ -1162,7 +1226,8 @@ var OpenAICompletionLanguageModel = class {
1162
1226
  ),
1163
1227
  rawCall: { rawPrompt, rawSettings },
1164
1228
  rawResponse: { headers: responseHeaders },
1165
- warnings
1229
+ warnings,
1230
+ request: { body: JSON.stringify(body) }
1166
1231
  };
1167
1232
  }
1168
1233
  };