@ai-sdk/openai 4.0.51 → 4.0.53

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,20 @@
1
1
  # @ai-sdk/openai
2
2
 
3
+ ## 4.0.53
4
+
5
+ ### Patch Changes
6
+
7
+ - 7439d7a: Fix workflow deserialization for OpenAI Responses models.
8
+
9
+ ## 4.0.52
10
+
11
+ ### Patch Changes
12
+
13
+ - 1c68540: Preserve explicit prompt cache breakpoints on scalar Responses tool results.
14
+ - Updated dependencies [aa45741]
15
+ - @ai-sdk/provider@4.0.9
16
+ - @ai-sdk/provider-utils@5.0.34
17
+
3
18
  ## 4.0.51
4
19
 
5
20
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1585,13 +1585,15 @@ type OpenAIToolOptions = {
1585
1585
  };
1586
1586
  };
1587
1587
 
1588
+ type OpenAIHeaders = Record<string, string | undefined>;
1588
1589
  type OpenAIConfig = {
1589
1590
  provider: string;
1591
+ baseURL?: string;
1590
1592
  url: (options: {
1591
1593
  modelId: string;
1592
1594
  path: string;
1593
1595
  }) => string;
1594
- headers?: () => Record<string, string | undefined>;
1596
+ headers?: () => OpenAIHeaders;
1595
1597
  fetch?: FetchFunction;
1596
1598
  webSocket?: WebSocketConstructor;
1597
1599
  generateId?: () => string;
package/dist/index.js CHANGED
@@ -3375,6 +3375,28 @@ import {
3375
3375
  } from "@ai-sdk/provider-utils";
3376
3376
  import { z as z28 } from "zod/v4";
3377
3377
 
3378
+ // src/openai-config.ts
3379
+ function prepareOpenAIConfigForWorkflowDeserialize(config) {
3380
+ if (config.provider == null) {
3381
+ throw new Error(
3382
+ "OpenAI model is missing provider after workflow deserialization."
3383
+ );
3384
+ }
3385
+ return {
3386
+ ...config,
3387
+ provider: config.provider,
3388
+ url: typeof config.url === "function" ? config.url : ({ path }) => {
3389
+ if (config.baseURL == null) {
3390
+ throw new Error(
3391
+ "OpenAI model is missing baseURL after workflow deserialization."
3392
+ );
3393
+ }
3394
+ return `${config.baseURL}${path}`;
3395
+ },
3396
+ headers: typeof config.headers === "function" ? config.headers : config.headers == null ? void 0 : () => config.headers
3397
+ };
3398
+ }
3399
+
3378
3400
  // src/responses/convert-openai-responses-usage.ts
3379
3401
  import { createNullLanguageModelUsage as createNullLanguageModelUsage3 } from "@ai-sdk/provider-utils";
3380
3402
  function convertOpenAIResponsesUsage(usage) {
@@ -4607,26 +4629,38 @@ async function convertFunctionToolResultOutput({
4607
4629
  output,
4608
4630
  toolName,
4609
4631
  outputSchemaToolNames,
4632
+ promptCacheBreakpoint,
4610
4633
  providerOptionsName,
4611
4634
  warnings
4612
4635
  }) {
4613
4636
  var _a2;
4614
4637
  const hasOutputSchema = outputSchemaToolNames == null ? void 0 : outputSchemaToolNames.has(toolName);
4638
+ const convertScalarOutput = (value) => promptCacheBreakpoint == null ? value : [
4639
+ {
4640
+ type: "input_text",
4641
+ text: value,
4642
+ prompt_cache_breakpoint: promptCacheBreakpoint
4643
+ }
4644
+ ];
4615
4645
  switch (output.type) {
4616
4646
  case "text":
4617
4647
  case "error-text":
4618
- return hasOutputSchema ? JSON.stringify(output.value) : output.value;
4648
+ return convertScalarOutput(
4649
+ hasOutputSchema ? JSON.stringify(output.value) : output.value
4650
+ );
4619
4651
  case "execution-denied": {
4620
4652
  const reason = (_a2 = output.reason) != null ? _a2 : "Tool call execution denied.";
4621
- return hasOutputSchema ? JSON.stringify(reason) : reason;
4653
+ return convertScalarOutput(
4654
+ hasOutputSchema ? JSON.stringify(reason) : reason
4655
+ );
4622
4656
  }
4623
4657
  case "json":
4624
4658
  case "error-json":
4625
- return JSON.stringify(output.value);
4659
+ return convertScalarOutput(JSON.stringify(output.value));
4626
4660
  case "content":
4627
4661
  return output.value.map((item) => {
4628
4662
  var _a3, _b, _c;
4629
- const promptCacheBreakpoint = getPromptCacheBreakpoint2(
4663
+ const promptCacheBreakpoint2 = getPromptCacheBreakpoint2(
4630
4664
  item.providerOptions,
4631
4665
  providerOptionsName
4632
4666
  );
@@ -4635,8 +4669,8 @@ async function convertFunctionToolResultOutput({
4635
4669
  return {
4636
4670
  type: "input_text",
4637
4671
  text: item.text,
4638
- ...promptCacheBreakpoint != null && {
4639
- prompt_cache_breakpoint: promptCacheBreakpoint
4672
+ ...promptCacheBreakpoint2 != null && {
4673
+ prompt_cache_breakpoint: promptCacheBreakpoint2
4640
4674
  }
4641
4675
  };
4642
4676
  }
@@ -4650,8 +4684,8 @@ async function convertFunctionToolResultOutput({
4650
4684
  type: "input_image",
4651
4685
  image_url: `data:${fullMediaType};base64,${convertToBase642(item.data.data)}`,
4652
4686
  detail: imageDetail,
4653
- ...promptCacheBreakpoint != null && {
4654
- prompt_cache_breakpoint: promptCacheBreakpoint
4687
+ ...promptCacheBreakpoint2 != null && {
4688
+ prompt_cache_breakpoint: promptCacheBreakpoint2
4655
4689
  }
4656
4690
  };
4657
4691
  }
@@ -4659,8 +4693,8 @@ async function convertFunctionToolResultOutput({
4659
4693
  type: "input_file",
4660
4694
  filename: (_c = item.filename) != null ? _c : "data",
4661
4695
  file_data: `data:${fullMediaType};base64,${convertToBase642(item.data.data)}`,
4662
- ...promptCacheBreakpoint != null && {
4663
- prompt_cache_breakpoint: promptCacheBreakpoint
4696
+ ...promptCacheBreakpoint2 != null && {
4697
+ prompt_cache_breakpoint: promptCacheBreakpoint2
4664
4698
  }
4665
4699
  };
4666
4700
  }
@@ -4670,16 +4704,16 @@ async function convertFunctionToolResultOutput({
4670
4704
  type: "input_image",
4671
4705
  image_url: item.data.url.toString(),
4672
4706
  detail: imageDetail,
4673
- ...promptCacheBreakpoint != null && {
4674
- prompt_cache_breakpoint: promptCacheBreakpoint
4707
+ ...promptCacheBreakpoint2 != null && {
4708
+ prompt_cache_breakpoint: promptCacheBreakpoint2
4675
4709
  }
4676
4710
  };
4677
4711
  }
4678
4712
  return {
4679
4713
  type: "input_file",
4680
4714
  file_url: item.data.url.toString(),
4681
- ...promptCacheBreakpoint != null && {
4682
- prompt_cache_breakpoint: promptCacheBreakpoint
4715
+ ...promptCacheBreakpoint2 != null && {
4716
+ prompt_cache_breakpoint: promptCacheBreakpoint2
4683
4717
  }
4684
4718
  };
4685
4719
  }
@@ -4761,6 +4795,17 @@ function getPromptCacheBreakpoint2(providerOptions, providerOptionsName) {
4761
4795
  var _a2;
4762
4796
  return (_a2 = providerOptions == null ? void 0 : providerOptions[providerOptionsName]) == null ? void 0 : _a2.promptCacheBreakpoint;
4763
4797
  }
4798
+ function getScalarToolResultPromptCacheBreakpoint({
4799
+ output,
4800
+ toolResultProviderOptions,
4801
+ providerOptionsName
4802
+ }) {
4803
+ var _a2;
4804
+ return output.type === "content" ? void 0 : (_a2 = getPromptCacheBreakpoint2(output.providerOptions, providerOptionsName)) != null ? _a2 : getPromptCacheBreakpoint2(
4805
+ toolResultProviderOptions,
4806
+ providerOptionsName
4807
+ );
4808
+ }
4764
4809
  function isFileId(data, prefixes) {
4765
4810
  if (!prefixes) return false;
4766
4811
  return prefixes.some((prefix) => data.startsWith(prefix));
@@ -5408,24 +5453,43 @@ async function convertToOpenAIResponsesInput({
5408
5453
  parallelToolResultGroup.metadata.toolCallId
5409
5454
  );
5410
5455
  const toolOutputs = await Promise.all(
5411
- parallelToolResultGroup.results.map(
5412
- async (result) => convertFunctionToolResultOutput({
5456
+ parallelToolResultGroup.results.map(async (result) => {
5457
+ const promptCacheBreakpoint = getScalarToolResultPromptCacheBreakpoint({
5413
5458
  output: result.output,
5414
- toolName: result.toolName,
5415
- outputSchemaToolNames,
5416
- providerOptionsName,
5417
- warnings
5418
- })
5419
- )
5459
+ toolResultProviderOptions: result.providerOptions,
5460
+ providerOptionsName
5461
+ });
5462
+ return {
5463
+ output: await convertFunctionToolResultOutput({
5464
+ output: result.output,
5465
+ toolName: result.toolName,
5466
+ outputSchemaToolNames,
5467
+ providerOptionsName,
5468
+ warnings
5469
+ }),
5470
+ promptCacheBreakpoint
5471
+ };
5472
+ })
5473
+ );
5474
+ const serializedToolOutputs = toolOutputs.map(
5475
+ ({ output: output2 }) => typeof output2 === "string" ? output2 : JSON.stringify(output2)
5476
+ );
5477
+ const hasPromptCacheBreakpoint = toolOutputs.some(
5478
+ ({ promptCacheBreakpoint }) => promptCacheBreakpoint != null
5420
5479
  );
5421
5480
  input.push({
5422
5481
  type: "function_call_output",
5423
5482
  call_id: parallelToolResultGroup.metadata.toolCallId,
5424
5483
  // The internal wrapper returns one output containing the child
5425
5484
  // results in the same order as the original tool_uses array.
5426
- output: toolOutputs.map(
5427
- (output2) => typeof output2 === "string" ? output2 : JSON.stringify(output2)
5428
- ).join("\n")
5485
+ output: hasPromptCacheBreakpoint ? serializedToolOutputs.map((text, index) => ({
5486
+ type: "input_text",
5487
+ text: index === 0 ? text : `
5488
+ ${text}`,
5489
+ ...toolOutputs[index].promptCacheBreakpoint != null && {
5490
+ prompt_cache_breakpoint: toolOutputs[index].promptCacheBreakpoint
5491
+ }
5492
+ })) : serializedToolOutputs.join("\n")
5429
5493
  });
5430
5494
  }
5431
5495
  continue;
@@ -5521,23 +5585,37 @@ async function convertToOpenAIResponsesInput({
5521
5585
  continue;
5522
5586
  }
5523
5587
  if (customProviderToolNames == null ? void 0 : customProviderToolNames.has(resolvedToolName)) {
5588
+ const promptCacheBreakpoint = getScalarToolResultPromptCacheBreakpoint({
5589
+ output,
5590
+ toolResultProviderOptions: part.providerOptions,
5591
+ providerOptionsName
5592
+ });
5593
+ const convertScalarOutput = (value) => promptCacheBreakpoint == null ? value : [
5594
+ {
5595
+ type: "input_text",
5596
+ text: value,
5597
+ prompt_cache_breakpoint: promptCacheBreakpoint
5598
+ }
5599
+ ];
5524
5600
  let outputValue;
5525
5601
  switch (output.type) {
5526
5602
  case "text":
5527
5603
  case "error-text":
5528
- outputValue = output.value;
5604
+ outputValue = convertScalarOutput(output.value);
5529
5605
  break;
5530
5606
  case "execution-denied":
5531
- outputValue = (_I = output.reason) != null ? _I : "Tool call execution denied.";
5607
+ outputValue = convertScalarOutput(
5608
+ (_I = output.reason) != null ? _I : "Tool call execution denied."
5609
+ );
5532
5610
  break;
5533
5611
  case "json":
5534
5612
  case "error-json":
5535
- outputValue = JSON.stringify(output.value);
5613
+ outputValue = convertScalarOutput(JSON.stringify(output.value));
5536
5614
  break;
5537
5615
  case "content":
5538
5616
  outputValue = output.value.map((item) => {
5539
5617
  var _a3, _b2, _c2;
5540
- const promptCacheBreakpoint = getPromptCacheBreakpoint2(
5618
+ const promptCacheBreakpoint2 = getPromptCacheBreakpoint2(
5541
5619
  item.providerOptions,
5542
5620
  providerOptionsName
5543
5621
  );
@@ -5546,8 +5624,8 @@ async function convertToOpenAIResponsesInput({
5546
5624
  return {
5547
5625
  type: "input_text",
5548
5626
  text: item.text,
5549
- ...promptCacheBreakpoint != null && {
5550
- prompt_cache_breakpoint: promptCacheBreakpoint
5627
+ ...promptCacheBreakpoint2 != null && {
5628
+ prompt_cache_breakpoint: promptCacheBreakpoint2
5551
5629
  }
5552
5630
  };
5553
5631
  case "file": {
@@ -5562,8 +5640,8 @@ async function convertToOpenAIResponsesInput({
5562
5640
  type: "input_image",
5563
5641
  image_url: `data:${fullMediaType};base64,${convertToBase642(item.data.data)}`,
5564
5642
  detail: imageDetail,
5565
- ...promptCacheBreakpoint != null && {
5566
- prompt_cache_breakpoint: promptCacheBreakpoint
5643
+ ...promptCacheBreakpoint2 != null && {
5644
+ prompt_cache_breakpoint: promptCacheBreakpoint2
5567
5645
  }
5568
5646
  };
5569
5647
  }
@@ -5571,8 +5649,8 @@ async function convertToOpenAIResponsesInput({
5571
5649
  type: "input_file",
5572
5650
  filename: (_c2 = item.filename) != null ? _c2 : "data",
5573
5651
  file_data: `data:${fullMediaType};base64,${convertToBase642(item.data.data)}`,
5574
- ...promptCacheBreakpoint != null && {
5575
- prompt_cache_breakpoint: promptCacheBreakpoint
5652
+ ...promptCacheBreakpoint2 != null && {
5653
+ prompt_cache_breakpoint: promptCacheBreakpoint2
5576
5654
  }
5577
5655
  };
5578
5656
  }
@@ -5582,16 +5660,16 @@ async function convertToOpenAIResponsesInput({
5582
5660
  type: "input_image",
5583
5661
  image_url: item.data.url.toString(),
5584
5662
  detail: imageDetail,
5585
- ...promptCacheBreakpoint != null && {
5586
- prompt_cache_breakpoint: promptCacheBreakpoint
5663
+ ...promptCacheBreakpoint2 != null && {
5664
+ prompt_cache_breakpoint: promptCacheBreakpoint2
5587
5665
  }
5588
5666
  };
5589
5667
  }
5590
5668
  return {
5591
5669
  type: "input_file",
5592
5670
  file_url: item.data.url.toString(),
5593
- ...promptCacheBreakpoint != null && {
5594
- prompt_cache_breakpoint: promptCacheBreakpoint
5671
+ ...promptCacheBreakpoint2 != null && {
5672
+ prompt_cache_breakpoint: promptCacheBreakpoint2
5595
5673
  }
5596
5674
  };
5597
5675
  }
@@ -5624,6 +5702,11 @@ async function convertToOpenAIResponsesInput({
5624
5702
  output,
5625
5703
  toolName: part.toolName,
5626
5704
  outputSchemaToolNames,
5705
+ promptCacheBreakpoint: getScalarToolResultPromptCacheBreakpoint({
5706
+ output,
5707
+ toolResultProviderOptions: part.providerOptions,
5708
+ providerOptionsName
5709
+ }),
5627
5710
  providerOptionsName,
5628
5711
  warnings
5629
5712
  });
@@ -6533,7 +6616,10 @@ var OpenAIResponsesLanguageModel = class _OpenAIResponsesLanguageModel {
6533
6616
  });
6534
6617
  }
6535
6618
  static [WORKFLOW_DESERIALIZE5](options) {
6536
- return new _OpenAIResponsesLanguageModel(options.modelId, options.config);
6619
+ return new _OpenAIResponsesLanguageModel(
6620
+ options.modelId,
6621
+ prepareOpenAIConfigForWorkflowDeserialize(options.config)
6622
+ );
6537
6623
  }
6538
6624
  get provider() {
6539
6625
  return this.config.provider;
@@ -8936,7 +9022,7 @@ var OpenAIResponsesBatchLanguageModel = class _OpenAIResponsesBatchLanguageModel
8936
9022
  static [WORKFLOW_DESERIALIZE6](options) {
8937
9023
  return new _OpenAIResponsesBatchLanguageModel(
8938
9024
  options.modelId,
8939
- options.config
9025
+ prepareOpenAIConfigForWorkflowDeserialize(options.config)
8940
9026
  );
8941
9027
  }
8942
9028
  constructor(modelId, config) {
@@ -10612,7 +10698,7 @@ var OpenAISkills = class {
10612
10698
  };
10613
10699
 
10614
10700
  // src/version.ts
10615
- var VERSION = true ? "4.0.51" : "0.0.0-test";
10701
+ var VERSION = true ? "4.0.53" : "0.0.0-test";
10616
10702
 
10617
10703
  // src/openai-provider.ts
10618
10704
  function createOpenAI(options = {}) {
@@ -10706,6 +10792,7 @@ function createOpenAI(options = {}) {
10706
10792
  const createResponsesModel = (modelId) => {
10707
10793
  return new OpenAIResponsesBatchLanguageModel(modelId, {
10708
10794
  provider: `${providerName}.responses`,
10795
+ baseURL,
10709
10796
  url: ({ path }) => `${baseURL}${path}`,
10710
10797
  headers: getHeaders,
10711
10798
  fetch: options.fetch,