@ai-sdk/provider-utils 3.0.0-beta.1 → 3.0.0-beta.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 3.0.0-beta.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 0571b98: chore (provider-utils): update eventsource-parser to 3.0.3
8
+ - 39a4fab: fix (provider-utils): detect failed fetch in browser environments
9
+ - d1a034f: feature: using Zod 4 for internal stuff
10
+
3
11
  ## 3.0.0-beta.1
4
12
 
5
13
  ### Major Changes
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
- import { JSONSchema7, JSONValue, JSONParseError, TypeValidationError, APICallError, SharedV2ProviderOptions, LanguageModelV2ToolResultOutput, LanguageModelV2ToolResultPart } from '@ai-sdk/provider';
2
- import { ZodSchema, z } from 'zod';
1
+ import { JSONValue, JSONParseError, TypeValidationError, APICallError, JSONSchema7, SharedV2ProviderOptions, LanguageModelV2ToolResultOutput, LanguageModelV2ToolResultPart } from '@ai-sdk/provider';
2
+ import * as z4 from 'zod/v4';
3
+ import { ZodType, z } from 'zod/v4';
3
4
  import * as z3 from 'zod/v3';
4
- import * as z4 from 'zod/v4/core';
5
5
  import { StandardSchemaV1 } from '@standard-schema/spec';
6
6
  export * from '@standard-schema/spec';
7
7
  export { EventSourceMessage, EventSourceParserStream } from 'eventsource-parser/stream';
