@ai-sdk/openai 2.0.0-canary.1 → 2.0.0-canary.2

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.
@@ -72,7 +72,7 @@ function convertToOpenAIChatMessages({
72
72
  image_url: {
73
73
  url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`,
74
74
  // OpenAI specific extension: image detail
75
- detail: (_c = (_b = part.providerMetadata) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
75
+ detail: (_c = (_b = part.providerOptions) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
76
76
  }
77
77
  };
78
78
  }
@@ -253,17 +253,16 @@ import {
253
253
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
254
254
  } from "@ai-sdk/provider";
255
255
  function prepareTools({
256
- mode,
256
+ tools,
257
+ toolChoice,
257
258
  useLegacyFunctionCalling = false,
258
259
  structuredOutputs
259
260
  }) {
260
- var _a;
261
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
261
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
262
262
  const toolWarnings = [];
263
263
  if (tools == null) {
264
- return { tools: void 0, tool_choice: void 0, toolWarnings };
264
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
265
265
  }
266
- const toolChoice = mode.toolChoice;
267
266
  if (useLegacyFunctionCalling) {
268
267
  const openaiFunctions = [];
269
268
  for (const tool of tools) {
@@ -323,18 +322,18 @@ function prepareTools({
323
322
  }
324
323
  }
325
324
  if (toolChoice == null) {
326
- return { tools: openaiTools, tool_choice: void 0, toolWarnings };
325
+ return { tools: openaiTools, toolChoice: void 0, toolWarnings };
327
326
  }
328
327
  const type = toolChoice.type;
329
328
  switch (type) {
330
329
  case "auto":
331
330
  case "none":
332
331
  case "required":
333
- return { tools: openaiTools, tool_choice: type, toolWarnings };
332
+ return { tools: openaiTools, toolChoice: type, toolWarnings };
334
333
  case "tool":
335
334
  return {
336
335
  tools: openaiTools,
337
- tool_choice: {
336
+ toolChoice: {
338
337
  type: "function",
339
338
  function: {
340
339
  name: toolChoice.toolName
@@ -376,7 +375,6 @@ var OpenAIChatLanguageModel = class {
376
375
  return !this.settings.downloadImages;
377
376
  }
378
377
  getArgs({
379
- mode,
380
378
  prompt,
381
379
  maxTokens,
382
380
  temperature,
@@ -387,10 +385,11 @@ var OpenAIChatLanguageModel = class {
387
385
  stopSequences,
388
386
  responseFormat,
389
387
  seed,
390
- providerMetadata
388
+ tools,
389
+ toolChoice,
390
+ providerOptions
391
391
  }) {
392
- var _a, _b, _c, _d, _e, _f, _g, _h;
393
- const type = mode.type;
392
+ var _a, _b, _c, _d, _e, _f, _g;
394
393
  const warnings = [];
395
394
  if (topK != null) {
396
395
  warnings.push({
@@ -439,6 +438,7 @@ var OpenAIChatLanguageModel = class {
439
438
  top_p: topP,
440
439
  frequency_penalty: frequencyPenalty,
441
440
  presence_penalty: presencePenalty,
441
+ // TODO improve below:
442
442
  response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs && responseFormat.schema != null ? {
443
443
  type: "json_schema",
444
444
  json_schema: {
@@ -452,11 +452,11 @@ var OpenAIChatLanguageModel = class {
452
452
  seed,
453
453
  // openai specific settings:
454
454
  // TODO remove in next major version; we auto-map maxTokens now
455
- max_completion_tokens: (_b = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _b.maxCompletionTokens,
456
- store: (_c = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _c.store,
457
- metadata: (_d = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _d.metadata,
458
- prediction: (_e = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _e.prediction,
459
- reasoning_effort: (_g = (_f = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _f.reasoningEffort) != null ? _g : this.settings.reasoningEffort,
455
+ max_completion_tokens: (_b = providerOptions == null ? void 0 : providerOptions.openai) == null ? void 0 : _b.maxCompletionTokens,
456
+ store: (_c = providerOptions == null ? void 0 : providerOptions.openai) == null ? void 0 : _c.store,
457
+ metadata: (_d = providerOptions == null ? void 0 : providerOptions.openai) == null ? void 0 : _d.metadata,
458
+ prediction: (_e = providerOptions == null ? void 0 : providerOptions.openai) == null ? void 0 : _e.prediction,
459
+ reasoning_effort: (_g = (_f = providerOptions == null ? void 0 : providerOptions.openai) == null ? void 0 : _f.reasoningEffort) != null ? _g : this.settings.reasoningEffort,
460
460
  // messages:
461
461
  messages
462
462
  };
@@ -521,81 +521,28 @@ var OpenAIChatLanguageModel = class {
521
521
  baseArgs.max_tokens = void 0;
522
522
  }
523
523
  }
524
- switch (type) {
525
- case "regular": {
526
- const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({
527
- mode,
528
- useLegacyFunctionCalling,
529
- structuredOutputs: this.supportsStructuredOutputs
530
- });
531
- return {
532
- args: {
533
- ...baseArgs,
534
- tools,
535
- tool_choice,
536
- functions,
537
- function_call
538
- },
539
- warnings: [...warnings, ...toolWarnings]
540
- };
541
- }
542
- case "object-json": {
543
- return {
544
- args: {
545
- ...baseArgs,
546
- response_format: this.supportsStructuredOutputs && mode.schema != null ? {
547
- type: "json_schema",
548
- json_schema: {
549
- schema: mode.schema,
550
- strict: true,
551
- name: (_h = mode.name) != null ? _h : "response",
552
- description: mode.description
553
- }
554
- } : { type: "json_object" }
555
- },
556
- warnings
557
- };
558
- }
559
- case "object-tool": {
560
- return {
561
- args: useLegacyFunctionCalling ? {
562
- ...baseArgs,
563
- function_call: {
564
- name: mode.tool.name
565
- },
566
- functions: [
567
- {
568
- name: mode.tool.name,
569
- description: mode.tool.description,
570
- parameters: mode.tool.parameters
571
- }
572
- ]
573
- } : {
574
- ...baseArgs,
575
- tool_choice: {
576
- type: "function",
577
- function: { name: mode.tool.name }
578
- },
579
- tools: [
580
- {
581
- type: "function",
582
- function: {
583
- name: mode.tool.name,
584
- description: mode.tool.description,
585
- parameters: mode.tool.parameters,
586
- strict: this.supportsStructuredOutputs ? true : void 0
587
- }
588
- }
589
- ]
590
- },
591
- warnings
592
- };
593
- }
594
- default: {
595
- const _exhaustiveCheck = type;
596
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
597
- }
598
- }
524
+ const {
525
+ tools: openaiTools,
526
+ toolChoice: openaiToolChoice,
527
+ functions,
528
+ function_call,
529
+ toolWarnings
530
+ } = prepareTools({
531
+ tools,
532
+ toolChoice,
533
+ useLegacyFunctionCalling,
534
+ structuredOutputs: this.supportsStructuredOutputs
535
+ });
536
+ return {
537
+ args: {
538
+ ...baseArgs,
539
+ tools: openaiTools,
540
+ tool_choice: openaiToolChoice,
541
+ functions,
542
+ function_call
543
+ },
544
+ warnings: [...warnings, ...toolWarnings]
545
+ };
599
546
  }
600
547
  async doGenerate(options) {
601
548
  var _a, _b, _c, _d, _e, _f, _g, _h;
@@ -1062,9 +1009,6 @@ var reasoningModels = {
1062
1009
  };
1063
1010
 
1064
1011
  // src/openai-completion-language-model.ts
1065
- import {
1066
- UnsupportedFunctionalityError as UnsupportedFunctionalityError5
1067
- } from "@ai-sdk/provider";
1068
1012
  import {
1069
1013
  combineHeaders as combineHeaders2,
1070
1014
  createEventSourceResponseHandler as createEventSourceResponseHandler2,
@@ -1187,7 +1131,6 @@ var OpenAICompletionLanguageModel = class {
1187
1131
  return this.config.provider;
1188
1132
  }
1189
1133
  getArgs({
1190
- mode,
1191
1134
  inputFormat,
1192
1135
  prompt,
1193
1136
  maxTokens,
@@ -1198,16 +1141,19 @@ var OpenAICompletionLanguageModel = class {
1198
1141
  presencePenalty,
1199
1142
  stopSequences: userStopSequences,
1200
1143
  responseFormat,
1144
+ tools,
1145
+ toolChoice,
1201
1146
  seed
1202
1147
  }) {
1203
- var _a;
1204
- const type = mode.type;
1205
1148
  const warnings = [];
1206
1149
  if (topK != null) {
1207
- warnings.push({
1208
- type: "unsupported-setting",
1209
- setting: "topK"
1210
- });
1150
+ warnings.push({ type: "unsupported-setting", setting: "topK" });
1151
+ }
1152
+ if (tools == null ? void 0 : tools.length) {
1153
+ warnings.push({ type: "unsupported-setting", setting: "tools" });
1154
+ }
1155
+ if (toolChoice != null) {
1156
+ warnings.push({ type: "unsupported-setting", setting: "toolChoice" });
1211
1157
  }
1212
1158
  if (responseFormat != null && responseFormat.type !== "text") {
1213
1159
  warnings.push({
@@ -1218,56 +1164,30 @@ var OpenAICompletionLanguageModel = class {
1218
1164
  }
1219
1165
  const { prompt: completionPrompt, stopSequences } = convertToOpenAICompletionPrompt({ prompt, inputFormat });
1220
1166
  const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
1221
- const baseArgs = {
1222
- // model id:
1223
- model: this.modelId,
1224
- // model specific settings:
1225
- echo: this.settings.echo,
1226
- logit_bias: this.settings.logitBias,
1227
- logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
1228
- suffix: this.settings.suffix,
1229
- user: this.settings.user,
1230
- // standardized settings:
1231
- max_tokens: maxTokens,
1232
- temperature,
1233
- top_p: topP,
1234
- frequency_penalty: frequencyPenalty,
1235
- presence_penalty: presencePenalty,
1236
- seed,
1237
- // prompt:
1238
- prompt: completionPrompt,
1239
- // stop sequences:
1240
- stop: stop.length > 0 ? stop : void 0
1167
+ return {
1168
+ args: {
1169
+ // model id:
1170
+ model: this.modelId,
1171
+ // model specific settings:
1172
+ echo: this.settings.echo,
1173
+ logit_bias: this.settings.logitBias,
1174
+ logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
1175
+ suffix: this.settings.suffix,
1176
+ user: this.settings.user,
1177
+ // standardized settings:
1178
+ max_tokens: maxTokens,
1179
+ temperature,
1180
+ top_p: topP,
1181
+ frequency_penalty: frequencyPenalty,
1182
+ presence_penalty: presencePenalty,
1183
+ seed,
1184
+ // prompt:
1185
+ prompt: completionPrompt,
1186
+ // stop sequences:
1187
+ stop: stop.length > 0 ? stop : void 0
1188
+ },
1189
+ warnings
1241
1190
  };
1242
- switch (type) {
1243
- case "regular": {
1244
- if ((_a = mode.tools) == null ? void 0 : _a.length) {
1245
- throw new UnsupportedFunctionalityError5({
1246
- functionality: "tools"
1247
- });
1248
- }
1249
- if (mode.toolChoice) {
1250
- throw new UnsupportedFunctionalityError5({
1251
- functionality: "toolChoice"
1252
- });
1253
- }
1254
- return { args: baseArgs, warnings };
1255
- }
1256
- case "object-json": {
1257
- throw new UnsupportedFunctionalityError5({
1258
- functionality: "object-json mode"
1259
- });
1260
- }
1261
- case "object-tool": {
1262
- throw new UnsupportedFunctionalityError5({
1263
- functionality: "object-tool mode"
1264
- });
1265
- }
1266
- default: {
1267
- const _exhaustiveCheck = type;
1268
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
1269
- }
1270
- }
1271
1191
  }
1272
1192
  async doGenerate(options) {
1273
1193
  const { args, warnings } = this.getArgs(options);
@@ -1618,7 +1538,7 @@ import { z as z6 } from "zod";
1618
1538
 
1619
1539
  // src/responses/convert-to-openai-responses-messages.ts
1620
1540
  import {
1621
- UnsupportedFunctionalityError as UnsupportedFunctionalityError6
1541
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError5
1622
1542
  } from "@ai-sdk/provider";
1623
1543
  import { convertUint8ArrayToBase64 as convertUint8ArrayToBase642 } from "@ai-sdk/provider-utils";
1624
1544
  function convertToOpenAIResponsesMessages({
@@ -1669,12 +1589,12 @@ function convertToOpenAIResponsesMessages({
1669
1589
  type: "input_image",
1670
1590
  image_url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase642(part.image)}`,
1671
1591
  // OpenAI specific extension: image detail
1672
- detail: (_c = (_b = part.providerMetadata) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
1592
+ detail: (_c = (_b = part.providerOptions) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
1673
1593
  };
1674
1594
  }
1675
1595
  case "file": {
1676
1596
  if (part.data instanceof URL) {
1677
- throw new UnsupportedFunctionalityError6({
1597
+ throw new UnsupportedFunctionalityError5({
1678
1598
  functionality: "File URLs in user messages"
1679
1599
  });
1680
1600
  }
@@ -1687,7 +1607,7 @@ function convertToOpenAIResponsesMessages({
1687
1607
  };
1688
1608
  }
1689
1609
  default: {
1690
- throw new UnsupportedFunctionalityError6({
1610
+ throw new UnsupportedFunctionalityError5({
1691
1611
  functionality: "Only PDF files are supported in user messages"
1692
1612
  });
1693
1613
  }
@@ -1760,19 +1680,18 @@ function mapOpenAIResponseFinishReason({
1760
1680
 
1761
1681
  // src/responses/openai-responses-prepare-tools.ts
1762
1682
  import {
1763
- UnsupportedFunctionalityError as UnsupportedFunctionalityError7
1683
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError6
1764
1684
  } from "@ai-sdk/provider";
1765
1685
  function prepareResponsesTools({
1766
- mode,
1686
+ tools,
1687
+ toolChoice,
1767
1688
  strict
1768
1689
  }) {
1769
- var _a;
1770
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
1690
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
1771
1691
  const toolWarnings = [];
1772
1692
  if (tools == null) {
1773
- return { tools: void 0, tool_choice: void 0, toolWarnings };
1693
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
1774
1694
  }
1775
- const toolChoice = mode.toolChoice;
1776
1695
  const openaiTools = [];
1777
1696
  for (const tool of tools) {
1778
1697
  switch (tool.type) {
@@ -1805,36 +1724,23 @@ function prepareResponsesTools({
1805
1724
  }
1806
1725
  }
1807
1726
  if (toolChoice == null) {
1808
- return { tools: openaiTools, tool_choice: void 0, toolWarnings };
1727
+ return { tools: openaiTools, toolChoice: void 0, toolWarnings };
1809
1728
  }
1810
1729
  const type = toolChoice.type;
1811
1730
  switch (type) {
1812
1731
  case "auto":
1813
1732
  case "none":
1814
1733
  case "required":
1815
- return { tools: openaiTools, tool_choice: type, toolWarnings };
1816
- case "tool": {
1817
- if (toolChoice.toolName === "web_search_preview") {
1818
- return {
1819
- tools: openaiTools,
1820
- tool_choice: {
1821
- type: "web_search_preview"
1822
- },
1823
- toolWarnings
1824
- };
1825
- }
1734
+ return { tools: openaiTools, toolChoice: type, toolWarnings };
1735
+ case "tool":
1826
1736
  return {
1827
1737
  tools: openaiTools,
1828
- tool_choice: {
1829
- type: "function",
1830
- name: toolChoice.toolName
1831
- },
1738
+ toolChoice: toolChoice.toolName === "web_search_preview" ? { type: "web_search_preview" } : { type: "function", name: toolChoice.toolName },
1832
1739
  toolWarnings
1833
1740
  };
1834
- }
1835
1741
  default: {
1836
1742
  const _exhaustiveCheck = type;
1837
- throw new UnsupportedFunctionalityError7({
1743
+ throw new UnsupportedFunctionalityError6({
1838
1744
  functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
1839
1745
  });
1840
1746
  }
@@ -1853,7 +1759,6 @@ var OpenAIResponsesLanguageModel = class {
1853
1759
  return this.config.provider;
1854
1760
  }
1855
1761
  getArgs({
1856
- mode,
1857
1762
  maxTokens,
1858
1763
  temperature,
1859
1764
  stopSequences,
@@ -1863,24 +1768,19 @@ var OpenAIResponsesLanguageModel = class {
1863
1768
  frequencyPenalty,
1864
1769
  seed,
1865
1770
  prompt,
1866
- providerMetadata,
1771
+ providerOptions,
1772
+ tools,
1773
+ toolChoice,
1867
1774
  responseFormat
1868
1775
  }) {
1869
- var _a, _b, _c;
1776
+ var _a, _b;
1870
1777
  const warnings = [];
1871
1778
  const modelConfig = getResponsesModelConfig(this.modelId);
1872
- const type = mode.type;
1873
1779
  if (topK != null) {
1874
- warnings.push({
1875
- type: "unsupported-setting",
1876
- setting: "topK"
1877
- });
1780
+ warnings.push({ type: "unsupported-setting", setting: "topK" });
1878
1781
  }
1879
1782
  if (seed != null) {
1880
- warnings.push({
1881
- type: "unsupported-setting",
1882
- setting: "seed"
1883
- });
1783
+ warnings.push({ type: "unsupported-setting", setting: "seed" });
1884
1784
  }
1885
1785
  if (presencePenalty != null) {
1886
1786
  warnings.push({
@@ -1895,10 +1795,7 @@ var OpenAIResponsesLanguageModel = class {
1895
1795
  });
1896
1796
  }
1897
1797
  if (stopSequences != null) {
1898
- warnings.push({
1899
- type: "unsupported-setting",
1900
- setting: "stopSequences"
1901
- });
1798
+ warnings.push({ type: "unsupported-setting", setting: "stopSequences" });
1902
1799
  }
1903
1800
  const { messages, warnings: messageWarnings } = convertToOpenAIResponsesMessages({
1904
1801
  prompt,
@@ -1907,7 +1804,7 @@ var OpenAIResponsesLanguageModel = class {
1907
1804
  warnings.push(...messageWarnings);
1908
1805
  const openaiOptions = parseProviderOptions({
1909
1806
  provider: "openai",
1910
- providerOptions: providerMetadata,
1807
+ providerOptions,
1911
1808
  schema: openaiResponsesProviderOptionsSchema
1912
1809
  });
1913
1810
  const isStrict = (_a = openaiOptions == null ? void 0 : openaiOptions.strictSchemas) != null ? _a : true;
@@ -1961,62 +1858,23 @@ var OpenAIResponsesLanguageModel = class {
1961
1858
  });
1962
1859
  }
1963
1860
  }
1964
- switch (type) {
1965
- case "regular": {
1966
- const { tools, tool_choice, toolWarnings } = prepareResponsesTools({
1967
- mode,
1968
- strict: isStrict
1969
- // TODO support provider options on tools
1970
- });
1971
- return {
1972
- args: {
1973
- ...baseArgs,
1974
- tools,
1975
- tool_choice
1976
- },
1977
- warnings: [...warnings, ...toolWarnings]
1978
- };
1979
- }
1980
- case "object-json": {
1981
- return {
1982
- args: {
1983
- ...baseArgs,
1984
- text: {
1985
- format: mode.schema != null ? {
1986
- type: "json_schema",
1987
- strict: isStrict,
1988
- name: (_c = mode.name) != null ? _c : "response",
1989
- description: mode.description,
1990
- schema: mode.schema
1991
- } : { type: "json_object" }
1992
- }
1993
- },
1994
- warnings
1995
- };
1996
- }
1997
- case "object-tool": {
1998
- return {
1999
- args: {
2000
- ...baseArgs,
2001
- tool_choice: { type: "function", name: mode.tool.name },
2002
- tools: [
2003
- {
2004
- type: "function",
2005
- name: mode.tool.name,
2006
- description: mode.tool.description,
2007
- parameters: mode.tool.parameters,
2008
- strict: isStrict
2009
- }
2010
- ]
2011
- },
2012
- warnings
2013
- };
2014
- }
2015
- default: {
2016
- const _exhaustiveCheck = type;
2017
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
2018
- }
2019
- }
1861
+ const {
1862
+ tools: openaiTools,
1863
+ toolChoice: openaiToolChoice,
1864
+ toolWarnings
1865
+ } = prepareResponsesTools({
1866
+ tools,
1867
+ toolChoice,
1868
+ strict: isStrict
1869
+ });
1870
+ return {
1871
+ args: {
1872
+ ...baseArgs,
1873
+ tools: openaiTools,
1874
+ tool_choice: openaiToolChoice
1875
+ },
1876
+ warnings: [...warnings, ...toolWarnings]
1877
+ };
2020
1878
  }
2021
1879
  async doGenerate(options) {
2022
1880
  var _a, _b, _c, _d, _e;