@ai-sdk/provider-utils 3.0.33 → 3.0.36

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,24 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 3.0.36
4
+
5
+ ### Patch Changes
6
+
7
+ - 26165ee: Mark transient network errors that occur while reading successful response bodies as retryable, including AI Gateway responses.
8
+
9
+ ## 3.0.35
10
+
11
+ ### Patch Changes
12
+
13
+ - 77d33c0: Split OpenAI and Azure OpenAI embedding requests by a conservative UTF-8 byte budget derived from their aggregate token limit, in addition to input count limits.
14
+
15
+ ## 3.0.34
16
+
17
+ ### Patch Changes
18
+
19
+ - 2fff9f1: fix(provider-utils): allow imports in runtimes without a global fetch function
20
+ - f364ea0: Preserve schema-valued additional properties when converting Zod 4 schemas.
21
+
3
22
  ## 3.0.33
4
23
 
5
24
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -72,6 +72,14 @@ declare class DownloadError extends AISDKError {
72
72
  static isInstance(error: unknown): error is DownloadError;
73
73
  }
74
74
 
75
+ /**
76
+ * Symbol for exposing the UTF-8 input byte budget of an embedding model.
77
+ *
78
+ * This capability is experimental and intentionally lives outside the versioned
79
+ * embedding model specification.
80
+ */
81
+ declare const EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL: unique symbol;
82
+
75
83
  /**
76
84
  * Fetches a URL while enforcing the SSRF download guard on every hop.
77
85
  *
@@ -1090,4 +1098,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1090
1098
  dynamic?: boolean;
1091
1099
  }
1092
1100
 
1093
- export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type FlexibleValidator, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type InferValidator, type LazySchema, type LazyValidator, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, type Validator, asSchema, asValidator, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isParsableJson, isSameOrigin, isUrlSupported, isValidator, jsonSchema, lazySchema, lazyValidator, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateDownloadUrl, validateTypes, validator, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1101
+ export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, type FetchFunction, type FilePart, type FlexibleSchema, type FlexibleValidator, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type InferValidator, type LazySchema, type LazyValidator, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, type Validator, asSchema, asValidator, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isParsableJson, isSameOrigin, isUrlSupported, isValidator, jsonSchema, lazySchema, lazyValidator, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateDownloadUrl, validateTypes, validator, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.d.ts CHANGED
@@ -72,6 +72,14 @@ declare class DownloadError extends AISDKError {
72
72
  static isInstance(error: unknown): error is DownloadError;
73
73
  }
74
74
 
75
+ /**
76
+ * Symbol for exposing the UTF-8 input byte budget of an embedding model.
77
+ *
78
+ * This capability is experimental and intentionally lives outside the versioned
79
+ * embedding model specification.
80
+ */
81
+ declare const EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL: unique symbol;
82
+
75
83
  /**
76
84
  * Fetches a URL while enforcing the SSRF download guard on every hop.
77
85
  *
@@ -1090,4 +1098,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1090
1098
  dynamic?: boolean;
1091
1099
  }
1092
1100
 
1093
- export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type FlexibleValidator, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type InferValidator, type LazySchema, type LazyValidator, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, type Validator, asSchema, asValidator, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isParsableJson, isSameOrigin, isUrlSupported, isValidator, jsonSchema, lazySchema, lazyValidator, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateDownloadUrl, validateTypes, validator, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1101
+ export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, type FetchFunction, type FilePart, type FlexibleSchema, type FlexibleValidator, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type InferValidator, type LazySchema, type LazyValidator, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, type Validator, asSchema, asValidator, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isParsableJson, isSameOrigin, isUrlSupported, isValidator, jsonSchema, lazySchema, lazyValidator, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateDownloadUrl, validateTypes, validator, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.js CHANGED
@@ -34,6 +34,7 @@ __export(index_exports, {
34
34
  DEFAULT_MAX_DOWNLOAD_SIZE: () => DEFAULT_MAX_DOWNLOAD_SIZE,
35
35
  DelayedPromise: () => DelayedPromise,
36
36
  DownloadError: () => DownloadError,
37
+ EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL: () => EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL,
37
38
  EventSourceParserStream: () => import_stream2.EventSourceParserStream,
38
39
  VERSION: () => VERSION,
39
40
  asSchema: () => asSchema,
@@ -255,6 +256,11 @@ var DownloadError = class extends (_b = import_provider.AISDKError, _a = symbol,
255
256
  }
256
257
  };
257
258
 
259
+ // src/embedding-model-capabilities.ts
260
+ var EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL = /* @__PURE__ */ Symbol.for(
261
+ "vercel.ai.embeddingModel.maxInputBytesPerCall"
262
+ );
263
+
258
264
  // src/cancel-response-body.ts
