Skip to content

fix(opencode): replace zodToJsonSchema with z.toJSONSchema for Zod v4 compat#20809

Open
jbmml wants to merge 1 commit intoanomalyco:devfrom
jbmml:fix/experimental-zod-v4-schema
Open

fix(opencode): replace zodToJsonSchema with z.toJSONSchema for Zod v4 compat#20809
jbmml wants to merge 1 commit intoanomalyco:devfrom
jbmml:fix/experimental-zod-v4-schema

Conversation

@jbmml
Copy link
Copy Markdown

@jbmml jbmml commented Apr 3, 2026

Issue for this PR

Fixes #20807
Related: #4357

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The experimental tools endpoint (GET /experimental/tools) uses zodToJsonSchema() from zod-to-json-schema@3.24.5 to convert tool parameter schemas. This library is incompatible with Zod v4 — for an object schema with two described string properties, it returns {"type":"string"} instead of the correct object schema. All properties, descriptions, and required fields are lost.

The fix switches to z.toJSONSchema() (Zod v4 built-in), which is already used in session/prompt.ts for the same purpose and correctly preserves .describe() metadata. The Zod-vs-plain detection also changes from _def (Zod v3 internals) to typeof parse === "function" which works reliably with Zod v4.

I confirmed the original bug reported in #4357 (tool descriptions lost) is already fixed in the prompt path by the Zod v4 migration. This PR fixes the same issue in the experimental API path.

How did you verify your code works?

  • Tested both converters directly: z.toJSONSchema() produces correct output with descriptions, zodToJsonSchema() returns garbage {"type":"string"}
  • 1808/1808 passing tests unchanged (6 pre-existing skill discovery failures on unmodified code)
  • Full turbo typecheck passes across all 13 packages

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

… compat

zodToJsonSchema from zod-to-json-schema@3.x produces incorrect output
with Zod v4 schemas (returns {"type":"string"} for object schemas,
losing all properties and descriptions). Switch the experimental tools
API endpoint to z.toJSONSchema() which is Zod v4's built-in converter
and correctly preserves .describe() metadata.

Fixes anomalyco#20807

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

The following comment was made by an LLM, it may be inaccurate:

Based on the search results, I found related PRs but they appear to be older attempts addressing similar issues with tool descriptions. However, PR #20809 is the current PR being analyzed, so I need to verify if any of the found PRs are still open and potentially duplicate:

The most relevant related PRs are:

These appear to be previous attempts at fixing similar issues. The current PR #20809 is a more targeted fix for the experimental tools endpoint using the Zod v4 built-in z.toJSONSchema() method instead of the external zodToJsonSchema library.

No duplicate PRs found

@jbmml
Copy link
Copy Markdown
Author

jbmml commented Apr 3, 2026

Re: the bot-flagged related PRs — I investigated all four:

This PR (#20809) fixes a different code path — the experimental HTTP API endpoint, which was the only remaining call site for the broken zodToJsonSchema. These are complementary, not overlapping.

jbmml pushed a commit to jbmml/opencode that referenced this pull request Apr 8, 2026
Applies both open PRs (anomalyco#20805, anomalyco#20809) to current dev for local use:
- task tool: accept agent/agent_type as aliases for subagent_type
- experimental.ts: replace broken zodToJsonSchema with z.toJSONSchema

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jbmml
Copy link
Copy Markdown
Author

jbmml commented Apr 9, 2026

Quick note for reviewers: this PR is independent of #19916 (plugin cross-instance Zod metadata rehydration).

This fixes a narrower issue — experimental.ts is the only remaining call site using zodToJsonSchema from zod-to-json-schema@3.24.5, which is incompatible with Zod v4 (returns {"type":"string"} for object schemas). The fix switches to z.toJSONSchema() which is native to Zod v4 and produces correct output.

I previously linked this to a broader re-architecture RFC (#20922) which I've since closed — the unification premise didn't hold up under analysis. This fix stands on its own.

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.

experimental.ts uses zodToJsonSchema which is broken with Zod v4

1 participant