A
arktype2mo ago
Isaac

Usage with JSON Schema

Hi all, I'm in the process of migrating my project from Zod to ArkType, but I'm running into issues while converting my AI SDK tool input schemas. The AI SDK requires non-Zod schemas to first be converted to JSON schema and be parsed with their jsonSchema helper, but when I do jsonSchema(arktypeSchema.toJsonSchema()) , I get the error:
Argument of type 'NonBooleanBranch' is not assignable to parameter of type 'JSONSchema7'.
Type 'Constrainable' is not assignable to type 'JSONSchema7'.
Types of property '$defs' are incompatible.
Type 'Record<string, NonBooleanBranch> | undefined' is not assignable to type '{ [key: string]: JSONSchema7Definition; } | undefined'.
Type 'Record<string, NonBooleanBranch>' is not assignable to type '{ [key: string]: JSONSchema7Definition; }'.
'string' index signatures are incompatible.
Type 'NonBooleanBranch' is not assignable to type 'JSONSchema7Definition'.
Type 'Constrainable' is not assignable to type 'JSONSchema7Definition'.ts(2345)
Argument of type 'NonBooleanBranch' is not assignable to parameter of type 'JSONSchema7'.
Type 'Constrainable' is not assignable to type 'JSONSchema7'.
Types of property '$defs' are incompatible.
Type 'Record<string, NonBooleanBranch> | undefined' is not assignable to type '{ [key: string]: JSONSchema7Definition; } | undefined'.
Type 'Record<string, NonBooleanBranch>' is not assignable to type '{ [key: string]: JSONSchema7Definition; }'.
'string' index signatures are incompatible.
Type 'NonBooleanBranch' is not assignable to type 'JSONSchema7Definition'.
Type 'Constrainable' is not assignable to type 'JSONSchema7Definition'.ts(2345)
This happens even when arktypeSchema is type({}). Any insights?
2 Replies
Isaac
IsaacOP2mo ago
Another piece of context: While jsonSchema(type({}).toJsonSchema()) gives the error, if I paste the runtime output of type({}).toJsonSchema() into the adapter as jsonSchema({ $schema: "https://json-schema.org/draft/2020-12/schema", type: "object" });, then it works fine.
ssalbdivad
ssalbdivad2mo ago
Welcome! This is just a quirk of interop between two libraries with different definition of the shape of a JsonSchema object, which is not trivial to represnt in TypeScript. Unless something is failing at runtime, your best bet would be to simply cast the result of ArkType's toJsonSchema method so that it is compatible with the library you're trying to use it with. If you're using this in many places, instead you may want to create a wrapper function like:
const aiJsonSchema = (t: type.Any): JSONSchema7 => t.toJsonSchema() as never
const aiJsonSchema = (t: type.Any): JSONSchema7 => t.toJsonSchema() as never

Did you find this page helpful?