259
265
  async function cancelResponseBody(response) {
260
266
  var _a2;
@@ -469,6 +475,9 @@ async function getDefaultDownloadFetch() {
469
475
  return safeNodeFetchPromise != null ? safeNodeFetchPromise : safeNodeFetchPromise = createSafeNodeFetch();
470
476
  }
471
477
  function isNodeDefaultFetch(fetch) {
478
+ if (typeof fetch !== "function") {
479
+ return false;
480
+ }
472
481
  const source = Function.prototype.toString.call(fetch);
473
482
  return source.includes("internal/deps/undici") || source.includes("lazy loading of undici");
474
483
  }
@@ -676,6 +685,32 @@ function isAbortError(error) {
676
685
 
677
686
  // src/handle-fetch-error.ts
678
687
  var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
688
+ var RETRYABLE_NETWORK_ERROR_CODES = /* @__PURE__ */ new Set([
689
+ "ConnectionRefused",
690
+ "ConnectionClosed",
691
+ "FailedToOpenSocket",
692
+ "ECONNRESET",
693
+ "ECONNREFUSED",
694
+ "ETIMEDOUT",
695
+ "EPIPE",
696
+ "UND_ERR_SOCKET",
697
+ "UND_ERR_HEADERS_TIMEOUT",
698
+ "UND_ERR_BODY_TIMEOUT",
699
+ "UND_ERR_CONNECT_TIMEOUT"
700
+ ]);
701
+ function findNetworkError(error) {
702
+ const visited = /* @__PURE__ */ new Set();
703
+ let current = error;
704
+ while (current instanceof Error && !visited.has(current)) {
705
+ visited.add(current);
706
+ const errorWithCode = current;
707
+ if (typeof errorWithCode.code === "string" && RETRYABLE_NETWORK_ERROR_CODES.has(errorWithCode.code)) {
708
+ return errorWithCode;
709
+ }
710
+ current = current.cause;
711
+ }
712
+ return void 0;
713
+ }
679
714
  function handleFetchError({
680
715
  error,
681
716
  url,
@@ -697,6 +732,29 @@ function handleFetchError({
697
732
  });
698
733
  }
699
734
  }
735
+ const networkError = findNetworkError(error);
736
+ if (networkError != null) {
737
+ if (import_provider3.APICallError.isInstance(error)) {
738
+ return new import_provider3.APICallError({
739
+ message: error.message,
740
+ cause: error.cause,
741
+ url: error.url,
742
+ requestBodyValues: error.requestBodyValues,
743
+ statusCode: error.statusCode,
744
+ responseHeaders: error.responseHeaders,
745
+ responseBody: error.responseBody,
746
+ data: error.data,
747
+ isRetryable: true
748
+ });
749
+ }
750
+ return new import_provider3.APICallError({
751
+ message: `Cannot connect to API: ${error instanceof Error ? error.message : networkError.message}`,
752
+ cause: error,
753
+ url,
754
+ requestBodyValues,
755
+ isRetryable: true
756
+ });
757
+ }
700
758
  return error;
701
759
  }
702
760
 
@@ -753,7 +811,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
753
811
  }
754
812
 
755
813
  // src/version.ts
756
- var VERSION = true ? "3.0.33" : "0.0.0-test";
814
+ var VERSION = true ? "3.0.36" : "0.0.0-test";
757
815
 
758
816
  // src/get-from-api.ts
759
817
  var getOriginalFetch = () => globalThis.fetch;