@@ -96,45 +96,8 @@ type Validator<OBJECT = unknown> = {
96
96
  */
97
97
  declare function validator<OBJECT>(validate?: undefined | ((value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>)): Validator<OBJECT>;
98
98
  declare function isValidator(value: unknown): value is Validator;
99
- declare function asValidator<OBJECT>(value: Validator<OBJECT> | StandardSchemaV1<OBJECT>): Validator<OBJECT>;
100
- declare function standardSchemaValidator<OBJECT>(standardSchema: StandardSchemaV1<OBJECT>): Validator<OBJECT>;
101
-
102
- /**
103
- * Used to mark schemas so we can support both Zod and custom schemas.
104
- */
105
- declare const schemaSymbol: unique symbol;
106
- type Schema<OBJECT = unknown> = Validator<OBJECT> & {
107
- /**
108
- * Used to mark schemas so we can support both Zod and custom schemas.
109
- */
110
- [schemaSymbol]: true;
111
- /**
112
- * Schema type for inference.
113
- */
114
- _type: OBJECT;
115
- /**
116
- * The JSON Schema for the schema. It is passed to the providers.
117
- */
118
- readonly jsonSchema: JSONSchema7;
119
- };
120
- type FlexibleSchema<T> = z4.$ZodType<T> | z3.Schema<T> | Schema<T>;
121
- type InferSchema<SCHEMA> = SCHEMA extends z3.Schema ? z3.infer<SCHEMA> : SCHEMA extends z4.$ZodType ? z4.infer<SCHEMA> : SCHEMA extends Schema<infer T> ? T : never;
122
- /**
123
- * Create a schema using a JSON Schema.
124
- *
125
- * @param jsonSchema The JSON Schema for the schema.
126
- * @param options.validate Optional. A validation function for the schema.
127
- */
128
- declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7, { validate, }?: {
129
- validate?: (value: unknown) => {
130
- success: true;
131
- value: OBJECT;
132
- } | {
133
- success: false;
134
- error: Error;
135
- };
136
- }): Schema<OBJECT>;
137
- declare function asSchema<OBJECT>(schema: z4.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any> | Schema<OBJECT> | undefined): Schema<OBJECT>;
99
+ declare function asValidator<OBJECT>(value: Validator<OBJECT> | StandardSchemaV1<unknown, OBJECT>): Validator<OBJECT>;
100
+ declare function standardSchemaValidator<OBJECT>(standardSchema: StandardSchemaV1<unknown, OBJECT>): Validator<OBJECT>;
138
101
 
139
102
  /**
140
103
  * Parses a JSON string into an unknown object.
@@ -154,10 +117,10 @@ declare function parseJSON(options: {
154
117
  * @param {Validator<T>} schema - The schema to use for parsing the JSON.
155
118
  * @returns {Promise<T>} - The parsed object.
156
119
  */
157
- declare function parseJSON<VALIDATOR extends z4.$ZodType | z3.Schema | Validator, VALUE = InferSchema<VALIDATOR>>(options: {
120
+ declare function parseJSON<T>(options: {
158
121
  text: string;
159
- schema: VALIDATOR;
160
- }): Promise<VALUE>;
122
+ schema: z4.ZodType<T> | z3.Schema<T> | Validator<T>;
123
+ }): Promise<T>;
161
124
  type ParseResult<T> = {
162
125
  success: true;
163
126
  value: T;
@@ -185,10 +148,10 @@ declare function safeParseJSON(options: {
185
148
  * @param {Validator<T>} schema - The schema to use for parsing the JSON.
186
149
  * @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
187
150
  */
188
- declare function safeParseJSON<SCHEMA extends z4.$ZodType | z3.Schema | Validator, VALUE = InferSchema<SCHEMA>>(options: {
151
+ declare function safeParseJSON<T>(options: {
189
152
  text: string;
190
- schema: SCHEMA;
191
- }): Promise<ParseResult<VALUE>>;
153
+ schema: z4.ZodType<T> | z3.Schema<T> | Validator<T>;
154
+ }): Promise<ParseResult<T>>;
192
155
  declare function isParsableJson(input: string): boolean;
193
156
 
194
157
  type ResponseHandler<RETURN_TYPE> = (options: {
@@ -201,13 +164,13 @@ type ResponseHandler<RETURN_TYPE> = (options: {
201
164
  responseHeaders?: Record<string, string>;
202
165
  }>;
203
166
  declare const createJsonErrorResponseHandler: <T>({ errorSchema, errorToMessage, isRetryable, }: {
204
- errorSchema: ZodSchema<T>;
167
+ errorSchema: ZodType<T>;
205
168
  errorToMessage: (error: T) => string;
206
169
  isRetryable?: (response: Response, error?: T) => boolean;
207
170
  }) => ResponseHandler<APICallError>;
208
- declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodSchema<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
209
- declare const createJsonStreamResponseHandler: <T>(chunkSchema: ZodSchema<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
210
- declare const createJsonResponseHandler: <T>(responseSchema: ZodSchema<T>) => ResponseHandler<T>;
171
+ declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodType<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
172
+ declare const createJsonStreamResponseHandler: <T>(chunkSchema: ZodType<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
173
+ declare const createJsonResponseHandler: <T>(responseSchema: ZodType<T>) => ResponseHandler<T>;
211
174
  declare const createBinaryResponseHandler: () => ResponseHandler<Uint8Array>;
212
175
  declare const createStatusCodeErrorResponseHandler: () => ResponseHandler<APICallError>;
213
176
 
@@ -283,13 +246,13 @@ declare function loadSetting({ settingValue, environmentVariableName, settingNam
283
246
  */
284
247
  declare function parseJsonEventStream<T>({ stream, schema, }: {
285
248
  stream: ReadableStream<Uint8Array>;
286
- schema: ZodSchema<T>;
249
+ schema: ZodType<T>;
287
250
  }): ReadableStream<ParseResult<T>>;
288
251
 
289
252
  declare function parseProviderOptions<T>({ provider, providerOptions, schema, }: {
290
253
  provider: string;
291
254
  providerOptions: Record<string, unknown> | undefined;
292
- schema: z.ZodSchema<T>;
255
+ schema: z.ZodType<T, T>;
293
256
  }): Promise<T | undefined>;
294
257
 
295
258
  declare const postJsonToApi: <T>({ url, headers, body, failedResponseHandler, successfulResponseHandler, abortSignal, fetch, }: {
@@ -335,6 +298,43 @@ declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, fa
335
298
  responseHeaders?: Record<string, string>;
336
299
  }>;
337
300
 
301
+ /**
302
+ * Used to mark schemas so we can support both Zod and custom schemas.
303
+ */
304
+ declare const schemaSymbol: unique symbol;
305
+ type Schema<OBJECT = unknown> = Validator<OBJECT> & {
306
+ /**
307
+ * Used to mark schemas so we can support both Zod and custom schemas.
308
+ */
309
+ [schemaSymbol]: true;
310
+ /**
311
+ * Schema type for inference.
312
+ */
313
+ _type: OBJECT;
314
+ /**
315
+ * The JSON Schema for the schema. It is passed to the providers.
316
+ */
317
+ readonly jsonSchema: JSONSchema7;
318
+ };
319
+ type FlexibleSchema<T> = z4.ZodType<T> | z3.Schema<T> | Schema<T>;
320
+ type InferSchema<SCHEMA> = SCHEMA extends z3.Schema ? z3.infer<SCHEMA> : SCHEMA extends z4.ZodType ? z4.infer<SCHEMA> : SCHEMA extends Schema<infer T> ? T : never;
321
+ /**
322
+ * Create a schema using a JSON Schema.
323
+ *
324
+ * @param jsonSchema The JSON Schema for the schema.
325
+ * @param options.validate Optional. A validation function for the schema.
326
+ */
327
+ declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7, { validate, }?: {
328
+ validate?: (value: unknown) => {
329
+ success: true;
330
+ value: OBJECT;
331
+ } | {
332
+ success: false;
333
+ error: Error;
334
+ };
335
+ }): Schema<OBJECT>;
336
+ declare function asSchema<OBJECT>(schema: z4.ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any> | Schema<OBJECT> | undefined): Schema<OBJECT>;
337
+
338
338
  /**
339
339
  Additional provider-specific options.
340
340
 
@@ -742,7 +742,7 @@ declare function convertToBase64(value: string | Uint8Array): string;
742
742
  */
743
743
  declare function validateTypes<OBJECT>({ value, schema, }: {
744
744
  value: unknown;
745
- schema: StandardSchemaV1<OBJECT> | Validator<OBJECT>;
745
+ schema: StandardSchemaV1<unknown, OBJECT> | Validator<OBJECT>;
746
746
  }): Promise<OBJECT>;
747
747
  /**
748
748
  * Safely validates the types of an unknown object using a schema and
@@ -755,7 +755,7 @@ declare function validateTypes<OBJECT>({ value, schema, }: {
755
755
  */
756
756
  declare function safeValidateTypes<OBJECT>({ value, schema, }: {
757
757
  value: unknown;
758
- schema: StandardSchemaV1<OBJECT> | Validator<OBJECT>;
758
+ schema: StandardSchemaV1<unknown, OBJECT> | Validator<OBJECT>;
759
759
  }): Promise<{
760
760
  success: true;
761
761
  value: OBJECT;
@@ -768,7 +768,7 @@ declare function safeValidateTypes<OBJECT>({ value, schema, }: {
768
768
 
769
769
  declare function withoutTrailingSlash(url: string | undefined): string | undefined;
770
770
 
771
- declare function zodSchema<OBJECT>(zodSchema: z4.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
771
+ declare function zodSchema<OBJECT>(zodSchema: z4.ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
772
772
  /**
773
773
  * Enables support for references in the schema.
774
774
  * This is required for recursive schemas, e.g. with `z.lazy`.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { JSONSchema7, JSONValue, JSONParseError, TypeValidationError, APICallError, SharedV2ProviderOptions, LanguageModelV2ToolResultOutput, LanguageModelV2ToolResultPart } from '@ai-sdk/provider';
2
- import { ZodSchema, z } from 'zod';
1
+ import { JSONValue, JSONParseError, TypeValidationError, APICallError, JSONSchema7, SharedV2ProviderOptions, LanguageModelV2ToolResultOutput, LanguageModelV2ToolResultPart } from '@ai-sdk/provider';
2
+ import * as z4 from 'zod/v4';
3
+ import { ZodType, z } from 'zod/v4';
3
4
  import * as z3 from 'zod/v3';
4
- import * as z4 from 'zod/v4/core';
5
5
  import { StandardSchemaV1 } from '@standard-schema/spec';
6
6
  export * from '@standard-schema/spec';
7
7
  export { EventSourceMessage, EventSourceParserStream } from 'eventsource-parser/stream';
@@ -96,45 +96,8 @@ type Validator<OBJECT = unknown> = {
96
96
  */
97
97
  declare function validator<OBJECT>(validate?: undefined | ((value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>)): Validator<OBJECT>;
98
98
  declare function isValidator(value: unknown): value is Validator;
99
- declare function asValidator<OBJECT>(value: Validator<OBJECT> | StandardSchemaV1<OBJECT>): Validator<OBJECT>;
100
- declare function standardSchemaValidator<OBJECT>(standardSchema: StandardSchemaV1<OBJECT>): Validator<OBJECT>;
101
-
102
- /**
103
- * Used to mark schemas so we can support both Zod and custom schemas.
104
- */
105
- declare const schemaSymbol: unique symbol;
106
- type Schema<OBJECT = unknown> = Validator<OBJECT> & {
107
- /**
108
- * Used to mark schemas so we can support both Zod and custom schemas.
109
- */
110
- [schemaSymbol]: true;
111
- /**
112
- * Schema type for inference.
113
- */
114
- _type: OBJECT;
115
- /**
116
- * The JSON Schema for the schema. It is passed to the providers.
117
- */
118
- readonly jsonSchema: JSONSchema7;
119
- };
120
- type FlexibleSchema<T> = z4.$ZodType<T> | z3.Schema<T> | Schema<T>;
121
- type InferSchema<SCHEMA> = SCHEMA extends z3.Schema ? z3.infer<SCHEMA> : SCHEMA extends z4.$ZodType ? z4.infer<SCHEMA> : SCHEMA extends Schema<infer T> ? T : never;
122
- /**
123
- * Create a schema using a JSON Schema.
124
- *
125
- * @param jsonSchema The JSON Schema for the schema.
126
- * @param options.validate Optional. A validation function for the schema.
127
- */
128
- declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7, { validate, }?: {
129
- validate?: (value: unknown) => {
130
- success: true;
131
- value: OBJECT;
132
- } | {
133
- success: false;
134
- error: Error;
135
- };
136
- }): Schema<OBJECT>;
137
- declare function asSchema<OBJECT>(schema: z4.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any> | Schema<OBJECT> | undefined): Schema<OBJECT>;
99
+ declare function asValidator<OBJECT>(value: Validator<OBJECT> | StandardSchemaV1<unknown, OBJECT>): Validator<OBJECT>;
100
+ declare function standardSchemaValidator<OBJECT>(standardSchema: StandardSchemaV1<unknown, OBJECT>): Validator<OBJECT>;
138
101
 
139
102
  /**
140
103
  * Parses a JSON string into an unknown object.
@@ -154,10 +117,10 @@ declare function parseJSON(options: {
154
117
  * @param {Validator<T>} schema - The schema to use for parsing the JSON.
155
118
  * @returns {Promise<T>} - The parsed object.
156
119
  */
157
- declare function parseJSON<VALIDATOR extends z4.$ZodType | z3.Schema | Validator, VALUE = InferSchema<VALIDATOR>>(options: {
120
+ declare function parseJSON<T>(options: {
158
121
  text: string;
159
- schema: VALIDATOR;
160
- }): Promise<VALUE>;
122
+ schema: z4.ZodType<T> | z3.Schema<T> | Validator<T>;
123
+ }): Promise<T>;
161
124
  type ParseResult<T> = {
162
125
  success: true;
163
126
  value: T;
@@ -185,10 +148,10 @@ declare function safeParseJSON(options: {
185
148
  * @param {Validator<T>} schema - The schema to use for parsing the JSON.
186
149
  * @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
187
150
  */
188
- declare function safeParseJSON<SCHEMA extends z4.$ZodType | z3.Schema | Validator, VALUE = InferSchema<SCHEMA>>(options: {
151
+ declare function safeParseJSON<T>(options: {
189
152
  text: string;
190
- schema: SCHEMA;
191
- }): Promise<ParseResult<VALUE>>;
153
+ schema: z4.ZodType<T> | z3.Schema<T> | Validator<T>;
154
+ }): Promise<ParseResult<T>>;
192
155
  declare function isParsableJson(input: string): boolean;
193
156
 
194
157
  type ResponseHandler<RETURN_TYPE> = (options: {
@@ -201,13 +164,13 @@ type ResponseHandler<RETURN_TYPE> = (options: {
201
164
  responseHeaders?: Record<string, string>;
202
165
  }>;
203
166
  declare const createJsonErrorResponseHandler: <T>({ errorSchema, errorToMessage, isRetryable, }: {
204
- errorSchema: ZodSchema<T>;
167
+ errorSchema: ZodType<T>;
205
168
  errorToMessage: (error: T) => string;
206
169
  isRetryable?: (response: Response, error?: T) => boolean;
207
170
  }) => ResponseHandler<APICallError>;
208
- declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodSchema<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
209
- declare const createJsonStreamResponseHandler: <T>(chunkSchema: ZodSchema<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
210
- declare const createJsonResponseHandler: <T>(responseSchema: ZodSchema<T>) => ResponseHandler<T>;
171
+ declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodType<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
172
+ declare const createJsonStreamResponseHandler: <T>(chunkSchema: ZodType<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
173
+ declare const createJsonResponseHandler: <T>(responseSchema: ZodType<T>) => ResponseHandler<T>;
211
174
  declare const createBinaryResponseHandler: () => ResponseHandler<Uint8Array>;
212
175
  declare const createStatusCodeErrorResponseHandler: () => ResponseHandler<APICallError>;
213
176
 
@@ -283,13 +246,13 @@ declare function loadSetting({ settingValue, environmentVariableName, settingNam
283
246
  */
284
247
  declare function parseJsonEventStream<T>({ stream, schema, }: {
285
248
  stream: ReadableStream<Uint8Array>;
286
- schema: ZodSchema<T>;
249
+ schema: ZodType<T>;
287
250
  }): ReadableStream<ParseResult<T>>;
288
251
 
289
252
  declare function parseProviderOptions<T>({ provider, providerOptions, schema, }: {
290
253
  provider: string;
291
254
  providerOptions: Record<string, unknown> | undefined;
292
- schema: z.ZodSchema<T>;
255
+ schema: z.ZodType<T, T>;
293
256
  }): Promise<T | undefined>;
294
257
 
295
258
  declare const postJsonToApi: <T>({ url, headers, body, failedResponseHandler, successfulResponseHandler, abortSignal, fetch, }: {
@@ -335,6 +298,43 @@ declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, fa
335
298
  responseHeaders?: Record<string, string>;
336
299
  }>;
337
300
 
301
+ /**
302
+ * Used to mark schemas so we can support both Zod and custom schemas.
303
+ */
304
+ declare const schemaSymbol: unique symbol;
305
+ type Schema<OBJECT = unknown> = Validator<OBJECT> & {
306
+ /**
307
+ * Used to mark schemas so we can support both Zod and custom schemas.
308
+ */
309
+ [schemaSymbol]: true;
310
+ /**
311
+ * Schema type for inference.
312
+ */
313
+ _type: OBJECT;
314
+ /**
315
+ * The JSON Schema for the schema. It is passed to the providers.
316
+ */
317
+ readonly jsonSchema: JSONSchema7;
318
+ };
319
+ type FlexibleSchema<T> = z4.ZodType<T> | z3.Schema<T> | Schema<T>;
320
+ type InferSchema<SCHEMA> = SCHEMA extends z3.Schema ? z3.infer<SCHEMA> : SCHEMA extends z4.ZodType ? z4.infer<SCHEMA> : SCHEMA extends Schema<infer T> ? T : never;
321
+ /**
322
+ * Create a schema using a JSON Schema.
323
+ *
324
+ * @param jsonSchema The JSON Schema for the schema.
325
+ * @param options.validate Optional. A validation function for the schema.
326
+ */
327
+ declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7, { validate, }?: {
328
+ validate?: (value: unknown) => {
329
+ success: true;
330
+ value: OBJECT;
331
+ } | {
332
+ success: false;
333
+ error: Error;
334
+ };
335
+ }): Schema<OBJECT>;
336
+ declare function asSchema<OBJECT>(schema: z4.ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any> | Schema<OBJECT> | undefined): Schema<OBJECT>;
337
+
338
338
  /**
339
339
  Additional provider-specific options.
340
340
 
@@ -742,7 +742,7 @@ declare function convertToBase64(value: string | Uint8Array): string;
742
742
  */
743
743
  declare function validateTypes<OBJECT>({ value, schema, }: {
744
744
  value: unknown;
745
- schema: StandardSchemaV1<OBJECT> | Validator<OBJECT>;
745
+ schema: StandardSchemaV1<unknown, OBJECT> | Validator<OBJECT>;
746
746
  }): Promise<OBJECT>;
747
747
  /**
748
748
  * Safely validates the types of an unknown object using a schema and
@@ -755,7 +755,7 @@ declare function validateTypes<OBJECT>({ value, schema, }: {
755
755
  */
756
756
  declare function safeValidateTypes<OBJECT>({ value, schema, }: {
757
757
  value: unknown;
758
- schema: StandardSchemaV1<OBJECT> | Validator<OBJECT>;
758
+ schema: StandardSchemaV1<unknown, OBJECT> | Validator<OBJECT>;
759
759
  }): Promise<{
760
760
  success: true;
761
761
  value: OBJECT;
@@ -768,7 +768,7 @@ declare function safeValidateTypes<OBJECT>({ value, schema, }: {
768
768
 
769
769
  declare function withoutTrailingSlash(url: string | undefined): string | undefined;
770
770
 
771
- declare function zodSchema<OBJECT>(zodSchema: z4.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
771
+ declare function zodSchema<OBJECT>(zodSchema: z4.ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
772
772
  /**
773
773
  * Enables support for references in the schema.
774
774
  * This is required for recursive schemas, e.g. with `z.lazy`.