From d91e8346dfc5dc1f8df752b5b53773dbf4767957 Mon Sep 17 00:00:00 2001 From: Volodymyr Vreshch Date: Thu, 5 Mar 2026 00:32:53 +0100 Subject: [PATCH] chore: complete Zod v4 migration in SDK - Use native toJSONSchema() from zod instead of removed .toJSONSchema() method - Detect Zod schemas via _zod property (v4) with _def fallback - Check optional type via _zod.def.type instead of _def.typeName - Use caret range for zod dependency --- package-lock.json | 2 +- packages/sdk/package.json | 2 +- packages/sdk/src/agent.ts | 14 ++++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index cb7d356..fb6b7e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7251,7 +7251,7 @@ "dependencies": { "@agentage/core": "^0.1.0", "openai": "^6.15.0", - "zod": "4.3.6" + "zod": "^4.3.6" }, "devDependencies": { "@types/jest": "^30.0.0", diff --git a/packages/sdk/package.json b/packages/sdk/package.json index c4cc309..52dee7f 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -41,7 +41,7 @@ "dependencies": { "@agentage/core": "^0.1.0", "openai": "^6.15.0", - "zod": "4.3.6" + "zod": "^4.3.6" }, "devDependencies": { "@types/jest": "^30.0.0", diff --git a/packages/sdk/src/agent.ts b/packages/sdk/src/agent.ts index e5d5e98..585233a 100644 --- a/packages/sdk/src/agent.ts +++ b/packages/sdk/src/agent.ts @@ -6,6 +6,7 @@ import type { Tool, } from '@agentage/core'; import OpenAI from 'openai'; +import { toJSONSchema } from 'zod'; import type { ChatCompletionMessageParam } from 'openai/resources/chat/completions'; import { MissingApiKeyError, @@ -42,19 +43,20 @@ function convertSchemaToJsonSchema(schema: any): any { const required: string[] = []; for (const [key, value] of Object.entries(schema)) { - // Check if it's a Zod schema (has toJSONSchema method from Zod 4.x) - if (value && typeof value === 'object' && 'toJSONSchema' in value) { + // Check if it's a Zod schema (has _zod property in Zod 4.x) + if (value && typeof value === 'object' && ('_zod' in value || '_def' in value)) { // eslint-disable-next-line @typescript-eslint/no-explicit-any - const jsonSchema = (value as any).toJSONSchema(); + const jsonSchema = toJSONSchema(value as any); // Extract the actual schema (remove top-level $schema if present) // eslint-disable-next-line @typescript-eslint/no-unused-vars const { $schema, ...cleanSchema } = jsonSchema as Record; properties[key] = cleanSchema; - // Check if the field is optional - const def = (value as { _def?: { typeName?: string } })._def; - if (def?.typeName !== 'ZodOptional') { + // Check if the field is optional (Zod 4.x uses _zod.def.type) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const zodDef = (value as any)._zod?.def; + if (zodDef?.type !== 'optional') { required.push(key); } } else {