diff --git a/js/ragas.ts b/js/ragas.ts index 2c89a975..553dda70 100644 --- a/js/ragas.ts +++ b/js/ragas.ts @@ -109,7 +109,7 @@ import OpenAI from "openai"; import { ListContains } from "./list"; import { EmbeddingSimilarity } from "./string"; import { z } from "zod"; -import zodToJsonSchema from "zod-to-json-schema"; +import { zodToJsonSchema } from "./zod-utils"; import { makePartial, ScorerWithPartial } from "./partial"; type RagasArgs = { diff --git a/js/zod-utils.ts b/js/zod-utils.ts new file mode 100644 index 00000000..3c11667c --- /dev/null +++ b/js/zod-utils.ts @@ -0,0 +1,22 @@ +import { zodToJsonSchema as zodToJsonSchemaV3 } from "zod-to-json-schema"; +import * as z3 from "zod/v3"; +import * as z4 from "zod/v4"; + +function isZodV4(zodObject: z3.ZodType | z4.ZodType): zodObject is z4.ZodType { + return ( + typeof zodObject === "object" && + zodObject !== null && + "_zod" in zodObject && + zodObject._zod !== undefined + ); +} + +export function zodToJsonSchema(schema: z4.ZodType | z3.ZodType) { + if (isZodV4(schema)) { + return z4.toJSONSchema(schema, { + target: "draft-7", + }); + } + + return zodToJsonSchemaV3(schema); +} diff --git a/package.json b/package.json index b7b42e0c..5de55294 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,8 @@ "typedoc": "^0.25.13", "typedoc-plugin-markdown": "^3.17.1", "typescript": "^5.9.2", - "vitest": "^2.1.9" + "vitest": "^2.1.9", + "zod": "^3.25.34" }, "dependencies": { "ajv": "^8.17.1", @@ -65,8 +66,10 @@ "linear-sum-assignment": "^1.0.7", "mustache": "^4.2.0", "openai": "^6.3.0", - "zod": "^3.25.76", "zod-to-json-schema": "^3.24.6" }, + "peerDependencies": { + "zod": "^3.25.34 || ^4.0" + }, "packageManager": "pnpm@10.33.0" }