@ai-sdk/openai 2.0.133 → 2.0.135

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,19 @@
1
1
  # @ai-sdk/openai
2
2
 
3
+ ## 2.0.135
4
+
5
+ ### Patch Changes
6
+
7
+ - c883c47: fix(openai): send assistant text and function calls with content instead of an item reference when their reasoning was removed
8
+
9
+ With reasoning models, pruning reasoning (e.g. `pruneMessages({ reasoning: 'all' })`) caused follow-up requests to fail with "Item 'msg*...' of type 'message' was provided without its required 'reasoning' item" or "Item 'fc*...' of type 'function_call' was provided without its required 'reasoning' item". Assistant text and function call parts are now only sent as item references, or with their item id, when they are preceded by reasoning in the same assistant message.
10
+
11
+ ## 2.0.134
12
+
13
+ ### Patch Changes
14
+
15
+ - b399a1d: fix(openai): replay web search results, including tools with custom names, when response storage is disabled
16
+
3
17
  ## 2.0.133
4
18
 
5
19
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -298,7 +298,7 @@ declare const openaiResponsesChunkSchema: _ai_sdk_provider_utils.LazyValidator<{
298
298
  call_id: string;
299
299
  name: string;
300
300
  arguments: string;
301
- status: "in_progress" | "completed" | "incomplete";
301
+ status: "completed" | "in_progress" | "incomplete";
302
302
  async?: boolean | null | undefined;
303
303
  } | {
304
304
  type: "code_interpreter_call";
package/dist/index.d.ts CHANGED
@@ -298,7 +298,7 @@ declare const openaiResponsesChunkSchema: _ai_sdk_provider_utils.LazyValidator<{
298
298
  call_id: string;
299
299
  name: string;
300
300
  arguments: string;
301
- status: "in_progress" | "completed" | "incomplete";
301
+ status: "completed" | "in_progress" | "incomplete";
302
302
  async?: boolean | null | undefined;
303
303
  } | {
304
304
  type: "code_interpreter_call";
package/dist/index.js CHANGED
@@ -2485,6 +2485,56 @@ var openaiResponsesSystemMessageOptionsSchema = (0, import_provider_utils21.lazy
2485
2485
  );
2486
2486
 
2487
2487
  // src/responses/convert-to-openai-responses-input.ts
2488
+ async function convertWebSearchToolResultOutput({
2489
+ output,
2490
+ id
2491
+ }) {
2492
+ if (output.type !== "json") {
2493
+ return void 0;
2494
+ }
2495
+ const validation = await (0, import_provider_utils22.safeValidateTypes)({
2496
+ value: output.value,
2497
+ schema: webSearchOutputSchema
2498
+ });
2499
+ if (!validation.success || validation.value.action == null) {
2500
+ return void 0;
2501
+ }
2502
+ const { action, sources } = validation.value;
2503
+ switch (action.type) {
2504
+ case "search":
2505
+ return {
2506
+ type: "web_search_call",
2507
+ id,
2508
+ status: "completed",
2509
+ action: {
2510
+ type: "search",
2511
+ ...action.query != null && { query: action.query },
2512
+ ...sources != null && { sources }
2513
+ }
2514
+ };
2515
+ case "openPage":
2516
+ return {
2517
+ type: "web_search_call",
2518
+ id,
2519
+ status: "completed",
2520
+ action: {
2521
+ type: "open_page",
2522
+ url: action.url
2523
+ }
2524
+ };
2525
+ case "findInPage":
2526
+ return {
2527
+ type: "web_search_call",
2528
+ id,
2529
+ status: "completed",
2530
+ action: {
2531
+ type: "find_in_page",
2532
+ url: action.url,
2533
+ pattern: action.pattern
2534
+ }
2535
+ };
2536
+ }
2537
+ }
2488
2538
  function getPromptCacheBreakpoint2(providerOptions) {
2489
2539
  var _a2;
2490
2540
  return (_a2 = providerOptions == null ? void 0 : providerOptions.openai) == null ? void 0 : _a2.promptCacheBreakpoint;
@@ -2500,10 +2550,12 @@ async function convertToOpenAIResponsesInput({
2500
2550
  explicitMessageItemType = false,
2501
2551
  fileIdPrefixes,
2502
2552
  store,
2553
+ isReasoningModel = false,
2503
2554
  configurationUpdateUnsupportedReason,
2504
- hasLocalShellTool = false
2555
+ hasLocalShellTool = false,
2556
+ webSearchToolName
2505
2557
  }) {
2506
- var _a2, _b, _c, _d, _e, _f, _g, _h;
2558
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2507
2559
  let input = [];
2508
2560
  const warnings = [];
2509
2561
  for (const { role, content, providerOptions } of prompt) {
@@ -2652,12 +2704,14 @@ async function convertToOpenAIResponsesInput({
2652
2704
  case "assistant": {
2653
2705
  const reasoningMessages = {};
2654
2706
  const toolCallParts = {};
2707
+ let hasPrecedingReasoning = false;
2655
2708
  for (const part of content) {
2656
2709
  switch (part.type) {
2657
2710
  case "text": {
2658
2711
  const id = (_b = (_a2 = part.providerOptions) == null ? void 0 : _a2.openai) == null ? void 0 : _b.itemId;
2659
2712
  const phase = (_d = (_c = part.providerOptions) == null ? void 0 : _c.openai) == null ? void 0 : _d.phase;
2660
- if (store && id != null) {
2713
+ const canUseItemId = id != null && (!isReasoningModel || hasPrecedingReasoning);
2714
+ if (store && canUseItemId) {
2661
2715
  input.push({ type: "item_reference", id });
2662
2716
  break;
2663
2717
  }
@@ -2665,7 +2719,7 @@ async function convertToOpenAIResponsesInput({
2665
2719
  ...explicitMessageItemType && { type: "message" },
2666
2720
  role: "assistant",
2667
2721
  content: [{ type: "output_text", text: part.text }],
2668
- id,
2722
+ id: canUseItemId ? id : void 0,
2669
2723
  ...phase != null && { phase }
2670
2724
  });
2671
2725
  break;
@@ -2677,7 +2731,8 @@ async function convertToOpenAIResponsesInput({
2677
2731
  }
2678
2732
  const id = (_f = (_e = part.providerOptions) == null ? void 0 : _e.openai) == null ? void 0 : _f.itemId;
2679
2733
  const isAsync = (_h = (_g = part.providerOptions) == null ? void 0 : _g.openai) == null ? void 0 : _h.async;
2680
- if (store && id != null) {
2734
+ const canUseItemId = id != null && (!isReasoningModel || hasPrecedingReasoning);
2735
+ if (store && canUseItemId) {
2681
2736
  input.push({ type: "item_reference", id });
2682
2737
  break;
2683
2738
  }
@@ -2707,12 +2762,27 @@ async function convertToOpenAIResponsesInput({
2707
2762
  name: part.toolName,
2708
2763
  arguments: JSON.stringify(part.input),
2709
2764
  ...isAsync != null && { async: isAsync },
2710
- id
2765
+ id: canUseItemId ? id : void 0
2711
2766
  });
2712
2767
  break;
2713
2768
  }
2714
2769
  // assistant tool result parts are from provider-executed tools:
2715
2770
  case "tool-result": {
2771
+ if (part.toolName === "web_search" || part.toolName === "web_search_preview" || part.toolName === webSearchToolName) {
2772
+ const itemId = (_k = (_j = (_i = part.providerOptions) == null ? void 0 : _i[providerOptionsName]) == null ? void 0 : _j.itemId) != null ? _k : part.toolCallId;
2773
+ if (store) {
2774
+ input.push({ type: "item_reference", id: itemId });
2775
+ break;
2776
+ }
2777
+ const webSearchCall = await convertWebSearchToolResultOutput({
2778
+ output: part.output,
2779
+ id: itemId
2780
+ });
2781
+ if (webSearchCall != null) {
2782
+ input.push(webSearchCall);
2783
+ break;
2784
+ }
2785
+ }
2716
2786
  if (store) {
2717
2787
  input.push({ type: "item_reference", id: part.toolCallId });
2718
2788
  } else {
@@ -2724,6 +2794,7 @@ async function convertToOpenAIResponsesInput({
2724
2794
  break;
2725
2795
  }
2726
2796
  case "reasoning": {
2797
+ hasPrecedingReasoning = true;
2727
2798
  const providerOptions2 = await (0, import_provider_utils22.parseProviderOptions)({
2728
2799
  provider: "openai",
2729
2800
  providerOptions: part.providerOptions,
@@ -3643,6 +3714,9 @@ var OpenAIResponsesLanguageModel = class {
3643
3714
  modelCapabilities,
3644
3715
  options: openaiOptions
3645
3716
  });
3717
+ const webSearchToolName = (_a2 = tools == null ? void 0 : tools.find(
3718
+ (tool) => tool.type === "provider-defined" && (tool.id === "openai.web_search" || tool.id === "openai.web_search_preview")
3719
+ )) == null ? void 0 : _a2.name;
3646
3720
  const { input, warnings: inputWarnings } = await convertToOpenAIResponsesInput({
3647
3721
  prompt,
3648
3722
  configurationUpdateUnsupportedReason,
@@ -3650,8 +3724,10 @@ var OpenAIResponsesLanguageModel = class {
3650
3724
  systemMessageMode: modelCapabilities.systemMessageMode,
3651
3725
  explicitMessageItemType: this.config.explicitMessageItemType,
3652
3726
  fileIdPrefixes: this.config.fileIdPrefixes,
3653
- store: (_a2 = openaiOptions == null ? void 0 : openaiOptions.store) != null ? _a2 : true,
3654
- hasLocalShellTool: hasOpenAITool("openai.local_shell")
3727
+ store: (_b = openaiOptions == null ? void 0 : openaiOptions.store) != null ? _b : true,
3728
+ isReasoningModel: modelCapabilities.isReasoningModel,
3729
+ hasLocalShellTool: hasOpenAITool("openai.local_shell"),
3730
+ webSearchToolName
3655
3731
  });
3656
3732
  warnings.push(...inputWarnings);
3657
3733
  let resolvedReasoningEffort = openaiOptions == null ? void 0 : openaiOptions.reasoningEffort;
@@ -3688,7 +3764,7 @@ var OpenAIResponsesLanguageModel = class {
3688
3764
  });
3689
3765
  }
3690
3766
  }
3691
- const strictJsonSchema = (_b = openaiOptions == null ? void 0 : openaiOptions.strictJsonSchema) != null ? _b : false;
3767
+ const strictJsonSchema = (_c = openaiOptions == null ? void 0 : openaiOptions.strictJsonSchema) != null ? _c : false;
3692
3768
  let include = openaiOptions == null ? void 0 : openaiOptions.include;
3693
3769
  function addInclude(key) {
3694
3770
  if (include == null) {
@@ -3706,9 +3782,6 @@ var OpenAIResponsesLanguageModel = class {
3706
3782
  if (topLogprobs) {
3707
3783
  addInclude("message.output_text.logprobs");
3708
3784
  }
3709
- const webSearchToolName = (_c = tools == null ? void 0 : tools.find(
3710
- (tool) => tool.type === "provider-defined" && (tool.id === "openai.web_search" || tool.id === "openai.web_search_preview")
3711
- )) == null ? void 0 : _c.name;
3712
3785
  if (webSearchToolName) {
3713
3786
  addInclude("web_search_call.action.sources");
3714
3787
  }
@@ -5139,7 +5212,7 @@ var OpenAITranscriptionModel = class {
5139
5212
  };
5140
5213
 
5141
5214
  // src/version.ts
5142
- var VERSION = true ? "2.0.133" : "0.0.0-test";
5215
+ var VERSION = true ? "2.0.135" : "0.0.0-test";
5143
5216
 
5144
5217
  // src/openai-provider.ts
5145
5218
  function createOpenAI(options = {}) {