Skip to content

Conversation

@toubatbrian
Copy link
Contributor

@toubatbrian toubatbrian commented Oct 29, 2025

Description

This PR adds seamless support for both Zod v3 and v4, allowing users to use either version without any code changes. The key feature is full support for .optional() fields - resolving the TypeScript error that users reported when using .optional() on Zod schema fields.

Changes Made

New Files

  • agents/src/llm/zod-utils.ts: Core utilities for dual-version support
    • isZod4Schema(): Detects Zod version via _zod property check
    • zodSchemaToJsonSchema(): Converts schemas to JSON Schema (handles both v3 and v4)
    • parseZodSchema(): Parses values with version-aware APIs

Updated Files

  • agents/src/llm/tool_context.ts:
    • Removed strict JSONObject constraint from ToolInputSchema<T> to support .optional() fields
    • Replaced instanceof ZodObject checks with isZodSchema() and isZodObjectSchema() helpers
    • Enhanced type inference with InferToolInput<T> helper (adapted from Vercel AI SDK)
  • agents/src/llm/utils.ts: Updated toJsonSchema() to use new zodSchemaToJsonSchema() helper
  • agents/src/voice/generation.ts: Replaced instanceof checks with version-agnostic helpers
  • agents/src/llm/tool_context.test.ts: Added comprehensive test for .optional() fields
  • agents/src/llm/zod-utils.test.ts: Full test suite with snapshot testing for schema conversions

Dependencies

  • agents/package.json: Updated peer dependency to "zod": "^3.25.76 || ^4.1.8"
  • examples/package.json: Updated to use Zod v4 (^4.1.12) by default
  • plugins/test/package.json: Updated peer dependency to match

Pre-Review Checklist

  • Build passes: All builds (lint, typecheck, tests) pass locally
  • AI-generated code reviewed: Code quality ensured, attribution comments added for Vercel AI SDK patterns
  • Changes explained: All changes documented with clear rationale
  • Scope appropriate: All changes relate to Zod v4 support and .optional() fix

@changeset-bot
Copy link

changeset-bot bot commented Oct 29, 2025

🦋 Changeset detected

Latest commit: 29fdd37

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
@livekit/agents-plugins-test Patch
@livekit/agents Patch
@livekit/agents-plugin-anam Patch
@livekit/agents-plugin-cartesia Patch
@livekit/agents-plugin-deepgram Patch
@livekit/agents-plugin-elevenlabs Patch
@livekit/agents-plugin-google Patch
@livekit/agents-plugin-neuphonic Patch
@livekit/agents-plugin-openai Patch
@livekit/agents-plugin-resemble Patch
@livekit/agents-plugin-rime Patch
@livekit/agents-plugin-bey Patch
@livekit/agents-plugin-livekit Patch
@livekit/agents-plugin-silero Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@toubatbrian
Copy link
Contributor Author

Should resolve #733

@toubatbrian toubatbrian changed the title [draft] Brian/zod v4 Brian/zod v4 Oct 29, 2025
@toubatbrian toubatbrian changed the title Brian/zod v4 brianyin/ajs-309-support-zod-v4 Oct 29, 2025
@toubatbrian toubatbrian merged commit 9b50d88 into main Oct 30, 2025
8 checks passed
@toubatbrian toubatbrian deleted the brian/zod-v4 branch October 30, 2025 07:01
@github-actions github-actions bot mentioned this pull request Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants