@ai-sdk/openai 2.0.125 → 2.0.127

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.
@@ -91,6 +91,11 @@ type OpenAIConfig = {
91
91
  headers: () => Record<string, string | undefined>;
92
92
  fetch?: FetchFunction;
93
93
  generateId?: () => string;
94
+ /**
95
+ * Whether Responses API message input items must include an explicit
96
+ * `type: 'message'` discriminator.
97
+ */
98
+ explicitMessageItemType?: boolean;
94
99
  /**
95
100
  * File ID prefixes used to identify file IDs in Responses API.
96
101
  * When undefined, all file data is treated as base64 content.
@@ -91,6 +91,11 @@ type OpenAIConfig = {
91
91
  headers: () => Record<string, string | undefined>;
92
92
  fetch?: FetchFunction;
93
93
  generateId?: () => string;
94
+ /**
95
+ * Whether Responses API message input items must include an explicit
96
+ * `type: 'message'` discriminator.
97
+ */
98
+ explicitMessageItemType?: boolean;
94
99
  /**
95
100
  * File ID prefixes used to identify file IDs in Responses API.
96
101
  * When undefined, all file data is treated as base64 content.
@@ -98,6 +98,7 @@ function getOpenAILanguageModelCapabilities(modelId) {
98
98
  supportsFlexProcessing,
99
99
  supportsPriorityProcessing,
100
100
  supportsConfigurationUpdate: isGpt6OrLaterModel,
101
+ supportsAsyncToolCalling: isGpt6OrLaterModel,
101
102
  supportedReasoningEfforts: isGpt6OrLaterModel ? ["low", "medium", "high", "xhigh", "max"] : void 0,
102
103
  isReasoningModel,
103
104
  systemMessageMode,
@@ -973,6 +974,12 @@ var OpenAIChatLanguageModel = class {
973
974
  fetch: this.config.fetch
974
975
  });
975
976
  const choice = response.choices[0];
977
+ if (choice == null) {
978
+ throw new import_provider3.InvalidResponseDataError({
979
+ data: rawResponse,
980
+ message: "Response did not contain any choices."
981
+ });
982
+ }
976
983
  const content = [];
977
984
  const text = choice.message.content;
978
985
  if (text != null && text.length > 0) {
@@ -2432,11 +2439,12 @@ function isFileId(data, prefixes) {
2432
2439
  async function convertToOpenAIResponsesInput({
2433
2440
  prompt,
2434
2441
  systemMessageMode,
2442
+ explicitMessageItemType = false,
2435
2443
  fileIdPrefixes,
2436
2444
  store,
2437
2445
  hasLocalShellTool = false
2438
2446
  }) {
2439
- var _a2, _b, _c, _d, _e, _f;
2447
+ var _a2, _b, _c, _d, _e, _f, _g, _h;
2440
2448
  let input = [];
2441
2449
  const warnings = [];
2442
2450
  for (const { role, content, providerOptions } of prompt) {
@@ -2446,6 +2454,7 @@ async function convertToOpenAIResponsesInput({
2446
2454
  case "system": {
2447
2455
  const promptCacheBreakpoint = getPromptCacheBreakpoint2(providerOptions);
2448
2456
  input.push({
2457
+ ...explicitMessageItemType && { type: "message" },
2449
2458
  role: "system",
2450
2459
  content: promptCacheBreakpoint == null ? content : [
2451
2460
  {
@@ -2460,6 +2469,7 @@ async function convertToOpenAIResponsesInput({
2460
2469
  case "developer": {
2461
2470
  const promptCacheBreakpoint = getPromptCacheBreakpoint2(providerOptions);
2462
2471
  input.push({
2472
+ ...explicitMessageItemType && { type: "message" },
2463
2473
  role: "developer",
2464
2474
  content: promptCacheBreakpoint == null ? content : [
2465
2475
  {
@@ -2489,6 +2499,7 @@ async function convertToOpenAIResponsesInput({
2489
2499
  }
2490
2500
  case "user": {
2491
2501
  input.push({
2502
+ ...explicitMessageItemType && { type: "message" },
2492
2503
  role: "user",
2493
2504
  content: content.map((part, index) => {
2494
2505
  var _a3, _b2, _c2;
@@ -2565,6 +2576,7 @@ async function convertToOpenAIResponsesInput({
2565
2576
  break;
2566
2577
  }
2567
2578
  input.push({
2579
+ ...explicitMessageItemType && { type: "message" },
2568
2580
  role: "assistant",
2569
2581
  content: [{ type: "output_text", text: part.text }],
2570
2582
  id,
@@ -2578,6 +2590,7 @@ async function convertToOpenAIResponsesInput({
2578
2590
  break;
2579
2591
  }
2580
2592
  const id = (_f = (_e = part.providerOptions) == null ? void 0 : _e.openai) == null ? void 0 : _f.itemId;
2593
+ const isAsync = (_h = (_g = part.providerOptions) == null ? void 0 : _g.openai) == null ? void 0 : _h.async;
2581
2594
  if (store && id != null) {
2582
2595
  input.push({ type: "item_reference", id });
2583
2596
  break;
@@ -2607,6 +2620,7 @@ async function convertToOpenAIResponsesInput({
2607
2620
  call_id: part.toolCallId,
2608
2621
  name: part.toolName,
2609
2622
  arguments: JSON.stringify(part.input),
2623
+ ...isAsync != null && { async: isAsync },
2610
2624
  id
2611
2625
  });
2612
2626
  break;
@@ -2879,7 +2893,8 @@ var openaiResponsesChunkSchema = (0, import_provider_utils22.lazyValidator)(
2879
2893
  id: import_v415.z.string(),
2880
2894
  call_id: import_v415.z.string(),
2881
2895
  name: import_v415.z.string(),
2882
- arguments: import_v415.z.string()
2896
+ arguments: import_v415.z.string(),
2897
+ async: import_v415.z.boolean().nullish()
2883
2898
  }),
2884
2899
  import_v415.z.object({
2885
2900
  type: import_v415.z.literal("web_search_call"),
@@ -2934,6 +2949,7 @@ var openaiResponsesChunkSchema = (0, import_provider_utils22.lazyValidator)(
2934
2949
  call_id: import_v415.z.string(),
2935
2950
  name: import_v415.z.string(),
2936
2951
  arguments: import_v415.z.string(),
2952
+ async: import_v415.z.boolean().nullish(),
2937
2953
  status: import_v415.z.literal("completed")
2938
2954
  }),
2939
2955
  import_v415.z.object({
@@ -3245,7 +3261,8 @@ var openaiResponsesResponseSchema = (0, import_provider_utils22.lazyValidator)(
3245
3261
  call_id: import_v415.z.string(),
3246
3262
  name: import_v415.z.string(),
3247
3263
  arguments: import_v415.z.string(),
3248
- id: import_v415.z.string()
3264
+ id: import_v415.z.string(),
3265
+ async: import_v415.z.boolean().nullish()
3249
3266
  }),
3250
3267
  import_v415.z.object({
3251
3268
  type: import_v415.z.literal("computer_call"),
@@ -3717,8 +3734,10 @@ var import_provider_utils29 = require("@ai-sdk/provider-utils");
3717
3734
  async function prepareResponsesTools({
3718
3735
  tools,
3719
3736
  toolChoice,
3720
- strictJsonSchema
3737
+ strictJsonSchema,
3738
+ supportsAsyncToolCalling = true
3721
3739
  }) {
3740
+ var _a2;
3722
3741
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
3723
3742
  const toolWarnings = [];
3724
3743
  if (tools == null) {
@@ -3727,15 +3746,24 @@ async function prepareResponsesTools({
3727
3746
  const openaiTools = [];
3728
3747
  for (const tool of tools) {
3729
3748
  switch (tool.type) {
3730
- case "function":
3749
+ case "function": {
3750
+ const openaiOptions = (_a2 = tool.providerOptions) == null ? void 0 : _a2.openai;
3751
+ const async = resolveAsyncToolOption({
3752
+ value: openaiOptions == null ? void 0 : openaiOptions.async,
3753
+ supportsAsyncToolCalling,
3754
+ tool,
3755
+ toolWarnings
3756
+ });
3731
3757
  openaiTools.push({
3732
3758
  type: "function",
3733
3759
  name: tool.name,
3734
3760
  description: tool.description,
3735
3761
  parameters: tool.inputSchema,
3736
- strict: strictJsonSchema
3762
+ strict: strictJsonSchema,
3763
+ ...async != null ? { async } : {}
3737
3764
  });
3738
3765
  break;
3766
+ }
3739
3767
  case "provider-defined": {
3740
3768
  switch (tool.id) {
3741
3769
  case "openai.file_search": {
@@ -3854,6 +3882,22 @@ async function prepareResponsesTools({
3854
3882
  }
3855
3883
  }
3856
3884
  }
3885
+ function resolveAsyncToolOption({
3886
+ value,
3887
+ supportsAsyncToolCalling,
3888
+ tool,
3889
+ toolWarnings
3890
+ }) {
3891
+ if (value !== true || supportsAsyncToolCalling) {
3892
+ return value;
3893
+ }
3894
+ toolWarnings.push({
3895
+ type: "unsupported-tool",
3896
+ tool,
3897
+ details: "Async tool calling is only supported by GPT-6 and later models."
3898
+ });
3899
+ return void 0;
3900
+ }
3857
3901
 
3858
3902
  // src/responses/openai-responses-language-model.ts
3859
3903
  var OpenAIResponsesLanguageModel = class {
@@ -3923,6 +3967,7 @@ var OpenAIResponsesLanguageModel = class {
3923
3967
  const { input, warnings: inputWarnings } = await convertToOpenAIResponsesInput({
3924
3968
  prompt,
3925
3969
  systemMessageMode: modelCapabilities.systemMessageMode,
3970
+ explicitMessageItemType: this.config.explicitMessageItemType,
3926
3971
  fileIdPrefixes: this.config.fileIdPrefixes,
3927
3972
  store: (_a2 = openaiOptions == null ? void 0 : openaiOptions.store) != null ? _a2 : true,
3928
3973
  hasLocalShellTool: hasOpenAITool("openai.local_shell")
@@ -4133,7 +4178,8 @@ var OpenAIResponsesLanguageModel = class {
4133
4178
  } = await prepareResponsesTools({
4134
4179
  tools,
4135
4180
  toolChoice,
4136
- strictJsonSchema
4181
+ strictJsonSchema,
4182
+ supportsAsyncToolCalling: modelCapabilities.supportsAsyncToolCalling
4137
4183
  });
4138
4184
  return {
4139
4185
  webSearchToolName,
@@ -4327,7 +4373,8 @@ var OpenAIResponsesLanguageModel = class {
4327
4373
  input: part.arguments,
4328
4374
  providerMetadata: {
4329
4375
  [providerKey]: {
4330
- itemId: part.id
4376
+ itemId: part.id,
4377
+ ...part.async != null && { async: part.async }
4331
4378
  }
4332
4379
  }
4333
4380
  });
@@ -4527,7 +4574,8 @@ var OpenAIResponsesLanguageModel = class {
4527
4574
  if (value.item.type === "function_call") {
4528
4575
  ongoingToolCalls[value.output_index] = {
4529
4576
  toolName: value.item.name,
4530
- toolCallId: value.item.call_id
4577
+ toolCallId: value.item.call_id,
4578
+ async: value.item.async
4531
4579
  };
4532
4580
  controller.enqueue({
4533
4581
  type: "tool-input-start",
@@ -4651,6 +4699,7 @@ var OpenAIResponsesLanguageModel = class {
4651
4699
  }
4652
4700
  });
4653
4701
  } else if (value.item.type === "function_call") {
4702
+ const ongoingToolCall = ongoingToolCalls[value.output_index];
4654
4703
  ongoingToolCalls[value.output_index] = void 0;
4655
4704
  hasFunctionCall = true;
4656
4705
  controller.enqueue({
@@ -4664,7 +4713,8 @@ var OpenAIResponsesLanguageModel = class {
4664
4713
  input: value.item.arguments,
4665
4714
  providerMetadata: {
4666
4715
  [providerKey]: {
4667
- itemId: value.item.id
4716
+ itemId: value.item.id,
4717
+ ...value.item.async != null ? { async: value.item.async } : (ongoingToolCall == null ? void 0 : ongoingToolCall.async) != null ? { async: ongoingToolCall.async } : {}
4668
4718
  }
4669
4719
  }
4670
4720
  });