@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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @ai-sdk/openai
2
2
 
3
+ ## 0.0.70
4
+
5
+ ### Patch Changes
6
+
7
+ - 3b1b69a: feat: provider-defined tools
8
+ - Updated dependencies [aa98cdb]
9
+ - Updated dependencies [1486128]
10
+ - Updated dependencies [7b937c5]
11
+ - Updated dependencies [3b1b69a]
12
+ - Updated dependencies [811a317]
13
+ - @ai-sdk/provider-utils@1.0.22
14
+ - @ai-sdk/provider@0.0.26
15
+
16
+ ## 0.0.69
17
+
18
+ ### Patch Changes
19
+
20
+ - b9b0d7b: feat (ai): access raw request body
21
+ - Updated dependencies [b9b0d7b]
22
+ - @ai-sdk/provider@0.0.25
23
+ - @ai-sdk/provider-utils@1.0.21
24
+
3
25
  ## 0.0.68
4
26
 
5
27
  ### Patch Changes
package/dist/index.js CHANGED
@@ -30,7 +30,7 @@ module.exports = __toCommonJS(src_exports);
30
30
  var import_provider_utils5 = require("@ai-sdk/provider-utils");
31
31
 
32
32
  // src/openai-chat-language-model.ts
33
- var import_provider2 = require("@ai-sdk/provider");
33
+ var import_provider3 = require("@ai-sdk/provider");
34
34
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
35
35
  var import_zod2 = require("zod");
36
36
 
@@ -238,6 +238,105 @@ function getResponseMetadata({
238
238
  };
239
239
  }
240
240
 
241
+ // src/openai-prepare-tools.ts
242
+ var import_provider2 = require("@ai-sdk/provider");
243
+ function prepareTools({
244
+ mode,
245
+ useLegacyFunctionCalling = false,
246
+ structuredOutputs = false
247
+ }) {
248
+ var _a;
249
+ const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
250
+ const toolWarnings = [];
251
+ if (tools == null) {
252
+ return { tools: void 0, tool_choice: void 0, toolWarnings };
253
+ }
254
+ const toolChoice = mode.toolChoice;
255
+ if (useLegacyFunctionCalling) {
256
+ const openaiFunctions = [];
257
+ for (const tool of tools) {
258
+ if (tool.type === "provider-defined") {
259
+ toolWarnings.push({ type: "unsupported-tool", tool });
260
+ } else {
261
+ openaiFunctions.push({
262
+ name: tool.name,
263
+ description: tool.description,
264
+ parameters: tool.parameters
265
+ });
266
+ }
267
+ }
268
+ if (toolChoice == null) {
269
+ return {
270
+ functions: openaiFunctions,
271
+ function_call: void 0,
272
+ toolWarnings
273
+ };
274
+ }
275
+ const type2 = toolChoice.type;
276
+ switch (type2) {
277
+ case "auto":
278
+ case "none":
279
+ case void 0:
280
+ return {
281
+ functions: openaiFunctions,
282
+ function_call: void 0,
283
+ toolWarnings
284
+ };
285
+ case "required":
286
+ throw new import_provider2.UnsupportedFunctionalityError({
287
+ functionality: "useLegacyFunctionCalling and toolChoice: required"
288
+ });
289
+ default:
290
+ return {
291
+ functions: openaiFunctions,
292
+ function_call: { name: toolChoice.toolName },
293
+ toolWarnings
294
+ };
295
+ }
296
+ }
297
+ const openaiTools = [];
298
+ for (const tool of tools) {
299
+ if (tool.type === "provider-defined") {
300
+ toolWarnings.push({ type: "unsupported-tool", tool });
301
+ } else {
302
+ openaiTools.push({
303
+ type: "function",
304
+ function: {
305
+ name: tool.name,
306
+ description: tool.description,
307
+ parameters: tool.parameters,
308
+ strict: structuredOutputs === true ? true : void 0
309
+ }
310
+ });
311
+ }
312
+ }
313
+ if (toolChoice == null) {
314
+ return { tools: openaiTools, tool_choice: void 0, toolWarnings };
315
+ }
316
+ const type = toolChoice.type;
317
+ switch (type) {
318
+ case "auto":
319
+ case "none":
320
+ case "required":
321
+ return { tools: openaiTools, tool_choice: type, toolWarnings };
322
+ case "tool":
323
+ return {
324
+ tools: openaiTools,
325
+ tool_choice: {
326
+ type: "function",
327
+ function: {
328
+ name: toolChoice.toolName
329
+ }
330
+ },
331
+ toolWarnings
332
+ };
333
+ default: {
334
+ const _exhaustiveCheck = type;
335
+ throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
336
+ }
337
+ }
338
+ }
339
+
241
340
  // src/openai-chat-language-model.ts
