@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/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # @ai-sdk/openai
2
2
 
3
+ ## 0.0.71
4
+
5
+ ### Patch Changes
6
+
7
+ - 54a3a59: fix (provider/openai): support object-json mode without schema
8
+
9
+ ## 0.0.70
10
+
11
+ ### Patch Changes
12
+
13
+ - 3b1b69a: feat: provider-defined tools
14
+ - Updated dependencies [aa98cdb]
15
+ - Updated dependencies [1486128]
16
+ - Updated dependencies [7b937c5]
17
+ - Updated dependencies [3b1b69a]
18
+ - Updated dependencies [811a317]
19
+ - @ai-sdk/provider-utils@1.0.22
20
+ - @ai-sdk/provider@0.0.26
21
+
22
+ ## 0.0.69
23
+
24
+ ### Patch Changes
25
+
26
+ - b9b0d7b: feat (ai): access raw request body
27
+ - Updated dependencies [b9b0d7b]
28
+ - @ai-sdk/provider@0.0.25
29
+ - @ai-sdk/provider-utils@1.0.21
30
+
3
31
  ## 0.0.68
4
32
 
5
33
  ### 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,107 @@ 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 import_provider2.UnsupportedFunctionalityError({
336
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
337
+ });
338
+ }
339
+ }
340
+ }
341
+
241
342
  // src/openai-chat-language-model.ts
