Skip to content

schemaToJSONSchema

schemaToJSONSchema(schema, options?): Record<string, unknown>

Defined in: gadgets/schema-to-json.ts:47

Convert a Zod schema to JSON Schema with description fallback.

If descriptions exist in schema._def but are missing from the generated JSON Schema (indicating a Zod instance mismatch), this function:

  1. Logs a warning recommending import { z } from "llmist"
  2. Extracts descriptions from _def and merges them into the JSON Schema

ZodType

Zod schema to convert

Conversion options (target JSON Schema version)

"draft-7" | "draft-2020-12"

Record<string, unknown>

JSON Schema object with descriptions preserved

import { schemaToJSONSchema } from './schema-to-json.js';
import { z } from 'zod';
const schema = z.object({
name: z.string().describe('User name'),
});
const jsonSchema = schemaToJSONSchema(schema);
// { type: 'object', properties: { name: { type: 'string', description: 'User name' } } }