{"record":{"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/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v4/core/json-schema-processors.ts#L234-L270","documentation":"Thrown by toJSONSchema() when a ZodFunction schema (z.function(args, ret)) is encountered with unrepresentable 'throw' (default). JSON Schema describes data, not callable values, so function schemas have no representation.","triggerScenarios":"Calling toJSONSchema() on a schema that contains a z.function() field — common in RPC/tool schemas. Exporting an entire registry that includes procedure signatures.","commonSituations":"Tool-calling / MCP-style schemas where the top-level model embeds handler signatures, then feeding that model to a JSON Schema-based validator or doc generator.","solutions":["Pass { unrepresentable: 'any' } to toJSONSchema() so function fields become {}.","Separate the function signature from the data model: export only the argument and return schemas via toJSONSchema(), not the wrapping z.function().","For OpenAPI, describe operations explicitly rather than converting z.function() schemas."],"exampleFix":"// before (throws)\nconst Tool = z.object({\n  name: z.string(),\n  handler: z.function(z.tuple([z.string()]), z.void()),\n});\nz.toJSONSchema(Tool);\n\n// after (export only the data contract)\nconst ToolArgs = z.tuple([z.string()]);\nz.toJSONSchema(ToolArgs);","handlingStrategy":"try-catch","validationCode":"const opts = schemaContains(schema, (s) => s._zod.def.type === 'function')\n  ? { unrepresentable: 'any' }\n  : {};\nconst json = z.toJSONSchema(schema, opts);","typeGuard":"function hasFunction(schema) {\n  return schema._zod.def.type === 'function';\n}","tryCatchPattern":"try {\n  return z.toJSONSchema(schema);\n} catch (e) {\n  if (e.message === 'Function types cannot be represented in JSON Schema') {\n    return z.toJSONSchema(schema, { unrepresentable: 'any' });\n  }\n  throw e;\n}","preventionTips":["Do not embed z.function() in data schemas you export; export argument/return schemas separately.","For OpenAPI, describe operations directly rather than converting z.function() schemas.","Pass { unrepresentable: 'any' } when exporting registries that mix data and procedure schemas."],"tags":["json-schema","function","unrepresentable","to-json-schema","rpc"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}