@@ -1400,6 +1458,62 @@ async function resolve(value) {
1400
1458
  // src/response-handler.ts
1401
1459
  var import_provider12 = require("@ai-sdk/provider");
1402
1460
  var textDecoder = new TextDecoder();
1461
+ function wrapResponseBodyStream({
1462
+ stream,
1463
+ url,
1464
+ requestBodyValues,
1465
+ statusCode,
1466
+ responseHeaders
1467
+ }) {
1468
+ const reader = stream.getReader();
1469
+ let readerReleased = false;
1470
+ const releaseReader = () => {
1471
+ if (!readerReleased) {
1472
+ reader.releaseLock();
1473
+ readerReleased = true;
1474
+ }
1475
+ };
1476
+ return new ReadableStream({
1477
+ async pull(controller) {
1478
+ try {
1479
+ const { done, value } = await reader.read();
1480
+ if (done) {
1481
+ releaseReader();
1482
+ controller.close();
1483
+ } else {
1484
+ controller.enqueue(value);
1485
+ }
1486
+ } catch (error) {
1487
+ releaseReader();
1488
+ if (isAbortError(error)) {
1489
+ controller.error(error);
1490
+ return;
1491
+ }
1492
+ controller.error(
1493
+ handleFetchError({
1494
+ error: new import_provider12.APICallError({
1495
+ message: "Failed to process successful response",
1496
+ cause: error,
1497
+ statusCode,
1498
+ url,
1499
+ responseHeaders,
1500
+ requestBodyValues
1501
+ }),
1502
+ url,
1503
+ requestBodyValues
1504
+ })
1505
+ );
1506
+ }
1507
+ },
1508
+ async cancel(reason) {
1509
+ try {
1510
+ await reader.cancel(reason);
1511
+ } finally {
1512
+ releaseReader();
1513
+ }
1514
+ }
1515
+ });
1516
+ }
1403
1517
  async function readResponseBodyAsText({
1404
1518
  response,
1405
1519
  url
@@ -1465,7 +1579,7 @@ var createJsonErrorResponseHandler = ({
1465
1579
  };
1466
1580
  }
1467
1581
  };
1468
- var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
1582
+ var createEventSourceResponseHandler = (chunkSchema) => async ({ response, url, requestBodyValues }) => {
1469
1583
  const responseHeaders = extractResponseHeaders(response);
1470
1584
  if (response.body == null) {
1471
1585
  throw new import_provider12.EmptyResponseBodyError({});
@@ -1473,7 +1587,13 @@ var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) =>
1473
1587
  return {
1474
1588
  responseHeaders,
1475
1589
  value: parseJsonEventStream({
1476
- stream: response.body,
1590
+ stream: wrapResponseBodyStream({
1591
+ stream: response.body,
1592
+ url,
1593
+ requestBodyValues,
1594
+ statusCode: response.status,
1595
+ responseHeaders
1596
+ }),
1477
1597
  schema: chunkSchema
1478
1598
  })
1479
1599
  };
@@ -1610,7 +1730,8 @@ var z4 = __toESM(require("zod/v4"));
1610
1730
  // src/add-additional-properties-to-json-schema.ts
1611
1731
  function addAdditionalPropertiesToJsonSchema(jsonSchema2) {
1612
1732
  if (jsonSchema2.type === "object") {
1613
- jsonSchema2.additionalProperties = false;
1733
+ const { additionalProperties } = jsonSchema2;
1734
+ jsonSchema2.additionalProperties = additionalProperties != null && typeof additionalProperties !== "boolean" ? addAdditionalPropertiesToJsonSchema(additionalProperties) : false;
1614
1735
  const properties = jsonSchema2.properties;
1615
1736
  if (properties != null) {
1616
1737
  for (const property in properties) {
@@ -2929,6 +3050,7 @@ var import_stream2 = require("eventsource-parser/stream");
2929
3050
  DEFAULT_MAX_DOWNLOAD_SIZE,
2930
3051
  DelayedPromise,
2931
3052
  DownloadError,
3053
+ EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL,
2932
3054
  EventSourceParserStream,
2933
3055
  VERSION,
2934
3056
  asSchema,