242
343
  var OpenAIChatLanguageModel = class {
243
344
  constructor(modelId, settings, config) {
@@ -293,12 +394,12 @@ var OpenAIChatLanguageModel = class {
293
394
  }
294
395
  const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
295
396
  if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
296
- throw new import_provider2.UnsupportedFunctionalityError({
397
+ throw new import_provider3.UnsupportedFunctionalityError({
297
398
  functionality: "useLegacyFunctionCalling with parallelToolCalls"
298
399
  });
299
400
  }
300
401
  if (useLegacyFunctionCalling && this.settings.structuredOutputs === true) {
301
- throw new import_provider2.UnsupportedFunctionalityError({
402
+ throw new import_provider3.UnsupportedFunctionalityError({
302
403
  functionality: "structuredOutputs with useLegacyFunctionCalling"
303
404
  });
304
405
  }
@@ -339,23 +440,27 @@ var OpenAIChatLanguageModel = class {
339
440
  }
340
441
  switch (type) {
341
442
  case "regular": {
443
+ const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({
444
+ mode,
445
+ useLegacyFunctionCalling,
446
+ structuredOutputs: this.settings.structuredOutputs
447
+ });
342
448
  return {
343
449
  args: {
344
450
  ...baseArgs,
345
- ...prepareToolsAndToolChoice({
346
- mode,
347
- useLegacyFunctionCalling,
348
- structuredOutputs: this.settings.structuredOutputs
349
- })
451
+ tools,
452
+ tool_choice,
453
+ functions,
454
+ function_call
350
455
  },
351
- warnings
456
+ warnings: [...warnings, ...toolWarnings]
352
457
  };
353
458
  }
354
459
  case "object-json": {
355
460
  return {
356
461
  args: {
357
462
  ...baseArgs,
358
- response_format: this.settings.structuredOutputs === true ? {
463
+ response_format: this.settings.structuredOutputs === true && mode.schema != null ? {
359
464
  type: "json_schema",
360
465
  json_schema: {
361
466
  schema: mode.schema,
@@ -411,14 +516,14 @@ var OpenAIChatLanguageModel = class {
411
516
  }
412
517
  async doGenerate(options) {
413
518
  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);
519
+ const { args: body, warnings } = this.getArgs(options);
415
520
  const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
416
521
  url: this.config.url({
417
522
  path: "/chat/completions",
418
523
  modelId: this.modelId
419
524
  }),
420
525
  headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
421
- body: args,
526
+ body,
422
527
  failedResponseHandler: openaiFailedResponseHandler,
423
528
  successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
424
529
  openAIChatResponseSchema
@@ -426,7 +531,7 @@ var OpenAIChatLanguageModel = class {
426
531
  abortSignal: options.abortSignal,
427
532
  fetch: this.config.fetch
428
533
  });
429
- const { messages: rawPrompt, ...rawSettings } = args;
534
+ const { messages: rawPrompt, ...rawSettings } = body;
430
535
  const choice = response.choices[0];
431
536
  let providerMetadata;
432
537
  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 +568,7 @@ var OpenAIChatLanguageModel = class {
463
568
  },
464
569
  rawCall: { rawPrompt, rawSettings },
465
570
  rawResponse: { headers: responseHeaders },
571
+ request: { body: JSON.stringify(body) },
466
572
  response: getResponseMetadata(response),
467
573
  warnings,
468
574
  logprobs: mapOpenAIChatLogProbsOutput(choice.logprobs),
@@ -507,18 +613,19 @@ var OpenAIChatLanguageModel = class {
507
613
  };
508
614
  }
509
615
  const { args, warnings } = this.getArgs(options);
616
+ const body = {
617
+ ...args,
618
+ stream: true,
619
+ // only include stream_options when in strict compatibility mode:
620
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
621
+ };
510
622
  const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
511
623
  url: this.config.url({
512
624
  path: "/chat/completions",
513
625
  modelId: this.modelId
514
626
  }),
515
627
  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
- },
628
+ body,
522
629
  failedResponseHandler: openaiFailedResponseHandler,
523
630
  successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
524
631
  openaiChatChunkSchema
@@ -607,19 +714,19 @@ var OpenAIChatLanguageModel = class {
607
714
  const index = toolCallDelta.index;
608
715
  if (toolCalls[index] == null) {
609
716
  if (toolCallDelta.type !== "function") {
610
- throw new import_provider2.InvalidResponseDataError({
717
+ throw new import_provider3.InvalidResponseDataError({
611
718
  data: toolCallDelta,
612
719
  message: `Expected 'function' type.`
613
720
  });
614
721
  }
615
722
  if (toolCallDelta.id == null) {
616
- throw new import_provider2.InvalidResponseDataError({
723
+ throw new import_provider3.InvalidResponseDataError({
617
724
  data: toolCallDelta,
618
725
  message: `Expected 'id' to be a string.`
619
726
  });
620
727
  }
621
728
  if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
622
- throw new import_provider2.InvalidResponseDataError({
729
+ throw new import_provider3.InvalidResponseDataError({
623
730
  data: toolCallDelta,
624
731
  message: `Expected 'function.name' to be a string.`
625
732
  });
@@ -695,6 +802,7 @@ var OpenAIChatLanguageModel = class {
695
802
  ),
696
803
  rawCall: { rawPrompt, rawSettings },
697
804
  rawResponse: { headers: responseHeaders },
805
+ request: { body: JSON.stringify(body) },
698
806
  warnings
699
807
  };
700
808
  }
@@ -801,80 +909,6 @@ var openaiChatChunkSchema = import_zod2.z.union([
801
909
  }),
802
910
  openAIErrorDataSchema
803
911
  ]);
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
912
  function isReasoningModel(modelId) {
879
913
  return modelId.startsWith("o1-");
880
914
  }
@@ -883,12 +917,12 @@ function isAudioModel(modelId) {
883
917
  }
884
918
 
885
919
  // src/openai-completion-language-model.ts
886
- var import_provider4 = require("@ai-sdk/provider");
920
+ var import_provider5 = require("@ai-sdk/provider");
887
921
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
888
922
  var import_zod3 = require("zod");
889
923
 
890
924
  // src/convert-to-openai-completion-prompt.ts
891
- var import_provider3 = require("@ai-sdk/provider");
925
+ var import_provider4 = require("@ai-sdk/provider");
892
926
  function convertToOpenAICompletionPrompt({
893
927
  prompt,
894
928
  inputFormat,
@@ -908,7 +942,7 @@ function convertToOpenAICompletionPrompt({
908
942
  for (const { role, content } of prompt) {
909
943
  switch (role) {
910
944
  case "system": {
911
- throw new import_provider3.InvalidPromptError({
945
+ throw new import_provider4.InvalidPromptError({
912
946
  message: "Unexpected system message in prompt: ${content}",
913
947
  prompt
914
948
  });
@@ -920,7 +954,7 @@ function convertToOpenAICompletionPrompt({
920
954
  return part.text;
921
955
  }
922
956
  case "image": {
923
- throw new import_provider3.UnsupportedFunctionalityError({
957
+ throw new import_provider4.UnsupportedFunctionalityError({
924
958
  functionality: "images"
925
959
  });
926
960
  }
@@ -939,7 +973,7 @@ ${userMessage}
939
973
  return part.text;
940
974
  }
941
975
  case "tool-call": {
942
- throw new import_provider3.UnsupportedFunctionalityError({
976
+ throw new import_provider4.UnsupportedFunctionalityError({
943
977
  functionality: "tool-call messages"
944
978
  });
945
979
  }
@@ -952,7 +986,7 @@ ${assistantMessage}
952
986
  break;
953
987
  }
954
988
  case "tool": {
955
- throw new import_provider3.UnsupportedFunctionalityError({
989
+ throw new import_provider4.UnsupportedFunctionalityError({
956
990
  functionality: "tool messages"
957
991
  });
958
992
  }
@@ -1053,24 +1087,24 @@ var OpenAICompletionLanguageModel = class {
1053
1087
  switch (type) {
1054
1088
  case "regular": {
1055
1089
  if ((_a = mode.tools) == null ? void 0 : _a.length) {
1056
- throw new import_provider4.UnsupportedFunctionalityError({
1090
+ throw new import_provider5.UnsupportedFunctionalityError({
1057
1091
  functionality: "tools"
1058
1092
  });
1059
1093
  }
1060
1094
  if (mode.toolChoice) {
1061
- throw new import_provider4.UnsupportedFunctionalityError({
1095
+ throw new import_provider5.UnsupportedFunctionalityError({
1062
1096
  functionality: "toolChoice"
1063
1097
  });
1064
1098
  }
1065
1099
  return { args: baseArgs, warnings };
1066
1100
  }
1067
1101
  case "object-json": {
1068
- throw new import_provider4.UnsupportedFunctionalityError({
1102
+ throw new import_provider5.UnsupportedFunctionalityError({
1069
1103
  functionality: "object-json mode"
1070
1104
  });
1071
1105
  }
1072
1106
  case "object-tool": {
1073
- throw new import_provider4.UnsupportedFunctionalityError({
1107
+ throw new import_provider5.UnsupportedFunctionalityError({
1074
1108
  functionality: "object-tool mode"
1075
1109
  });
1076
1110
  }
@@ -1109,23 +1143,25 @@ var OpenAICompletionLanguageModel = class {
1109
1143
  rawCall: { rawPrompt, rawSettings },
1110
1144
  rawResponse: { headers: responseHeaders },
1111
1145
  response: getResponseMetadata(response),
1112
- warnings
1146
+ warnings,
1147
+ request: { body: JSON.stringify(args) }
1113
1148
  };
1114
1149
  }
1115
1150
  async doStream(options) {
1116
1151
  const { args, warnings } = this.getArgs(options);
1152
+ const body = {
1153
+ ...args,
1154
+ stream: true,
1155
+ // only include stream_options when in strict compatibility mode:
1156
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
1157
+ };
1117
1158
  const { responseHeaders, value: response } = await (0, import_provider_utils4.postJsonToApi)({
1118
1159
  url: this.config.url({
1119
1160
  path: "/completions",
1120
1161
  modelId: this.modelId
1121
1162
  }),
1122
1163
  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
- },
1164
+ body,
1129
1165
  failedResponseHandler: openaiFailedResponseHandler,
1130
1166
  successfulResponseHandler: (0, import_provider_utils4.createEventSourceResponseHandler)(
1131
1167
  openaiCompletionChunkSchema
@@ -1199,7 +1235,8 @@ var OpenAICompletionLanguageModel = class {
1199
1235
  ),
1200
1236
  rawCall: { rawPrompt, rawSettings },
1201
1237
  rawResponse: { headers: responseHeaders },
1202
- warnings
1238
+ warnings,
1239
+ request: { body: JSON.stringify(body) }
1203
1240
  };
1204
1241
  }
1205
1242
  };
@@ -1299,7 +1336,7 @@ var OpenAI = class {
1299
1336
  var import_provider_utils7 = require("@ai-sdk/provider-utils");
1300
1337
 
1301
1338
  // src/openai-embedding-model.ts
1302
- var import_provider5 = require("@ai-sdk/provider");
1339
+ var import_provider6 = require("@ai-sdk/provider");
1303
1340
  var import_provider_utils6 = require("@ai-sdk/provider-utils");
1304
1341
  var import_zod4 = require("zod");
1305
1342
  var OpenAIEmbeddingModel = class {
@@ -1326,7 +1363,7 @@ var OpenAIEmbeddingModel = class {
1326
1363
  abortSignal
1327
1364
  }) {
1328
1365
  if (values.length > this.maxEmbeddingsPerCall) {
1329
- throw new import_provider5.TooManyEmbeddingValuesForCallError({
1366
+ throw new import_provider6.TooManyEmbeddingValuesForCallError({
1330
1367
  provider: this.provider,
1331
1368
  modelId: this.modelId,
1332
1369
  maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,