@ai-sdk/openai 2.0.118 → 2.0.122

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/dist/index.mjs CHANGED
@@ -41,10 +41,10 @@ var openaiFailedResponseHandler = createJsonErrorResponseHandler({
41
41
 
42
42
  // src/openai-language-model-capabilities.ts
43
43
  function getOpenAILanguageModelCapabilities(modelId) {
44
- var _a, _b, _c, _d, _e;
44
+ var _a2, _b, _c, _d, _e;
45
45
  const oSeriesVersion = getOSeriesVersion(modelId);
46
46
  const gptVersion = getGptVersion(modelId);
47
- const isGptChatModel = (gptVersion == null ? void 0 : gptVersion.minor) == null && ((_b = (_a = gptVersion == null ? void 0 : gptVersion.variant) == null ? void 0 : _a.startsWith("chat")) != null ? _b : false);
47
+ const isGptChatModel = (gptVersion == null ? void 0 : gptVersion.minor) == null && ((_b = (_a2 = gptVersion == null ? void 0 : gptVersion.variant) == null ? void 0 : _a2.startsWith("chat")) != null ? _b : false);
48
48
  const isGptNanoModel = (_d = (_c = gptVersion == null ? void 0 : gptVersion.variant) == null ? void 0 : _c.startsWith("nano")) != null ? _d : false;
49
49
  const supportsFlexProcessing = oSeriesVersion != null && oSeriesVersion >= 3 || gptVersion != null && gptVersion.major >= 5 && !isGptChatModel;
50
50
  const supportsPriorityProcessing = modelId.startsWith("gpt-4") || gptVersion != null && gptVersion.major >= 5 && !isGptNanoModel && !isGptChatModel || oSeriesVersion != null && oSeriesVersion >= 3;
@@ -80,9 +80,14 @@ import {
80
80
  UnsupportedFunctionalityError
81
81
  } from "@ai-sdk/provider";
82
82
  import { convertToBase64 } from "@ai-sdk/provider-utils";
83
+ function serializeToolCallArguments(input) {
84
+ return JSON.stringify(
85
+ typeof input === "object" && input !== null && !Array.isArray(input) ? input : {}
86
+ );
87
+ }
83
88
  function getPromptCacheBreakpoint(providerOptions) {
84
- var _a;
85
- return (_a = providerOptions == null ? void 0 : providerOptions.openai) == null ? void 0 : _a.promptCacheBreakpoint;
89
+ var _a2;
90
+ return (_a2 = providerOptions == null ? void 0 : providerOptions.openai) == null ? void 0 : _a2.promptCacheBreakpoint;
86
91
  }
87
92
  function convertToOpenAIChatMessages({
88
93
  prompt,
@@ -146,7 +151,7 @@ function convertToOpenAIChatMessages({
146
151
  messages.push({
147
152
  role: "user",
148
153
  content: content.map((part, index) => {
149
- var _a, _b, _c;
154
+ var _a2, _b, _c;
150
155
  switch (part.type) {
151
156
  case "text": {
152
157
  const promptCacheBreakpoint = getPromptCacheBreakpoint(
@@ -171,7 +176,7 @@ function convertToOpenAIChatMessages({
171
176
  image_url: {
172
177
  url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`,
173
178
  // OpenAI specific extension: image detail
174
- detail: (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.imageDetail
179
+ detail: (_b = (_a2 = part.providerOptions) == null ? void 0 : _a2.openai) == null ? void 0 : _b.imageDetail
175
180
  },
176
181
  ...promptCacheBreakpoint != null && {
177
182
  prompt_cache_breakpoint: promptCacheBreakpoint
@@ -270,7 +275,7 @@ function convertToOpenAIChatMessages({
270
275
  type: "function",
271
276
  function: {
272
277
  name: part.toolName,
273
- arguments: JSON.stringify(part.input)
278
+ arguments: serializeToolCallArguments(part.input)
274
279
  }
275
280
  });
276
281
  break;
@@ -713,13 +718,13 @@ var OpenAIChatLanguageModel = class {
713
718
  toolChoice,
714
719
  providerOptions
715
720
  }) {
716
- var _a, _b, _c, _d;
721
+ var _a2, _b, _c, _d;
717
722
  const warnings = [];
718
- const openaiOptions = (_a = await parseProviderOptions({
723
+ const openaiOptions = (_a2 = await parseProviderOptions({
719
724
  provider: "openai",
720
725
  providerOptions,
721
726
  schema: openaiChatLanguageModelOptions
722
- })) != null ? _a : {};
727
+ })) != null ? _a2 : {};
723
728
  const structuredOutputs = (_b = openaiOptions.structuredOutputs) != null ? _b : true;
724
729
  const modelCapabilities = getOpenAILanguageModelCapabilities(this.modelId);
725
730
  if (topK != null) {
@@ -893,7 +898,7 @@ var OpenAIChatLanguageModel = class {
893
898
  };
894
899
  }
895
900
  async doGenerate(options) {
896
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
901
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
897
902
  const { args: body, warnings } = await this.getArgs(options);
898
903
  const {
899
904
  responseHeaders,
@@ -919,7 +924,7 @@ var OpenAIChatLanguageModel = class {
919
924
  if (text != null && text.length > 0) {
920
925
  content.push({ type: "text", text });
921
926
  }
922
- for (const toolCall of (_a = choice.message.tool_calls) != null ? _a : []) {
927
+ for (const toolCall of (_a2 = choice.message.tool_calls) != null ? _a2 : []) {
923
928
  content.push({
924
929
  type: "tool-call",
925
930
  toolCallId: (_b = toolCall.id) != null ? _b : generateId(),
@@ -1013,7 +1018,7 @@ var OpenAIChatLanguageModel = class {
1013
1018
  controller.enqueue({ type: "stream-start", warnings });
1014
1019
  },
1015
1020
  transform(chunk, controller) {
1016
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
1021
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
1017
1022
  if (options.includeRawChunks) {
1018
1023
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1019
1024
  }
@@ -1039,7 +1044,7 @@ var OpenAIChatLanguageModel = class {
1039
1044
  }
1040
1045
  }
1041
1046
  if (value.usage != null) {
1042
- usage.inputTokens = (_a = value.usage.prompt_tokens) != null ? _a : void 0;
1047
+ usage.inputTokens = (_a2 = value.usage.prompt_tokens) != null ? _a2 : void 0;
1043
1048
  usage.outputTokens = (_b = value.usage.completion_tokens) != null ? _b : void 0;
1044
1049
  usage.totalTokens = (_c = value.usage.total_tokens) != null ? _c : void 0;
1045
1050
  usage.reasoningTokens = (_e = (_d = value.usage.completion_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : void 0;
@@ -1514,7 +1519,7 @@ var OpenAICompletionLanguageModel = class {
1514
1519
  };
1515
1520
  }
1516
1521
  async doGenerate(options) {
1517
- var _a, _b, _c;
1522
+ var _a2, _b, _c;
1518
1523
  const { args, warnings } = await this.getArgs(options);
1519
1524
  const {
1520
1525
  responseHeaders,
@@ -1542,7 +1547,7 @@ var OpenAICompletionLanguageModel = class {
1542
1547
  return {
1543
1548
  content: [{ type: "text", text: choice.text }],
1544
1549
  usage: {
1545
- inputTokens: (_a = response.usage) == null ? void 0 : _a.prompt_tokens,
1550
+ inputTokens: (_a2 = response.usage) == null ? void 0 : _a2.prompt_tokens,
1546
1551
  outputTokens: (_b = response.usage) == null ? void 0 : _b.completion_tokens,
1547
1552
  totalTokens: (_c = response.usage) == null ? void 0 : _c.total_tokens
1548
1553
  },
@@ -1663,6 +1668,7 @@ import {
1663
1668
  import {
1664
1669
  combineHeaders as combineHeaders3,
1665
1670
  createJsonResponseHandler as createJsonResponseHandler3,
1671
+ EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL,
1666
1672
  parseProviderOptions as parseProviderOptions3,
1667
1673
  postJsonToApi as postJsonToApi3
1668
1674
  } from "@ai-sdk/provider-utils";
@@ -1703,10 +1709,13 @@ var openaiTextEmbeddingResponseSchema = lazyValidator6(
1703
1709
  );
1704
1710
 
1705
1711
  // src/embedding/openai-embedding-model.ts
1712
+ var _a;
1713
+ _a = EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL;
1706
1714
  var OpenAIEmbeddingModel = class {
1707
1715
  constructor(modelId, config) {
1708
1716
  this.specificationVersion = "v2";
1709
1717
  this.maxEmbeddingsPerCall = 2048;
1718
+ this[_a] = 3e5;
1710
1719
  this.supportsParallelCalls = true;
1711
1720
  this.modelId = modelId;
1712
1721
  this.config = config;
@@ -1720,7 +1729,7 @@ var OpenAIEmbeddingModel = class {
1720
1729
  abortSignal,
1721
1730
  providerOptions
1722
1731
  }) {
1723
- var _a;
1732
+ var _a2;
1724
1733
  if (values.length > this.maxEmbeddingsPerCall) {
1725
1734
  throw new TooManyEmbeddingValuesForCallError({
1726
1735
  provider: this.provider,
@@ -1729,11 +1738,11 @@ var OpenAIEmbeddingModel = class {
1729
1738
  values
1730
1739
  });
1731
1740
  }
1732
- const openaiOptions = (_a = await parseProviderOptions3({
1741
+ const openaiOptions = (_a2 = await parseProviderOptions3({
1733
1742
  provider: "openai",
1734
1743
  providerOptions,
1735
1744
  schema: openaiEmbeddingProviderOptions
1736
- })) != null ? _a : {};
1745
+ })) != null ? _a2 : {};
1737
1746
  const {
1738
1747
  responseHeaders,
1739
1748
  value: response,
@@ -1825,8 +1834,8 @@ function hasDefaultResponseFormat(modelId) {
1825
1834
  );
1826
1835
  }
1827
1836
  function getMaxImagesPerCall(modelId) {
1828
- var _a;
1829
- return (_a = modelMaxImagesPerCall[modelId]) != null ? _a : modelId.startsWith("gpt-image-") ? 10 : 1;
1837
+ var _a2;
1838
+ return (_a2 = modelMaxImagesPerCall[modelId]) != null ? _a2 : modelId.startsWith("gpt-image-") ? 10 : 1;
1830
1839
  }
1831
1840
  var baseImageModelOptionsObject = z9.object({
1832
1841
  /**
@@ -1901,7 +1910,7 @@ var OpenAIImageModel = class {
1901
1910
  headers,
1902
1911
  abortSignal
1903
1912
  }) {
1904
- var _a, _b, _c, _d;
1913
+ var _a2, _b, _c, _d;
1905
1914
  const warnings = [];
1906
1915
  if (aspectRatio != null) {
1907
1916
  warnings.push({
@@ -1913,7 +1922,7 @@ var OpenAIImageModel = class {
1913
1922
  if (seed != null) {
1914
1923
  warnings.push({ type: "unsupported-setting", setting: "seed" });
1915
1924
  }
1916
- const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
1925
+ const currentDate = (_c = (_b = (_a2 = this.config._internal) == null ? void 0 : _a2.currentDate) == null ? void 0 : _b.call(_a2)) != null ? _c : /* @__PURE__ */ new Date();
1917
1926
  const openaiOptions = (_d = await parseProviderOptions4({
1918
1927
  provider: "openai",
1919
1928
  providerOptions,
@@ -2388,8 +2397,8 @@ import {
2388
2397
  } from "@ai-sdk/provider-utils";
2389
2398
  import { z as z16 } from "zod/v4";
2390
2399
  function getPromptCacheBreakpoint2(providerOptions) {
2391
- var _a;
2392
- return (_a = providerOptions == null ? void 0 : providerOptions.openai) == null ? void 0 : _a.promptCacheBreakpoint;
2400
+ var _a2;
2401
+ return (_a2 = providerOptions == null ? void 0 : providerOptions.openai) == null ? void 0 : _a2.promptCacheBreakpoint;
2393
2402
  }
2394
2403
  function isFileId(data, prefixes) {
2395
2404
  if (!prefixes) return false;
@@ -2402,7 +2411,7 @@ async function convertToOpenAIResponsesInput({
2402
2411
  store,
2403
2412
  hasLocalShellTool = false
2404
2413
  }) {
2405
- var _a, _b, _c, _d, _e, _f;
2414
+ var _a2, _b, _c, _d, _e, _f;
2406
2415
  let input = [];
2407
2416
  const warnings = [];
2408
2417
  for (const { role, content, providerOptions } of prompt) {
@@ -2457,7 +2466,7 @@ async function convertToOpenAIResponsesInput({
2457
2466
  input.push({
2458
2467
  role: "user",
2459
2468
  content: content.map((part, index) => {
2460
- var _a2, _b2, _c2;
2469
+ var _a3, _b2, _c2;
2461
2470
  switch (part.type) {
2462
2471
  case "text": {
2463
2472
  const promptCacheBreakpoint = getPromptCacheBreakpoint2(
@@ -2482,7 +2491,7 @@ async function convertToOpenAIResponsesInput({
2482
2491
  ...part.data instanceof URL ? { image_url: part.data.toString() } : typeof part.data === "string" && isFileId(part.data, fileIdPrefixes) ? { file_id: part.data } : {
2483
2492
  image_url: `data:${mediaType};base64,${convertToBase642(part.data)}`
2484
2493
  },
2485
- detail: (_b2 = (_a2 = part.providerOptions) == null ? void 0 : _a2.openai) == null ? void 0 : _b2.imageDetail,
2494
+ detail: (_b2 = (_a3 = part.providerOptions) == null ? void 0 : _a3.openai) == null ? void 0 : _b2.imageDetail,
2486
2495
  ...promptCacheBreakpoint != null && {
2487
2496
  prompt_cache_breakpoint: promptCacheBreakpoint
2488
2497
  }
@@ -2524,7 +2533,7 @@ async function convertToOpenAIResponsesInput({
2524
2533
  for (const part of content) {
2525
2534
  switch (part.type) {
2526
2535
  case "text": {
2527
- const id = (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.itemId;
2536
+ const id = (_b = (_a2 = part.providerOptions) == null ? void 0 : _a2.openai) == null ? void 0 : _b.itemId;
2528
2537
  const phase = (_d = (_c = part.providerOptions) == null ? void 0 : _c.openai) == null ? void 0 : _d.phase;
2529
2538
  if (store && id != null) {
2530
2539
  input.push({ type: "item_reference", id });
@@ -3609,7 +3618,7 @@ var OpenAIResponsesLanguageModel = class {
3609
3618
  toolChoice,
3610
3619
  responseFormat
3611
3620
  }) {
3612
- var _a, _b, _c, _d;
3621
+ var _a2, _b, _c, _d;
3613
3622
  const warnings = [];
3614
3623
  const modelCapabilities = getOpenAILanguageModelCapabilities(this.modelId);
3615
3624
  if (topK != null) {
@@ -3649,7 +3658,7 @@ var OpenAIResponsesLanguageModel = class {
3649
3658
  prompt,
3650
3659
  systemMessageMode: modelCapabilities.systemMessageMode,
3651
3660
  fileIdPrefixes: this.config.fileIdPrefixes,
3652
- store: (_a = openaiOptions == null ? void 0 : openaiOptions.store) != null ? _a : true,
3661
+ store: (_a2 = openaiOptions == null ? void 0 : openaiOptions.store) != null ? _a2 : true,
3653
3662
  hasLocalShellTool: hasOpenAITool("openai.local_shell")
3654
3663
  });
3655
3664
  warnings.push(...inputWarnings);
@@ -3827,7 +3836,7 @@ var OpenAIResponsesLanguageModel = class {
3827
3836
  };
3828
3837
  }
3829
3838
  async doGenerate(options) {
3830
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C;
3839
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C;
3831
3840
  const {
3832
3841
  args: body,
3833
3842
  warnings,
@@ -3880,7 +3889,7 @@ var OpenAIResponsesLanguageModel = class {
3880
3889
  providerMetadata: {
3881
3890
  [providerKey]: {
3882
3891
  itemId: part.id,
3883
- reasoningEncryptedContent: (_a = part.encrypted_content) != null ? _a : null
3892
+ reasoningEncryptedContent: (_a2 = part.encrypted_content) != null ? _a2 : null
3884
3893
  }
3885
3894
  }
3886
3895
  });
@@ -4193,7 +4202,7 @@ var OpenAIResponsesLanguageModel = class {
4193
4202
  controller.enqueue({ type: "stream-start", warnings });
4194
4203
  },
4195
4204
  transform(chunk, controller) {
4196
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
4205
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
4197
4206
  if (options.includeRawChunks) {
4198
4207
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4199
4208
  }
@@ -4284,7 +4293,7 @@ var OpenAIResponsesLanguageModel = class {
4284
4293
  });
4285
4294
  } else if (value.item.type === "message") {
4286
4295
  ongoingAnnotations.splice(0, ongoingAnnotations.length);
4287
- activeMessagePhase = (_a = value.item.phase) != null ? _a : void 0;
4296
+ activeMessagePhase = (_a2 = value.item.phase) != null ? _a2 : void 0;
4288
4297
  controller.enqueue({
4289
4298
  type: "text-start",
4290
4299
  id: value.item.id,
@@ -4684,14 +4693,14 @@ function isErrorChunk(chunk) {
4684
4693
  return chunk.type === "error";
4685
4694
  }
4686
4695
  function mapWebSearchOutput(action) {
4687
- var _a;
4696
+ var _a2;
4688
4697
  if (action == null) {
4689
4698
  return {};
4690
4699
  }
4691
4700
  switch (action.type) {
4692
4701
  case "search":
4693
4702
  return {
4694
- action: { type: "search", query: (_a = action.query) != null ? _a : void 0 },
4703
+ action: { type: "search", query: (_a2 = action.query) != null ? _a2 : void 0 },
4695
4704
  // include sources when provided by the Responses API (behind include flag)
4696
4705
  ...action.sources != null && { sources: action.sources }
4697
4706
  };
@@ -4708,8 +4717,8 @@ function mapWebSearchOutput(action) {
4708
4717
  }
4709
4718
  }
4710
4719
  function getResponsesUsageMetadata(usage) {
4711
- var _a, _b, _c, _d;
4712
- const cacheWriteTokens = (_a = usage == null ? void 0 : usage.input_tokens_details) == null ? void 0 : _a.cache_write_tokens;
4720
+ var _a2, _b, _c, _d;
4721
+ const cacheWriteTokens = (_a2 = usage == null ? void 0 : usage.input_tokens_details) == null ? void 0 : _a2.cache_write_tokens;
4713
4722
  const orchestrationInputTokens = (_b = usage == null ? void 0 : usage.input_tokens_details) == null ? void 0 : _b.orchestration_input_tokens;
4714
4723
  const orchestrationInputCachedTokens = (_c = usage == null ? void 0 : usage.input_tokens_details) == null ? void 0 : _c.orchestration_input_cached_tokens;
4715
4724
  const orchestrationOutputTokens = (_d = usage == null ? void 0 : usage.output_tokens_details) == null ? void 0 : _d.orchestration_output_tokens;
@@ -4815,8 +4824,8 @@ var OpenAISpeechModel = class {
4815
4824
  };
4816
4825
  }
4817
4826
  async doGenerate(options) {
4818
- var _a, _b, _c;
4819
- const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
4827
+ var _a2, _b, _c;
4828
+ const currentDate = (_c = (_b = (_a2 = this.config._internal) == null ? void 0 : _a2.currentDate) == null ? void 0 : _b.call(_a2)) != null ? _c : /* @__PURE__ */ new Date();
4820
4829
  const { requestBody, warnings } = await this.getArgs(options);
4821
4830
  const {
4822
4831
  value: audio,
@@ -5050,8 +5059,8 @@ var OpenAITranscriptionModel = class {
5050
5059
  };
5051
5060
  }
5052
5061
  async doGenerate(options) {
5053
- var _a, _b, _c, _d, _e, _f, _g, _h;
5054
- const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
5062
+ var _a2, _b, _c, _d, _e, _f, _g, _h;
5063
+ const currentDate = (_c = (_b = (_a2 = this.config._internal) == null ? void 0 : _a2.currentDate) == null ? void 0 : _b.call(_a2)) != null ? _c : /* @__PURE__ */ new Date();
5055
5064
  const { formData, warnings } = await this.getArgs(options);
5056
5065
  const {
5057
5066
  value: response,
@@ -5097,17 +5106,17 @@ var OpenAITranscriptionModel = class {
5097
5106
  };
5098
5107
 
5099
5108
  // src/version.ts
5100
- var VERSION = true ? "2.0.118" : "0.0.0-test";
5109
+ var VERSION = true ? "2.0.122" : "0.0.0-test";
5101
5110
 
5102
5111
  // src/openai-provider.ts
5103
5112
  function createOpenAI(options = {}) {
5104
- var _a, _b;
5105
- const baseURL = (_a = withoutTrailingSlash(
5113
+ var _a2, _b;
5114
+ const baseURL = (_a2 = withoutTrailingSlash(
5106
5115
  loadOptionalSetting({
5107
5116
  settingValue: options.baseURL,
5108
5117
  environmentVariableName: "OPENAI_BASE_URL"
5109
5118
  })
5110
- )) != null ? _a : "https://api.openai.com/v1";
5119
+ )) != null ? _a2 : "https://api.openai.com/v1";
5111
5120
  const providerName = (_b = options.name) != null ? _b : "openai";
5112
5121
  const getHeaders = () => withUserAgentSuffix(
5113
5122
  {