colinhacks/zod · error · Error
Function types cannot be represented in JSON Schema
Error message
Function types cannot be represented in JSON Schema
What it means
Functions are not serializable, so `functionProcessor` (json-schema-processors.ts:250) refuses `z.function()` when `ctx.unrepresentable === "throw"` (default). JSON Schema has no concept of a callable value.
Source
Thrown at packages/zod/src/v4/core/json-schema-processors.ts:252
}
} else {
Object.assign(_json, file);
}
};
export const successProcessor: Processor<schemas.$ZodSuccess> = (_schema, _ctx, json, _params) => {
(json as JSONSchema.BooleanSchema).type = "boolean";
};
export const customProcessor: Processor<schemas.$ZodCustom> = (_schema, ctx, _json, _params) => {
if (ctx.unrepresentable === "throw") {
throw new Error("Custom types cannot be represented in JSON Schema");
}
};
export const functionProcessor: Processor<schemas.$ZodFunction> = (_schema, ctx, _json, _params) => {
if (ctx.unrepresentable === "throw") {
throw new Error("Function types cannot be represented in JSON Schema");
}
};
export const transformProcessor: Processor<schemas.$ZodTransform> = (_schema, ctx, _json, _params) => {
if (ctx.unrepresentable === "throw") {
throw new Error("Transforms cannot be represented in JSON Schema");
}
};
export const mapProcessor: Processor<schemas.$ZodMap> = (_schema, ctx, _json, _params) => {
if (ctx.unrepresentable === "throw") {
throw new Error("Map cannot be represented in JSON Schema");
}
};
export const setProcessor: Processor<schemas.$ZodSet> = (_schema, ctx, _json, _params) => {
if (ctx.unrepresentable === "throw") {
throw new Error("Set cannot be represented in JSON Schema");View on GitHub (pinned to 912f0f51b0)
Solutions
- Pass `{ unrepresentable: "any" }` to skip the function node.
- Remove the function field from the converted shape; model RPC/callable surfaces with separate request/response schemas instead.
- Use `.omit({ handler: true })` on the object before conversion.
Example fix
// before
z.toJSONSchema(z.object({ onClick: z.function() })); // throws
// after
z.toJSONSchema(z.object({ onClick: z.function() }).omit({ onClick: true }), { unrepresentable: "any" }); Defensive patterns
Strategy: fallback
Validate before calling
// Omit function fields before conversion.
const contractSchema = schema.omit({ onClick: true });
const json = z.toJSONSchema(contractSchema, { unrepresentable: "any" }); Type guard
function usesFunction(schema: z.ZodType): boolean {
return schema._zod.traits.has("$ZodFunction");
} Try / catch
try {
return z.toJSONSchema(schema);
} catch (e) {
if (e instanceof Error && /cannot be represented in JSON Schema/.test(e.message)) {
return z.toJSONSchema(schema, { unrepresentable: "any" });
}
throw e;
} Prevention
- Do not include `z.function()` fields in data schemas that become JSON contracts.
- Model RPC surfaces with separate request/response schemas, not function types.
- Use `.omit()` to drop callable fields before conversion.
When it happens
Trigger: `z.toJSONSchema()` over a schema tree that includes `z.function(args, ret)`, with default options.
Common situations: Documenting a schema that carries callbacks or RPC handles; converting a meta-schema that wraps function types.
Related errors
- BigInt cannot be represented in JSON Schema
- Symbols cannot be represented in JSON Schema
- Undefined cannot be represented in JSON Schema
- Void cannot be represented in JSON Schema
- Date cannot be represented in JSON Schema
AI-assisted analysis of colinhacks/zod@912f0f51b0 (2026-08-03).
Data as JSON: /data/errors/40d2cba7e29d7e68.json.
Report an issue: GitHub.