242
341
  var OpenAIChatLanguageModel = class {
243
342
  constructor(modelId, settings, config) {
@@ -293,12 +392,12 @@ var OpenAIChatLanguageModel = class {
293
392
  }
294
393
  const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
295
394
  if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
296
- throw new import_provider2.UnsupportedFunctionalityError({
395
+ throw new import_provider3.UnsupportedFunctionalityError({
297
396
  functionality: "useLegacyFunctionCalling with parallelToolCalls"
298
397
  });
299
398
  }
300
399
  if (useLegacyFunctionCalling && this.settings.structuredOutputs === true) {
301
- throw new import_provider2.UnsupportedFunctionalityError({
400
+ throw new import_provider3.UnsupportedFunctionalityError({
302
401
  functionality: "structuredOutputs with useLegacyFunctionCalling"
303
402
  });
304
403
  }
@@ -339,16 +438,20 @@ var OpenAIChatLanguageModel = class {
339
438
  }
340
439
  switch (type) {
341
440
  case "regular": {
441
+ const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({
442
+ mode,
443
+ useLegacyFunctionCalling,
444
+ structuredOutputs: this.settings.structuredOutputs
445
+ });
342
446
  return {
343
447
  args: {
344
448
  ...baseArgs,
345
- ...prepareToolsAndToolChoice({
346
- mode,
347
- useLegacyFunctionCalling,
348
- structuredOutputs: this.settings.structuredOutputs
349
- })
449
+ tools,
450
+ tool_choice,
451
+ functions,
452
+ function_call
350
453
  },
351
- warnings
454
+ warnings: [...warnings, ...toolWarnings]
352
455
  };
353
456
  }
354
457
  case "object-json": {
@@ -411,14 +514,14 @@ var OpenAIChatLanguageModel = class {
411
514
  }
412
515
  async doGenerate(options) {
413
516
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
414
- const { args, warnings } = this.getArgs(options);
517
+ const { args: body, warnings } = this.getArgs(options);
415
518
  const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
416
519
  url: this.config.url({
417
520
  path: "/chat/completions",
418
521
  modelId: this.modelId
419
522
  }),
420
523
  headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
421
- body: args,
524
+ body,
422
525
  failedResponseHandler: openaiFailedResponseHandler,
423
526
  successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
424
527
  openAIChatResponseSchema
@@ -426,7 +529,7 @@ var OpenAIChatLanguageModel = class {
426
529
  abortSignal: options.abortSignal,
427
530
  fetch: this.config.fetch
428
531
  });
429
- const { messages: rawPrompt, ...rawSettings } = args;
532
+ const { messages: rawPrompt, ...rawSettings } = body;
430
533
  const choice = response.choices[0];
431
534
  let providerMetadata;
432
535
  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) {
@@ -463,6 +566,7 @@ var OpenAIChatLanguageModel = class {
463
566
  },
464
567
  rawCall: { rawPrompt, rawSettings },
465
568
  rawResponse: { headers: responseHeaders },
569
+ request: { body: JSON.stringify(body) },
466
570
  response: getResponseMetadata(response),
467
571
  warnings,
468
572
  logprobs: mapOpenAIChatLogProbsOutput(choice.logprobs),
@@ -507,18 +611,19 @@ var OpenAIChatLanguageModel = class {
507
611
  };
508
612
  }
509
613
  const { args, warnings } = this.getArgs(options);
614
+ const body = {
615
+ ...args,
616
+ stream: true,
617
+ // only include stream_options when in strict compatibility mode:
618
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
619
+ };
510
620
  const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
511
621
  url: this.config.url({
512
622
  path: "/chat/completions",
513
623
  modelId: this.modelId
514
624
  }),
515
625
  headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
516
- body: {
517
- ...args,
518
- stream: true,
519
- // only include stream_options when in strict compatibility mode:
520
- stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
521
- },
626
+ body,
522
627
  failedResponseHandler: openaiFailedResponseHandler,
523
628
  successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
524
629
  openaiChatChunkSchema
@@ -607,19 +712,19 @@ var OpenAIChatLanguageModel = class {
607
712
  const index = toolCallDelta.index;
608
713
  if (toolCalls[index] == null) {
609
714
  if (toolCallDelta.type !== "function") {
610
- throw new import_provider2.InvalidResponseDataError({
715
+ throw new import_provider3.InvalidResponseDataError({
611
716
  data: toolCallDelta,
612
717
  message: `Expected 'function' type.`
613
718
  });
614
719
  }
615
720
  if (toolCallDelta.id == null) {
616
- throw new import_provider2.InvalidResponseDataError({
721
+ throw new import_provider3.InvalidResponseDataError({
617
722
  data: toolCallDelta,
618
723
  message: `Expected 'id' to be a string.`
619
724
  });
620
725
  }
621
726
  if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
622
- throw new import_provider2.InvalidResponseDataError({
727
+ throw new import_provider3.InvalidResponseDataError({
623
728
  data: toolCallDelta,
624
729
  message: `Expected 'function.name' to be a string.`
625
730
  });
@@ -695,6 +800,7 @@ var OpenAIChatLanguageModel = class {
695
800
  ),
696
801
  rawCall: { rawPrompt, rawSettings },
697
802
  rawResponse: { headers: responseHeaders },
803
+ request: { body: JSON.stringify(body) },
698
804
  warnings
699
805
  };
700
806
  }
@@ -801,80 +907,6 @@ var openaiChatChunkSchema = import_zod2.z.union([
801
907
  }),
802
908
  openAIErrorDataSchema
803
909
  ]);
804
- function prepareToolsAndToolChoice({
805
- mode,
806
- useLegacyFunctionCalling = false,
807
- structuredOutputs = false
808
- }) {
809
- var _a;
810
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
811
- if (tools == null) {
812
- return { tools: void 0, tool_choice: void 0 };
813
- }
814
- const toolChoice = mode.toolChoice;
815
- if (useLegacyFunctionCalling) {
816
- const mappedFunctions = tools.map((tool) => ({
817
- name: tool.name,
818
- description: tool.description,
819
- parameters: tool.parameters
820
- }));
821
- if (toolChoice == null) {
822
- return { functions: mappedFunctions, function_call: void 0 };
823
- }
824
- const type2 = toolChoice.type;
825
- switch (type2) {
826
- case "auto":
827
- case "none":
828
- case void 0:
829
- return {
830
- functions: mappedFunctions,
831
- function_call: void 0
832
- };
833
- case "required":
834
- throw new import_provider2.UnsupportedFunctionalityError({
835
- functionality: "useLegacyFunctionCalling and toolChoice: required"
836
- });
837
- default:
838
- return {
839
- functions: mappedFunctions,
840
- function_call: { name: toolChoice.toolName }
841
- };
842
- }
843
- }
844
- const mappedTools = tools.map((tool) => ({
845
- type: "function",
846
- function: {
847
- name: tool.name,
848
- description: tool.description,
849
- parameters: tool.parameters,
850
- strict: structuredOutputs === true ? true : void 0
851
- }
852
- }));
853
- if (toolChoice == null) {
854
- return { tools: mappedTools, tool_choice: void 0 };
855
- }
856
- const type = toolChoice.type;
857
- switch (type) {
858
- case "auto":
859
- case "none":
860
- case "required":
861
- return { tools: mappedTools, tool_choice: type };
862
- case "tool":
863
- return {
864
- tools: mappedTools,
865
- tool_choice: {
866
- type: "function",
867
- function: {
868
- name: toolChoice.toolName
869
- }
870
- }
871
- };
872
- default: {
873
- const _exhaustiveCheck = type;
874
- throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
875
- }
876
- }
877
- }
878
910
  function isReasoningModel(modelId) {
879
911
  return modelId.startsWith("o1-");
880
912
  }
@@ -883,12 +915,12 @@ function isAudioModel(modelId) {
883
915
  }
884
916
 
885
917
  // src/openai-completion-language-model.ts
886
- var import_provider4 = require("@ai-sdk/provider");
918
+ var import_provider5 = require("@ai-sdk/provider");
887
919
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
888
920
  var import_zod3 = require("zod");
889
921
 
890
922
  // src/convert-to-openai-completion-prompt.ts
891
- var import_provider3 = require("@ai-sdk/provider");
923
+ var import_provider4 = require("@ai-sdk/provider");
892
924
  function convertToOpenAICompletionPrompt({
893
925
  prompt,
894
926
  inputFormat,
@@ -908,7 +940,7 @@ function convertToOpenAICompletionPrompt({
908
940
  for (const { role, content } of prompt) {
909
941
  switch (role) {
910
942
  case "system": {
911
- throw new import_provider3.InvalidPromptError({
943
+ throw new import_provider4.InvalidPromptError({
912
944
  message: "Unexpected system message in prompt: ${content}",
913
945
  prompt
914
946
  });
@@ -920,7 +952,7 @@ function convertToOpenAICompletionPrompt({
920
952
  return part.text;
921
953
  }
922
954
  case "image": {
923
- throw new import_provider3.UnsupportedFunctionalityError({
955
+ throw new import_provider4.UnsupportedFunctionalityError({
924
956
  functionality: "images"
925
957
  });
926
958
  }
@@ -939,7 +971,7 @@ ${userMessage}
939
971
  return part.text;
940
972
  }
941
973
  case "tool-call": {
942
- throw new import_provider3.UnsupportedFunctionalityError({
974
+ throw new import_provider4.UnsupportedFunctionalityError({
943
975
  functionality: "tool-call messages"
944
976
  });
945
977
  }
@@ -952,7 +984,7 @@ ${assistantMessage}
952
984
  break;
953
985
  }
954
986
  case "tool": {
955
- throw new import_provider3.UnsupportedFunctionalityError({
987
+ throw new import_provider4.UnsupportedFunctionalityError({
956
988
  functionality: "tool messages"
957
989
  });
958
990
  }
@@ -1053,24 +1085,24 @@ var OpenAICompletionLanguageModel = class {
1053
1085
  switch (type) {
1054
1086
  case "regular": {
1055
1087
  if ((_a = mode.tools) == null ? void 0 : _a.length) {
1056
- throw new import_provider4.UnsupportedFunctionalityError({
1088
+ throw new import_provider5.UnsupportedFunctionalityError({
1057
1089
  functionality: "tools"
1058
1090
  });
1059
1091
  }
1060
1092
  if (mode.toolChoice) {
1061
- throw new import_provider4.UnsupportedFunctionalityError({
1093
+ throw new import_provider5.UnsupportedFunctionalityError({
1062
1094
  functionality: "toolChoice"
1063
1095
  });
1064
1096
  }
1065
1097
  return { args: baseArgs, warnings };
1066
1098
  }
1067
1099
  case "object-json": {
1068
- throw new import_provider4.UnsupportedFunctionalityError({
1100
+ throw new import_provider5.UnsupportedFunctionalityError({
1069
1101
  functionality: "object-json mode"
1070
1102
  });
1071
1103
  }
1072
1104
  case "object-tool": {
1073
- throw new import_provider4.UnsupportedFunctionalityError({
1105
+ throw new import_provider5.UnsupportedFunctionalityError({
1074
1106
  functionality: "object-tool mode"
1075
1107
  });
1076
1108
  }
@@ -1109,23 +1141,25 @@ var OpenAICompletionLanguageModel = class {
1109
1141
  rawCall: { rawPrompt, rawSettings },
1110
1142
  rawResponse: { headers: responseHeaders },
1111
1143
  response: getResponseMetadata(response),
1112
- warnings
1144
+ warnings,
1145
+ request: { body: JSON.stringify(args) }
1113
1146
  };
1114
1147
  }
1115
1148
  async doStream(options) {
1116
1149
  const { args, warnings } = this.getArgs(options);
1150
+ const body = {
1151
+ ...args,
1152
+ stream: true,
1153
+ // only include stream_options when in strict compatibility mode:
1154
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
1155
+ };
1117
1156
  const { responseHeaders, value: response } = await (0, import_provider_utils4.postJsonToApi)({
1118
1157
  url: this.config.url({
1119
1158
  path: "/completions",
1120
1159
  modelId: this.modelId
1121
1160
  }),
1122
1161
  headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), options.headers),
1123
- body: {
1124
- ...args,
1125
- stream: true,
1126
- // only include stream_options when in strict compatibility mode:
1127
- stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
1128
- },
1162
+ body,
1129
1163
  failedResponseHandler: openaiFailedResponseHandler,
1130
1164
  successfulResponseHandler: (0, import_provider_utils4.createEventSourceResponseHandler)(
1131
1165
  openaiCompletionChunkSchema
@@ -1199,7 +1233,8 @@ var OpenAICompletionLanguageModel = class {
1199
1233
  ),
1200
1234
  rawCall: { rawPrompt, rawSettings },
1201
1235
  rawResponse: { headers: responseHeaders },
1202
- warnings
1236
+ warnings,
1237
+ request: { body: JSON.stringify(body) }
1203
1238
  };
1204
1239
  }
1205
1240
  };
@@ -1299,7 +1334,7 @@ var OpenAI = class {
1299
1334
  var import_provider_utils7 = require("@ai-sdk/provider-utils");
1300
1335
 
1301
1336
  // src/openai-embedding-model.ts
1302
- var import_provider5 = require("@ai-sdk/provider");
1337
+ var import_provider6 = require("@ai-sdk/provider");
1303
1338
  var import_provider_utils6 = require("@ai-sdk/provider-utils");
1304
1339
  var import_zod4 = require("zod");
1305
1340
  var OpenAIEmbeddingModel = class {
@@ -1326,7 +1361,7 @@ var OpenAIEmbeddingModel = class {
1326
1361
  abortSignal
1327
1362
  }) {
1328
1363
  if (values.length > this.maxEmbeddingsPerCall) {
1329
- throw new import_provider5.TooManyEmbeddingValuesForCallError({
1364
+ throw new import_provider6.TooManyEmbeddingValuesForCallError({
1330
1365
  provider: this.provider,
1331
1366
  modelId: this.modelId,
1332
1367
  maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,