@ai-sdk/openai 0.0.68 → 0.0.71

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
@@ -4,7 +4,7 @@ import { loadApiKey, withoutTrailingSlash } from "@ai-sdk/provider-utils";
4
4
  // src/openai-chat-language-model.ts
5
5
  import {
6
6
  InvalidResponseDataError,
7
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
7
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
8
8
  } from "@ai-sdk/provider";
9
9
  import {
10
10
  combineHeaders,
@@ -222,6 +222,109 @@ function getResponseMetadata({
222
222
  };
223
223
  }
224
224
 
225
+ // src/openai-prepare-tools.ts
226
+ import {
227
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
228
+ } from "@ai-sdk/provider";
229
+ function prepareTools({
230
+ mode,
231
+ useLegacyFunctionCalling = false,
232
+ structuredOutputs = false
233
+ }) {
234
+ var _a;
235
+ const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
236
+ const toolWarnings = [];
237
+ if (tools == null) {
238
+ return { tools: void 0, tool_choice: void 0, toolWarnings };
239
+ }
240
+ const toolChoice = mode.toolChoice;
241
+ if (useLegacyFunctionCalling) {
242
+ const openaiFunctions = [];
243
+ for (const tool of tools) {
244
+ if (tool.type === "provider-defined") {
245
+ toolWarnings.push({ type: "unsupported-tool", tool });
246
+ } else {
247
+ openaiFunctions.push({
248
+ name: tool.name,
249
+ description: tool.description,
250
+ parameters: tool.parameters
251
+ });
252
+ }
253
+ }
254
+ if (toolChoice == null) {
255
+ return {
256
+ functions: openaiFunctions,
257
+ function_call: void 0,
258
+ toolWarnings
259
+ };
260
+ }
261
+ const type2 = toolChoice.type;
262
+ switch (type2) {
263
+ case "auto":
264
+ case "none":
265
+ case void 0:
266
+ return {
267
+ functions: openaiFunctions,
268
+ function_call: void 0,
269
+ toolWarnings
270
+ };
271
+ case "required":
272
+ throw new UnsupportedFunctionalityError2({
273
+ functionality: "useLegacyFunctionCalling and toolChoice: required"
274
+ });
275
+ default:
276
+ return {
277
+ functions: openaiFunctions,
278
+ function_call: { name: toolChoice.toolName },
279
+ toolWarnings
280
+ };
281
+ }
282
+ }
283
+ const openaiTools = [];
284
+ for (const tool of tools) {
285
+ if (tool.type === "provider-defined") {
286
+ toolWarnings.push({ type: "unsupported-tool", tool });
287
+ } else {
288
+ openaiTools.push({
289
+ type: "function",
290
+ function: {
291
+ name: tool.name,
292
+ description: tool.description,
293
+ parameters: tool.parameters,
294
+ strict: structuredOutputs === true ? true : void 0
295
+ }
296
+ });
297
+ }
298
+ }
299
+ if (toolChoice == null) {
300
+ return { tools: openaiTools, tool_choice: void 0, toolWarnings };
301
+ }
302
+ const type = toolChoice.type;
303
+ switch (type) {
304
+ case "auto":
305
+ case "none":
306
+ case "required":
307
+ return { tools: openaiTools, tool_choice: type, toolWarnings };
308
+ case "tool":
309
+ return {
310
+ tools: openaiTools,
311
+ tool_choice: {
312
+ type: "function",
313
+ function: {
314
+ name: toolChoice.toolName
315
+ }
316
+ },
317
+ toolWarnings
318
+ };
319
+ default: {
320
+ const _exhaustiveCheck = type;
321
+ throw new UnsupportedFunctionalityError2({
322
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
323
+ });
324
+ }
325
+ }
326
+ }
327
+
225
328
  // src/openai-chat-language-model.ts
226
329
  var OpenAIChatLanguageModel = class {
227
330
  constructor(modelId, settings, config) {
@@ -277,12 +380,12 @@ var OpenAIChatLanguageModel = class {
277
380
  }
278
381
  const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
279
382
  if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
280
- throw new UnsupportedFunctionalityError2({
383
+ throw new UnsupportedFunctionalityError3({
281
384
  functionality: "useLegacyFunctionCalling with parallelToolCalls"
282
385
  });
283
386
  }
284
387
  if (useLegacyFunctionCalling && this.settings.structuredOutputs === true) {
285
- throw new UnsupportedFunctionalityError2({
388
+ throw new UnsupportedFunctionalityError3({
286
389
  functionality: "structuredOutputs with useLegacyFunctionCalling"
287
390
  });
288
391
  }
@@ -323,23 +426,27 @@ var OpenAIChatLanguageModel = class {
323
426
  }
324
427
  switch (type) {
325
428
  case "regular": {
429
+ const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({
430
+ mode,
431
+ useLegacyFunctionCalling,
432
+ structuredOutputs: this.settings.structuredOutputs
433
+ });
326
434
  return {
327
435
  args: {
328
436
  ...baseArgs,
329
- ...prepareToolsAndToolChoice({
330
- mode,
331
- useLegacyFunctionCalling,
332
- structuredOutputs: this.settings.structuredOutputs
333
- })
437
+ tools,
438
+ tool_choice,
439
+ functions,
440
+ function_call
334
441
  },
335
- warnings
442
+ warnings: [...warnings, ...toolWarnings]
336
443
  };
337
444
  }
338
445
  case "object-json": {
339
446
  return {
340
447
  args: {
341
448
  ...baseArgs,
342
- response_format: this.settings.structuredOutputs === true ? {
449
+ response_format: this.settings.structuredOutputs === true && mode.schema != null ? {
343
450
  type: "json_schema",
344
451
  json_schema: {
345
452
  schema: mode.schema,
@@ -395,14 +502,14 @@ var OpenAIChatLanguageModel = class {
395
502
  }
396
503
  async doGenerate(options) {
397
504
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
398
- const { args, warnings } = this.getArgs(options);
505
+ const { args: body, warnings } = this.getArgs(options);
399
506
  const { responseHeaders, value: response } = await postJsonToApi({
400
507
  url: this.config.url({
401
508
  path: "/chat/completions",
402
509
  modelId: this.modelId
403
510
  }),
404
511
  headers: combineHeaders(this.config.headers(), options.headers),
405
- body: args,
512
+ body,
406
513
  failedResponseHandler: openaiFailedResponseHandler,
407
514
  successfulResponseHandler: createJsonResponseHandler(
408
515
  openAIChatResponseSchema
@@ -410,7 +517,7 @@ var OpenAIChatLanguageModel = class {
410
517
  abortSignal: options.abortSignal,
411
518
  fetch: this.config.fetch
412
519
  });
413
- const { messages: rawPrompt, ...rawSettings } = args;
520
+ const { messages: rawPrompt, ...rawSettings } = body;
414
521
  const choice = response.choices[0];
415
522
  let providerMetadata;
416
523
  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) {
@@ -447,6 +554,7 @@ var OpenAIChatLanguageModel = class {
447
554
  },
448
555
  rawCall: { rawPrompt, rawSettings },
449
556
  rawResponse: { headers: responseHeaders },
557
+ request: { body: JSON.stringify(body) },
450
558
  response: getResponseMetadata(response),
451
559
  warnings,
452
560
  logprobs: mapOpenAIChatLogProbsOutput(choice.logprobs),
@@ -491,18 +599,19 @@ var OpenAIChatLanguageModel = class {
491
599
  };
492
600
  }
493
601
  const { args, warnings } = this.getArgs(options);
602
+ const body = {
603
+ ...args,
604
+ stream: true,
605
+ // only include stream_options when in strict compatibility mode:
606
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
607
+ };
494
608
  const { responseHeaders, value: response } = await postJsonToApi({
495
609
  url: this.config.url({
496
610
  path: "/chat/completions",
497
611
  modelId: this.modelId
498
612
  }),
499
613
  headers: combineHeaders(this.config.headers(), options.headers),
500
- body: {
501
- ...args,
502
- stream: true,
503
- // only include stream_options when in strict compatibility mode:
504
- stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
505
- },
614
+ body,
506
615
  failedResponseHandler: openaiFailedResponseHandler,
507
616
  successfulResponseHandler: createEventSourceResponseHandler(
508
617
  openaiChatChunkSchema
@@ -679,6 +788,7 @@ var OpenAIChatLanguageModel = class {
679
788
  ),
680
789
  rawCall: { rawPrompt, rawSettings },
681
790
  rawResponse: { headers: responseHeaders },
791
+ request: { body: JSON.stringify(body) },
682
792
  warnings
683
793
  };
684
794
  }
@@ -785,80 +895,6 @@ var openaiChatChunkSchema = z2.union([
785
895
  }),
786
896
  openAIErrorDataSchema
787
897
  ]);
788
- function prepareToolsAndToolChoice({
789
- mode,
790
- useLegacyFunctionCalling = false,
791
- structuredOutputs = false
792
- }) {
793
- var _a;
794
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
795
- if (tools == null) {
796
- return { tools: void 0, tool_choice: void 0 };
797
- }
798
- const toolChoice = mode.toolChoice;
799
- if (useLegacyFunctionCalling) {
800
- const mappedFunctions = tools.map((tool) => ({
801
- name: tool.name,
802
- description: tool.description,
803
- parameters: tool.parameters
804
- }));
805
- if (toolChoice == null) {
806
- return { functions: mappedFunctions, function_call: void 0 };
807
- }
808
- const type2 = toolChoice.type;
809
- switch (type2) {
810
- case "auto":
811
- case "none":
812
- case void 0:
813
- return {
814
- functions: mappedFunctions,
815
- function_call: void 0
816
- };
817
- case "required":
818
- throw new UnsupportedFunctionalityError2({
819
- functionality: "useLegacyFunctionCalling and toolChoice: required"
820
- });
821
- default:
822
- return {
823
- functions: mappedFunctions,
824
- function_call: { name: toolChoice.toolName }
825
- };
826
- }
827
- }
828
- const mappedTools = tools.map((tool) => ({
829
- type: "function",
830
- function: {
831
- name: tool.name,
832
- description: tool.description,
833
- parameters: tool.parameters,
834
- strict: structuredOutputs === true ? true : void 0
835
- }
836
- }));
837
- if (toolChoice == null) {
838
- return { tools: mappedTools, tool_choice: void 0 };
839
- }
840
- const type = toolChoice.type;
841
- switch (type) {
842
- case "auto":
843
- case "none":
844
- case "required":
845
- return { tools: mappedTools, tool_choice: type };
846
- case "tool":
847
- return {
848
- tools: mappedTools,
849
- tool_choice: {
850
- type: "function",
851
- function: {
852
- name: toolChoice.toolName
853
- }
854
- }
855
- };
856
- default: {
857
- const _exhaustiveCheck = type;
858
- throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
859
- }
860
- }
861
- }
862
898
  function isReasoningModel(modelId) {
863
899
  return modelId.startsWith("o1-");
864
900
  }
@@ -868,7 +904,7 @@ function isAudioModel(modelId) {
868
904
 
869
905
  // src/openai-completion-language-model.ts
870
906
  import {
871
- UnsupportedFunctionalityError as UnsupportedFunctionalityError4
907
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError5
872
908
  } from "@ai-sdk/provider";
873
909
  import {
874
910
  combineHeaders as combineHeaders2,
@@ -881,7 +917,7 @@ import { z as z3 } from "zod";
881
917
  // src/convert-to-openai-completion-prompt.ts
882
918
  import {
883
919
  InvalidPromptError,
884
- UnsupportedFunctionalityError as UnsupportedFunctionalityError3
920
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError4
885
921
  } from "@ai-sdk/provider";
886
922
  function convertToOpenAICompletionPrompt({
887
923
  prompt,
@@ -914,7 +950,7 @@ function convertToOpenAICompletionPrompt({
914
950
  return part.text;
915
951
  }
916
952
  case "image": {
917
- throw new UnsupportedFunctionalityError3({
953
+ throw new UnsupportedFunctionalityError4({
918
954
  functionality: "images"
919
955
  });
920
956
  }
@@ -933,7 +969,7 @@ ${userMessage}
933
969
  return part.text;
934
970
  }
935
971
  case "tool-call": {
936
- throw new UnsupportedFunctionalityError3({
972
+ throw new UnsupportedFunctionalityError4({
937
973
  functionality: "tool-call messages"
938
974
  });
939
975
  }
@@ -946,7 +982,7 @@ ${assistantMessage}
946
982
  break;
947
983
  }
948
984
  case "tool": {
949
- throw new UnsupportedFunctionalityError3({
985
+ throw new UnsupportedFunctionalityError4({
950
986
  functionality: "tool messages"
951
987
  });
952
988
  }
@@ -1047,24 +1083,24 @@ var OpenAICompletionLanguageModel = class {
1047
1083
  switch (type) {
1048
1084
  case "regular": {
1049
1085
  if ((_a = mode.tools) == null ? void 0 : _a.length) {
1050
- throw new UnsupportedFunctionalityError4({
1086
+ throw new UnsupportedFunctionalityError5({
1051
1087
  functionality: "tools"
1052
1088
  });
1053
1089
  }
1054
1090
  if (mode.toolChoice) {
1055
- throw new UnsupportedFunctionalityError4({
1091
+ throw new UnsupportedFunctionalityError5({
1056
1092
  functionality: "toolChoice"
1057
1093
  });
1058
1094
  }
1059
1095
  return { args: baseArgs, warnings };
1060
1096
  }
1061
1097
  case "object-json": {
1062
- throw new UnsupportedFunctionalityError4({
1098
+ throw new UnsupportedFunctionalityError5({
1063
1099
  functionality: "object-json mode"
1064
1100
  });
1065
1101
  }
1066
1102
  case "object-tool": {
1067
- throw new UnsupportedFunctionalityError4({
1103
+ throw new UnsupportedFunctionalityError5({
1068
1104
  functionality: "object-tool mode"
1069
1105
  });
1070
1106
  }
@@ -1103,23 +1139,25 @@ var OpenAICompletionLanguageModel = class {
1103
1139
  rawCall: { rawPrompt, rawSettings },
1104
1140
  rawResponse: { headers: responseHeaders },
1105
1141
  response: getResponseMetadata(response),
1106
- warnings
1142
+ warnings,
1143
+ request: { body: JSON.stringify(args) }
1107
1144
  };
1108
1145
  }
1109
1146
  async doStream(options) {
1110
1147
  const { args, warnings } = this.getArgs(options);
1148
+ const body = {
1149
+ ...args,
1150
+ stream: true,
1151
+ // only include stream_options when in strict compatibility mode:
1152
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
1153
+ };
1111
1154
  const { responseHeaders, value: response } = await postJsonToApi2({
1112
1155
  url: this.config.url({
1113
1156
  path: "/completions",
1114
1157
  modelId: this.modelId
1115
1158
  }),
1116
1159
  headers: combineHeaders2(this.config.headers(), options.headers),
1117
- body: {
1118
- ...args,
1119
- stream: true,
1120
- // only include stream_options when in strict compatibility mode:
1121
- stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
1122
- },
1160
+ body,
1123
1161
  failedResponseHandler: openaiFailedResponseHandler,
1124
1162
  successfulResponseHandler: createEventSourceResponseHandler2(
1125
1163
  openaiCompletionChunkSchema
@@ -1193,7 +1231,8 @@ var OpenAICompletionLanguageModel = class {
1193
1231
  ),
1194
1232
  rawCall: { rawPrompt, rawSettings },
1195
1233
  rawResponse: { headers: responseHeaders },
1196
- warnings
1234
+ warnings,
1235
+ request: { body: JSON.stringify(body) }
1197
1236
  };
1198
1237
  }
1199
1238
  };