{"id":"40d2cba7e29d7e68","repo":"colinhacks/zod","slug":"function-types-cannot-be-represented-in-json-schem","errorCode":null,"errorMessage":"Function types cannot be represented in JSON Schema","messagePattern":"Function types cannot be represented in JSON Schema","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v4/core/json-schema-processors.ts","lineNumber":252,"sourceCode":"    }\n  } else {\n    Object.assign(_json, file);\n  }\n};\n\nexport const successProcessor: Processor<schemas.$ZodSuccess> = (_schema, _ctx, json, _params) => {\n  (json as JSONSchema.BooleanSchema).type = \"boolean\";\n};\n\nexport const customProcessor: Processor<schemas.$ZodCustom> = (_schema, ctx, _json, _params) => {\n  if (ctx.unrepresentable === \"throw\") {\n    throw new Error(\"Custom types cannot be represented in JSON Schema\");\n  }\n};\n\nexport const functionProcessor: Processor<schemas.$ZodFunction> = (_schema, ctx, _json, _params) => {\n  if (ctx.unrepresentable === \"throw\") {\n    throw new Error(\"Function types cannot be represented in JSON Schema\");\n  }\n};\n\nexport const transformProcessor: Processor<schemas.$ZodTransform> = (_schema, ctx, _json, _params) => {\n  if (ctx.unrepresentable === \"throw\") {\n    throw new Error(\"Transforms cannot be represented in JSON Schema\");\n  }\n};\n\nexport const mapProcessor: Processor<schemas.$ZodMap> = (_schema, ctx, _json, _params) => {\n  if (ctx.unrepresentable === \"throw\") {\n    throw new Error(\"Map cannot be represented in JSON Schema\");\n  }\n};\n\nexport const setProcessor: Processor<schemas.$ZodSet> = (_schema, ctx, _json, _params) => {\n  if (ctx.unrepresentable === \"throw\") {\n    throw new Error(\"Set cannot be represented in JSON Schema\");","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v4/core/json-schema-processors.ts#L234-L270","documentation":"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.","triggerScenarios":"`z.toJSONSchema()` over a schema tree that includes `z.function(args, ret)`, with default options.","commonSituations":"Documenting a schema that carries callbacks or RPC handles; converting a meta-schema that wraps function types.","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."],"exampleFix":"// before\nz.toJSONSchema(z.object({ onClick: z.function() })); // throws\n// after\nz.toJSONSchema(z.object({ onClick: z.function() }).omit({ onClick: true }), { unrepresentable: \"any\" });","handlingStrategy":"fallback","validationCode":"// Omit function fields before conversion.\nconst contractSchema = schema.omit({ onClick: true });\nconst json = z.toJSONSchema(contractSchema, { unrepresentable: \"any\" });","typeGuard":"function usesFunction(schema: z.ZodType): boolean {\n  return schema._zod.traits.has(\"$ZodFunction\");\n}","tryCatchPattern":"try {\n  return z.toJSONSchema(schema);\n} catch (e) {\n  if (e instanceof Error && /cannot be represented in JSON Schema/.test(e.message)) {\n    return z.toJSONSchema(schema, { unrepresentable: \"any\" });\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["json-schema","function","unrepresentable","openapi"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}