mastra-ai/mastra · error

Async validation is not supported

Error message

Async validation is not supported

What it means

When converting a Standard Schema-compatible schema to an AI SDK schema, processToAISDKSchema installs a synchronous `validate` callback. Standard Schema validation may be async (returns a Promise); the AI SDK's jsonSchema() validate hook is synchronous, so the library throws rather than silently dropping validation.

Source

Thrown at packages/schema-compat/src/provider-compats/anthropic.ts:125

        .any()
        .refine(v => v === null, { message: 'must be null' })
        .describe(value.description || 'must be null');
    } else if (isIntersection(z)(value)) {
      return this.defaultZodIntersectionHandler(value);
    }

    return this.defaultUnsupportedZodTypeHandler(value);
  }

  processToAISDKSchema(zodSchema: ZodTypeV3 | ZodTypeV4) {
    const compat = this.processToCompatSchema(zodSchema);
    const transformedJsonSchema = standardSchemaToJSONSchema(compat);

    return jsonSchema(transformedJsonSchema, {
      validate: (value: unknown) => {
        const result = compat['~standard'].validate(value);
        if (result instanceof Promise) {
          throw new Error('Async validation is not supported');
        }
        return 'issues' in result && result.issues
          ? { success: false as const, error: new Error(result.issues.map(i => i.message).join(', ')) }
          : { success: true as const, value: (result as { value: unknown }).value };
      },
    });
  }

  public processToCompatSchema<T>(schema: PublicSchema<T>): StandardSchemaWithJSON<T> {
    const originalStandardSchema = toStandardSchema(schema);
    const validationStandardSchema = this.#getCompatValidationStandardSchema(schema, originalStandardSchema);

    return {
      '~standard': {
        version: 1,
        vendor: 'mastra',
        validate: (value: unknown) => {
          const transformedJsonSchema = this.processToJSONSchema(schema, 'input') as Record<string, unknown>;

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Remove async refinements/transforms from the schema so validation is synchronous.
  2. Pre-validate the value yourself before calling the AI SDK, using the schema's async validate, and pass an already-valid value.
  3. Use a synchronous-only schema library or version for the structured-output path.
  4. Wrap a sync subset schema for the model and enforce async rules in application code after the response.

Example fix

// before
const schema = z.object({ id: z.string().refine(async id => checkExists(id)) });
// after
const schema = z.object({ id: z.string() });
// check existence asynchronously in your own code before/after the model call
Defensive patterns

Strategy: validation

Validate before calling

const probe = schema['~standard'].validate({});
if (probe instanceof Promise) {
  throw new TypeError('Schema uses async validation; provide a synchronous schema for AI SDK structured output');
}

Type guard

function isSyncStandardSchema(s: { '~standard': { validate: (v: unknown) => unknown } }): boolean {
  return !(s['~standard'].validate({}) instanceof Promise);
}

Try / catch

try {
  converted = processToAISDKSchema(schema);
} catch (e) {
  if ((e as Error).message === 'Async validation is not supported') {
    converted = processToAISDKSchema(stripAsyncRefinements(schema));
  } else throw e;
}

Prevention

When it happens

Trigger: Using a schema whose `~standard.validate` returns a Promise — e.g. a Zod schema wrapped through an async-transforming adapter, a custom Standard Schema implementation with async refinements/transformations, or schemas using `z.string().refine(async ...)`.

Common situations: Migrating to the AI SDK path while keeping schemas with async refinements; third-party schema libraries that only implement async validation; combining structured output with schema transforms that are inherently async.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/c9be66b2b569319b. Report an issue: GitHub.