mastra-ai/mastra · error
Unsupported schema type: function (schema libraries should i
Error message
Unsupported schema type: function (schema libraries should implement StandardSchemaWithJSON)
What it means
After toStandardSchema() rules out null/primitives, it checks functions: a bare function is not a recognized schema representation. Only StandardSchemaWithJSON functions (which expose a '~standard' and 'jsonSchema' interface) are accepted. A plain function such as a bare zod schema factory, a class constructor, or a schema-producing callback is rejected with this message telling library authors to implement StandardSchemaWithJSON.
Source
Thrown at packages/schema-compat/src/standard-schema/standard-schema.ts:179
// Zod v4 schemas are also instanceof z3.ZodType due to prototype compatibility
if (isZodV3(schema)) {
return toStandardSchemaZodV3(schema as ZodType);
}
// Check for AI SDK Schema objects (Vercel's jsonSchema wrapper)
if (isVercelSchema(schema)) {
return toStandardSchemaAiSdk(schema as Schema<T>);
}
// At this point, assume it's a plain JSON Schema object
// JSON Schema objects are plain objects with properties like 'type', 'properties', etc.
if (schema === null || (typeof schema !== 'object' && typeof schema !== 'function')) {
throw new Error(`Unsupported schema type: ${typeof schema}`);
}
// If it's a function that's not StandardSchemaWithJSON, it's not supported
if (typeof schema === 'function') {
throw new Error(`Unsupported schema type: function (schema libraries should implement StandardSchemaWithJSON)`);
}
return toStandardSchemaJsonSchema(schema as JSONSchema7);
}
/**
* Type guard to check if a value implements the StandardSchemaV1 interface.
*
* @param value - The value to check
* @returns True if the value implements StandardSchemaV1
*
* @example
* ```typescript
* import { isStandardSchema } from '@mastra/schema-compat';
*
* if (isStandardSchema(someValue)) {
* const result = someValue['~standard'].validate(input);
* }View on GitHub (pinned to 75dd419e61)
Solutions
- Call the factory function to get the schema instance: () => z.object({...}) becomes z.object({...}).
- If using a schema library, upgrade to a version implementing the Standard Schema spec (Zod 3.24+/v4, Valibot, ArkType).
- Wrap the function's returned schema in a StandardSchemaWithJSON adapter before passing it.
- Convert the schema to plain JSON Schema (JSONSchema7 object) and pass that instead.
Example fix
// before
agent({ schema: MyZodObjectSchemaFactory }) // function passed
// after
agent({ schema: MyZodObjectSchemaFactory() }) // call it to get the zod schema instance Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof schema === 'function' && !('~standard' in (schema as any))) {
throw new TypeError('Function schemas must implement StandardSchemaWithJSON; call the factory or pass its result.');
} Type guard
const isStandardSchemaWithJSONFn = (s: unknown): s is { '~standard': unknown; jsonSchema: unknown } =>
typeof s === 'function' && '~standard' in s; Try / catch
let normalized;
try {
normalized = toStandardSchema(schema);
} catch (e) {
if (/Unsupported schema type: function/.test(e.message)) {
normalized = toStandardSchema((schema as () => unknown)()); // call factory
} else throw e;
} Prevention
- Always invoke schema factory functions before passing them.
- Use schema libraries implementing the Standard Schema spec (Zod 3.24+/v4, Valibot, ArkType).
- Prefer schema instances over classes/functions at API boundaries.
When it happens
Trigger: Passing a plain function (e.g. () => z.object(...), a class, or a legacy schema-library constructor) to toStandardSchema via wrapped/result/rewrapped, when the function does not implement StandardSchemaWithJSON.
Common situations: Passing a Zod v2/legacy schema class; passing a function from an older schema library that never implemented the Standard Schema spec; accidentally passing the schema factory instead of calling it (z.object({...}) vs z.object); custom schema wrappers that are callables.
Related errors
- Your schema is async, which is not supported. Please use a s
- Unsupported schema type: ${typeof schema}
- StandardSchemaWithJSON is not supported for applyCompatLayer
- ${label} contains an unsupported field.
- SCHEMA_UNAVAILABLE
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/a8484d7fdb247069.
Report an issue: GitHub.