mastra-ai/mastra · error

StandardSchemaWithJSON is not supported for applyCompatLayer

Error message

StandardSchemaWithJSON is not supported for applyCompatLayer and aiSdkSchema mode

What it means

applyCompatLayer() with aiSdkSchema mode normalizes a schema into Zod (applying provider compatibility fixes, e.g. for Gemini) and then converts back to an AI SDK Schema. Its conversion path only understands ZodType instances and Schema/JSONSchema7 objects; StandardSchemaWithJSON (Standard Schema objects that carry their own JSON representation) cannot round-trip through the Zod-based compat layer, so it is explicitly rejected with this error.

Source

Thrown at packages/schema-compat/src/utils.ts:212

    const standardSchema = toStandardSchema(schema);

    for (const compat of compatLayers) {
      if (compat.shouldApply()) {
        const compatSchema = compat.processToCompatSchema(standardSchema);

        return standardSchemaToJSONSchema(compatSchema);
      }
    }

    return standardSchemaToJSONSchema(standardSchema);
  } else {
    let zodSchema: ZodSchema;

    if (isZodType(schema)) {
      zodSchema = schema;
    } else {
      if (isStandardSchemaWithJSON(schema)) {
        throw new Error('StandardSchemaWithJSON is not supported for applyCompatLayer and aiSdkSchema mode');
      }

      // Convert non-zod schema to Zod
      // After ruling out ZodType and StandardSchemaWithJSON above, remaining PublicSchema
      // types are Schema (v4/v5/v6) and JSONSchema7, all handled by convertSchemaToZod
      zodSchema = convertSchemaToZod(schema as Schema | JSONSchema7);
    }

    for (const compat of compatLayers) {
      if (compat.shouldApply()) {
        return compat.processToAISDKSchema(zodSchema);
      }
    }

    return convertZodSchemaToAISDKSchema(zodSchema);
  }
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Convert the StandardSchemaWithJSON to a plain JSON Schema (use its jsonSchema/~standard.jsonSchema.output) and pass the JSON Schema instead.
  2. Pass a native Zod (v3 or v4) schema to applyCompatLayer, which it can handle directly.
  3. Use the standard-schema (non-aiSdkSchema) path of toStandardSchema instead, which supports StandardSchemaWithJSON.
  4. Downgrade/recreate the schema with Zod v3 if the Gemini compat path requires it.

Example fix

// before
applyCompatLayer({ schema: myStandardSchemaWithJSON, mode: 'aiSdkSchema' })
// after
applyCompatLayer({ schema: z.object({ query: z.string() }), mode: 'aiSdkSchema' })
Defensive patterns

Strategy: type-guard

Validate before calling

if (schema && typeof schema === 'object' && '~standard' in schema && !('_def' in schema) && !('_zod' in schema)) {
  // StandardSchemaWithJSON — convert to JSON Schema first before applyCompatLayer aiSdkSchema mode
  schema = extractJsonSchema(schema);
}

Type guard

const isZodSchema = (s: unknown): boolean =>
  typeof s === 'object' && s !== null && ('_def' in s || '_zod' in s);

Try / catch

let safeSchema = schema;
if (isStandardSchemaWithJSON(schema)) {
  safeSchema = schema.jsonSchema ?? toStandardSchemaJsonSchema(schema);
}
const result = applyCompatLayer({ schema: safeSchema, mode: 'aiSdkSchema' });

Prevention

When it happens

Trigger: Calling applyCompatLayer({ schema, mode: 'aiSdkSchema' }) — directly or via applyGeminiSchemaCompatToTools/_applySchemaCompat/jsonSchema/build — with a StandardSchemaWithJSON schema (e.g. a Standard Schema from Zod v4/Valibot/ArkType) instead of a ZodType or JSON Schema object.

Common situations: Using Gemini models with tools whose parameters are Standard Schema objects from Zod v4 or another Standard-Schema-compliant library; mixing schema styles across tools where some are zod v3 and some are standard-schema objects.

Related errors


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