@ai-sdk/openai 0.0.68 → 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.
@@ -27,7 +27,7 @@ __export(internal_exports, {
27
27
  module.exports = __toCommonJS(internal_exports);
28
28
 
29
29
  // src/openai-chat-language-model.ts
30
- var import_provider2 = require("@ai-sdk/provider");
30
+ var import_provider3 = require("@ai-sdk/provider");
31
31
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
32
32
  var import_zod2 = require("zod");
33
33
 
@@ -235,6 +235,105 @@ function getResponseMetadata({
235
235
  };
236
236
  }
237
237
 
238
+ // src/openai-prepare-tools.ts
239
+ var import_provider2 = require("@ai-sdk/provider");
240
+ function prepareTools({
241
+ mode,
242
+ useLegacyFunctionCalling = false,
243
+ structuredOutputs = false
244
+ }) {
245
+ var _a;
246
+ const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
247
+ const toolWarnings = [];
248
+ if (tools == null) {
249
+ return { tools: void 0, tool_choice: void 0, toolWarnings };
250
+ }
251
+ const toolChoice = mode.toolChoice;
252
+ if (useLegacyFunctionCalling) {
253
+ const openaiFunctions = [];
254
+ for (const tool of tools) {
255
+ if (tool.type === "provider-defined") {
256
+ toolWarnings.push({ type: "unsupported-tool", tool });
257
+ } else {
258
+ openaiFunctions.push({
259
+ name: tool.name,
260
+ description: tool.description,
261
+ parameters: tool.parameters
262
+ });
263
+ }
264
+ }
265
+ if (toolChoice == null) {
266
+ return {
267
+ functions: openaiFunctions,
268
+ function_call: void 0,
269
+ toolWarnings
270
+ };
271
+ }
272
+ const type2 = toolChoice.type;
273
+ switch (type2) {
274
+ case "auto":
275
+ case "none":
276
+ case void 0:
277
+ return {
278
+ functions: openaiFunctions,
279
+ function_call: void 0,
280
+ toolWarnings
281
+ };
282
+ case "required":
283
+ throw new import_provider2.UnsupportedFunctionalityError({
284
+ functionality: "useLegacyFunctionCalling and toolChoice: required"
285
+ });
286
+ default:
287
+ return {
288
+ functions: openaiFunctions,
289
+ function_call: { name: toolChoice.toolName },
290
+ toolWarnings
291
+ };
292
+ }
293
+ }
294
+ const openaiTools = [];
295
+ for (const tool of tools) {
296
+ if (tool.type === "provider-defined") {
297
+ toolWarnings.push({ type: "unsupported-tool", tool });
298
+ } else {
299
+ openaiTools.push({
300
+ type: "function",
301
+ function: {
302
+ name: tool.name,
303
+ description: tool.description,
304
+ parameters: tool.parameters,
305
+ strict: structuredOutputs === true ? true : void 0
306
+ }
307
+ });
308
+ }
309
+ }
310
+ if (toolChoice == null) {
311
+ return { tools: openaiTools, tool_choice: void 0, toolWarnings };
312
+ }
313
+ const type = toolChoice.type;
314
+ switch (type) {
315
+ case "auto":
316
+ case "none":
317
+ case "required":
318
+ return { tools: openaiTools, tool_choice: type, toolWarnings };
319
+ case "tool":
320
+ return {
321
+ tools: openaiTools,
322
+ tool_choice: {
323
+ type: "function",
324
+ function: {
325
+ name: toolChoice.toolName
326
+ }
327
+ },
328
+ toolWarnings
329
+ };
330
+ default: {
331
+ const _exhaustiveCheck = type;
332
+ throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
333
+ }
334
+ }
335
+ }
336
+
238
337
  // src/openai-chat-language-model.ts
239
338
  var OpenAIChatLanguageModel = class {
240
339
  constructor(modelId, settings, config) {
@@ -290,12 +389,12 @@ var OpenAIChatLanguageModel = class {
290
389
  }
291
390
  const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
292
391
  if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
293
- throw new import_provider2.UnsupportedFunctionalityError({
392
+ throw new import_provider3.UnsupportedFunctionalityError({
294
393
  functionality: "useLegacyFunctionCalling with parallelToolCalls"
295
394
  });
296
395
  }
297
396
  if (useLegacyFunctionCalling && this.settings.structuredOutputs === true) {
298
- throw new import_provider2.UnsupportedFunctionalityError({
397
+ throw new import_provider3.UnsupportedFunctionalityError({
299
398
  functionality: "structuredOutputs with useLegacyFunctionCalling"
300
399
  });
301
400
  }
@@ -336,16 +435,20 @@ var OpenAIChatLanguageModel = class {
336
435
  }
337
436
  switch (type) {
338
437
  case "regular": {
438
+ const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({
439
+ mode,
440
+ useLegacyFunctionCalling,
441
+ structuredOutputs: this.settings.structuredOutputs
442
+ });
339
443
  return {
340
444
  args: {
341
445
  ...baseArgs,
342
- ...prepareToolsAndToolChoice({
343
- mode,
344
- useLegacyFunctionCalling,
345
- structuredOutputs: this.settings.structuredOutputs
346
- })
446
+ tools,
447
+ tool_choice,
448
+ functions,
449
+ function_call
347
450
  },
348
- warnings
451
+ warnings: [...warnings, ...toolWarnings]
349
452
  };
350
453
  }
351
454
  case "object-json": {
@@ -408,14 +511,14 @@ var OpenAIChatLanguageModel = class {
408
511
  }
409
512
  async doGenerate(options) {
410
513
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
411
- const { args, warnings } = this.getArgs(options);
514
+ const { args: body, warnings } = this.getArgs(options);
412
515
  const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
413
516
  url: this.config.url({
414
517
  path: "/chat/completions",
415
518
  modelId: this.modelId
416
519
  }),
417
520
  headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
418
- body: args,
521
+ body,
419
522
  failedResponseHandler: openaiFailedResponseHandler,
420
523
  successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
421
524
  openAIChatResponseSchema
@@ -423,7 +526,7 @@ var OpenAIChatLanguageModel = class {
423
526
  abortSignal: options.abortSignal,
424
527
  fetch: this.config.fetch
425
528
  });
426
- const { messages: rawPrompt, ...rawSettings } = args;
529
+ const { messages: rawPrompt, ...rawSettings } = body;
427
530
  const choice = response.choices[0];
428
531
  let providerMetadata;
429
532
  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) {
@@ -460,6 +563,7 @@ var OpenAIChatLanguageModel = class {
460
563
  },
461
564
  rawCall: { rawPrompt, rawSettings },
462
565
  rawResponse: { headers: responseHeaders },
566
+ request: { body: JSON.stringify(body) },
463
567
  response: getResponseMetadata(response),
464
568
  warnings,
465
569
  logprobs: mapOpenAIChatLogProbsOutput(choice.logprobs),
@@ -504,18 +608,19 @@ var OpenAIChatLanguageModel = class {
504
608
  };
505
609
  }
506
610
  const { args, warnings } = this.getArgs(options);
611
+ const body = {
612
+ ...args,
613
+ stream: true,
614
+ // only include stream_options when in strict compatibility mode:
615
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
616
+ };
507
617
  const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
508
618
  url: this.config.url({
509
619
  path: "/chat/completions",
510
620
  modelId: this.modelId
511
621
  }),
512
622
  headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
513
- body: {
514
- ...args,
515
- stream: true,
516
- // only include stream_options when in strict compatibility mode:
517
- stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
518
- },
623
+ body,
519
624
  failedResponseHandler: openaiFailedResponseHandler,
520
625
  successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
521
626
  openaiChatChunkSchema
@@ -604,19 +709,19 @@ var OpenAIChatLanguageModel = class {
604
709
  const index = toolCallDelta.index;
605
710
  if (toolCalls[index] == null) {
606
711
  if (toolCallDelta.type !== "function") {
607
- throw new import_provider2.InvalidResponseDataError({
712
+ throw new import_provider3.InvalidResponseDataError({
608
713
  data: toolCallDelta,
609
714
  message: `Expected 'function' type.`
610
715
  });
611
716
  }
612
717
  if (toolCallDelta.id == null) {
613
- throw new import_provider2.InvalidResponseDataError({
718
+ throw new import_provider3.InvalidResponseDataError({
614
719
  data: toolCallDelta,
615
720
  message: `Expected 'id' to be a string.`
616
721
  });
617
722
  }
618
723
  if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
619
- throw new import_provider2.InvalidResponseDataError({
724
+ throw new import_provider3.InvalidResponseDataError({
620
725
  data: toolCallDelta,
621
726
  message: `Expected 'function.name' to be a string.`
622
727
  });
@@ -692,6 +797,7 @@ var OpenAIChatLanguageModel = class {
692
797
  ),
693
798
  rawCall: { rawPrompt, rawSettings },
694
799
  rawResponse: { headers: responseHeaders },
800
+ request: { body: JSON.stringify(body) },
695
801
  warnings
696
802
  };
697
803
  }
@@ -798,80 +904,6 @@ var openaiChatChunkSchema = import_zod2.z.union([
798
904
  }),
799
905
  openAIErrorDataSchema
800
906
  ]);
801
- function prepareToolsAndToolChoice({
802
- mode,
803
- useLegacyFunctionCalling = false,
804
- structuredOutputs = false
805
- }) {
806
- var _a;
807
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
808
- if (tools == null) {
809
- return { tools: void 0, tool_choice: void 0 };
810
- }
811
- const toolChoice = mode.toolChoice;
812
- if (useLegacyFunctionCalling) {
813
- const mappedFunctions = tools.map((tool) => ({
814
- name: tool.name,
815
- description: tool.description,
816
- parameters: tool.parameters
817
- }));
818
- if (toolChoice == null) {
819
- return { functions: mappedFunctions, function_call: void 0 };
820
- }
821
- const type2 = toolChoice.type;
822
- switch (type2) {
823
- case "auto":
824
- case "none":
825
- case void 0:
826
- return {
827
- functions: mappedFunctions,
828
- function_call: void 0
829
- };
830
- case "required":
831
- throw new import_provider2.UnsupportedFunctionalityError({
832
- functionality: "useLegacyFunctionCalling and toolChoice: required"
833
- });
834
- default:
835
- return {
836
- functions: mappedFunctions,
837
- function_call: { name: toolChoice.toolName }
838
- };
839
- }
840
- }
841
- const mappedTools = tools.map((tool) => ({
842
- type: "function",
843
- function: {
844
- name: tool.name,
845
- description: tool.description,
846
- parameters: tool.parameters,
847
- strict: structuredOutputs === true ? true : void 0
848
- }
849
- }));
850
- if (toolChoice == null) {
851
- return { tools: mappedTools, tool_choice: void 0 };
852
- }
853
- const type = toolChoice.type;
854
- switch (type) {
855
- case "auto":
856
- case "none":
857
- case "required":
858
- return { tools: mappedTools, tool_choice: type };
859
- case "tool":
860
- return {
861
- tools: mappedTools,
862
- tool_choice: {
863
- type: "function",
864
- function: {
865
- name: toolChoice.toolName
866
- }
867
- }
868
- };
869
- default: {
870
- const _exhaustiveCheck = type;
871
- throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
872
- }
873
- }
874
- }
875
907
  function isReasoningModel(modelId) {
876
908
  return modelId.startsWith("o1-");
877
909
  }
@@ -880,12 +912,12 @@ function isAudioModel(modelId) {
880
912
  }
881
913
 
882
914
  // src/openai-completion-language-model.ts
883
- var import_provider4 = require("@ai-sdk/provider");
915
+ var import_provider5 = require("@ai-sdk/provider");
884
916
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
885
917
  var import_zod3 = require("zod");
886
918
 
887
919
  // src/convert-to-openai-completion-prompt.ts
888
- var import_provider3 = require("@ai-sdk/provider");
920
+ var import_provider4 = require("@ai-sdk/provider");
889
921
  function convertToOpenAICompletionPrompt({
890
922
  prompt,
891
923
  inputFormat,
@@ -905,7 +937,7 @@ function convertToOpenAICompletionPrompt({
905
937
  for (const { role, content } of prompt) {
906
938
  switch (role) {
907
939
  case "system": {
908
- throw new import_provider3.InvalidPromptError({
940
+ throw new import_provider4.InvalidPromptError({
909
941
  message: "Unexpected system message in prompt: ${content}",
910
942
  prompt
911
943
  });
@@ -917,7 +949,7 @@ function convertToOpenAICompletionPrompt({
917
949
  return part.text;
918
950
  }
919
951
  case "image": {
920
- throw new import_provider3.UnsupportedFunctionalityError({
952
+ throw new import_provider4.UnsupportedFunctionalityError({
921
953
  functionality: "images"
922
954
  });
923
955
  }
@@ -936,7 +968,7 @@ ${userMessage}
936
968
  return part.text;
937
969
  }
938
970
  case "tool-call": {
939
- throw new import_provider3.UnsupportedFunctionalityError({
971
+ throw new import_provider4.UnsupportedFunctionalityError({
940
972
  functionality: "tool-call messages"
941
973
  });
942
974
  }
@@ -949,7 +981,7 @@ ${assistantMessage}
949
981
  break;
950
982
  }
951
983
  case "tool": {
952
- throw new import_provider3.UnsupportedFunctionalityError({
984
+ throw new import_provider4.UnsupportedFunctionalityError({
953
985
  functionality: "tool messages"
954
986
  });
955
987
  }
@@ -1050,24 +1082,24 @@ var OpenAICompletionLanguageModel = class {
1050
1082
  switch (type) {
1051
1083
  case "regular": {
1052
1084
  if ((_a = mode.tools) == null ? void 0 : _a.length) {
1053
- throw new import_provider4.UnsupportedFunctionalityError({
1085
+ throw new import_provider5.UnsupportedFunctionalityError({
1054
1086
  functionality: "tools"
1055
1087
  });
1056
1088
  }
1057
1089
  if (mode.toolChoice) {
1058
- throw new import_provider4.UnsupportedFunctionalityError({
1090
+ throw new import_provider5.UnsupportedFunctionalityError({
1059
1091
  functionality: "toolChoice"
1060
1092
  });
1061
1093
  }
1062
1094
  return { args: baseArgs, warnings };
1063
1095
  }
1064
1096
  case "object-json": {
1065
- throw new import_provider4.UnsupportedFunctionalityError({
1097
+ throw new import_provider5.UnsupportedFunctionalityError({
1066
1098
  functionality: "object-json mode"
1067
1099
  });
1068
1100
  }
1069
1101
  case "object-tool": {
1070
- throw new import_provider4.UnsupportedFunctionalityError({
1102
+ throw new import_provider5.UnsupportedFunctionalityError({
1071
1103
  functionality: "object-tool mode"
1072
1104
  });
1073
1105
  }
@@ -1106,23 +1138,25 @@ var OpenAICompletionLanguageModel = class {
1106
1138
  rawCall: { rawPrompt, rawSettings },
1107
1139
  rawResponse: { headers: responseHeaders },
1108
1140
  response: getResponseMetadata(response),
1109
- warnings
1141
+ warnings,
1142
+ request: { body: JSON.stringify(args) }
1110
1143
  };
1111
1144
  }
1112
1145
  async doStream(options) {
1113
1146
  const { args, warnings } = this.getArgs(options);
1147
+ const body = {
1148
+ ...args,
1149
+ stream: true,
1150
+ // only include stream_options when in strict compatibility mode:
1151
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
1152
+ };
1114
1153
  const { responseHeaders, value: response } = await (0, import_provider_utils4.postJsonToApi)({
1115
1154
  url: this.config.url({
1116
1155
  path: "/completions",
1117
1156
  modelId: this.modelId
1118
1157
  }),
1119
1158
  headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), options.headers),
1120
- body: {
1121
- ...args,
1122
- stream: true,
1123
- // only include stream_options when in strict compatibility mode:
1124
- stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
1125
- },
1159
+ body,
1126
1160
  failedResponseHandler: openaiFailedResponseHandler,
1127
1161
  successfulResponseHandler: (0, import_provider_utils4.createEventSourceResponseHandler)(
1128
1162
  openaiCompletionChunkSchema
@@ -1196,7 +1230,8 @@ var OpenAICompletionLanguageModel = class {
1196
1230
  ),
1197
1231
  rawCall: { rawPrompt, rawSettings },
1198
1232
  rawResponse: { headers: responseHeaders },
1199
- warnings
1233
+ warnings,
1234
+ request: { body: JSON.stringify(body) }
1200
1235
  };
1201
1236
  }
1202
1237
  };
@@ -1246,7 +1281,7 @@ var openaiCompletionChunkSchema = import_zod3.z.union([
1246
1281
  ]);
1247
1282
 
1248
1283
  // src/openai-embedding-model.ts
1249
- var import_provider5 = require("@ai-sdk/provider");
1284
+ var import_provider6 = require("@ai-sdk/provider");
1250
1285
  var import_provider_utils5 = require("@ai-sdk/provider-utils");
1251
1286
  var import_zod4 = require("zod");
1252
1287
  var OpenAIEmbeddingModel = class {
@@ -1273,7 +1308,7 @@ var OpenAIEmbeddingModel = class {
1273
1308
  abortSignal
1274
1309
  }) {
1275
1310
  if (values.length > this.maxEmbeddingsPerCall) {
1276
- throw new import_provider5.TooManyEmbeddingValuesForCallError({
1311
+ throw new import_provider6.TooManyEmbeddingValuesForCallError({
1277
1312
  provider: this.provider,
1278
1313
  modelId: this.modelId,
1279
1314
  maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,