Skip to content

Zod Schema

zod is a TypeScript-first schema validation with static type inference.

import { z } from "zod"
// city array schema
const CitySchema = z.array(
z.object({
name: z.string(),
population: z.number(),
url: z.string(),
})
)

zod-to-json-schema is a library that converts zod schemas to JSON schema.

import { zodToJsonSchema } from "zod-to-json-schema"
// convert to JSON schema
const CitySchemaJSON = zodToJsonSchema(CitySchema, "citySchema").definitions[
"citySchema"
] as JSONSchemaArray

The JSON schemas can be used in defSchema to constrain the output of the tool.

// JSON schema to constrain the output of the tool.
const schema = defSchema("CITY_SCHEMA", CitySchemaJSON)
...