Skip to content

Commit 57edfcb

Browse files
authored
fix(@ai-sdk/provider-utils): Use safeParseAsync for zod schemas (#7389)
## Background To support `async` `refine` calls ## Summary Changed calls of `safeParse` to `safeParseAsync`. Calls to `validate` were already `await`ed. ## Verification `pnpm link`ed into another project where I added a tool with an `async` `refine` in its `inputSchema`. Without this change, it throws an error. ## Related Issues Fixes #6868
1 parent 5aa62ce commit 57edfcb

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

‎.changeset/sour-mails-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/provider-utils': patch
3+
---
4+
5+
Adds support for async zod validators

‎packages/provider-utils/src/schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Validator, validatorSymbol } from './validator';
1+
import { Validator, validatorSymbol, type ValidationResult } from './validator';
22
import { JSONSchema7 } from '@ai-sdk/provider';
33
import * as z3 from 'zod/v3';
44
import * as z4 from 'zod/v4';
@@ -49,7 +49,7 @@ export function jsonSchema<OBJECT = unknown>(
4949
}: {
5050
validate?: (
5151
value: unknown,
52-
) => { success: true; value: OBJECT } | { success: false; error: Error };
52+
) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>;
5353
} = {},
5454
): Schema<OBJECT> {
5555
return {

‎packages/provider-utils/src/zod-schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export function zod3Schema<OBJECT>(
2525
target: 'jsonSchema7', // note: openai mode breaks various gemini conversions
2626
}) as JSONSchema7,
2727
{
28-
validate: value => {
29-
const result = zodSchema.safeParse(value);
28+
validate: async value => {
29+
const result = await zodSchema.safeParseAsync(value);
3030
return result.success
3131
? { success: true, value: result.data }
3232
: { success: false, error: result.error };
@@ -57,8 +57,8 @@ export function zod4Schema<OBJECT>(
5757
}) as JSONSchema7;
5858

5959
return jsonSchema(z4JSONSchema, {
60-
validate: value => {
61-
const result = z4.safeParse(zodSchema, value);
60+
validate: async value => {
61+
const result = await z4.safeParseAsync(zodSchema, value);
6262
return result.success
6363
? { success: true, value: result.data }
6464
: { success: false, error: result.error };

0 commit comments

Comments
 (0)