[PR #13619] fix: sanitize tool JSON schemas for Claude API compatibility #14740

Open
opened 2026-02-16 18:19:30 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/13619

State: open
Merged: No


Summary

Fixes #13618

Claude API (especially thinking models like claude-opus-4-6-thinking) rejects tool schemas that have properties without a required field, returning:

***.custom.input_schema: JSON schema is invalid. It must match JSON Schema draft 2020-12

This affects users who use plugins like oh-my-opencode that generate tool schemas without explicit required arrays (e.g., the session_list tool).

Changes

Added two sanitization steps at the top of ProviderTransform.schema() in packages/opencode/src/provider/transform.ts:

  1. Strip $schema meta field — Zod 4's toJSONSchema() adds "$schema": "https://json-schema.org/draft/2020-12/schema" to every output. The codebase already handles this for createStructuredOutputTool but not in the general schema transform.

  2. Add required: [] when missing — When a schema has properties but no required field, automatically add an empty required array. This is the primary fix.

Verification

Tested by intercepting OpenCode's API requests with a proxy, isolating all 33 tools, and testing each individually against Claude API:

Test Result
session_list original schema (no required) 400 error
session_list with "required": [] added 200 OK
All 33 tools together after fix 200 OK

Tested with both claude-opus-4-6 and claude-opus-4-6-thinking models through new-api (OpenAI-compatible proxy).

Related

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/13619 **State:** open **Merged:** No --- ## Summary Fixes #13618 Claude API (especially thinking models like `claude-opus-4-6-thinking`) rejects tool schemas that have `properties` without a `required` field, returning: ``` ***.custom.input_schema: JSON schema is invalid. It must match JSON Schema draft 2020-12 ``` This affects users who use plugins like oh-my-opencode that generate tool schemas without explicit `required` arrays (e.g., the `session_list` tool). ## Changes Added two sanitization steps at the top of `ProviderTransform.schema()` in `packages/opencode/src/provider/transform.ts`: 1. **Strip `$schema` meta field** — Zod 4's `toJSONSchema()` adds `"$schema": "https://json-schema.org/draft/2020-12/schema"` to every output. The codebase already handles this for `createStructuredOutputTool` but not in the general schema transform. 2. **Add `required: []` when missing** — When a schema has `properties` but no `required` field, automatically add an empty `required` array. This is the primary fix. ## Verification Tested by intercepting OpenCode's API requests with a proxy, isolating all 33 tools, and testing each individually against Claude API: | Test | Result | |------|--------| | `session_list` original schema (no `required`) | ❌ 400 error | | `session_list` with `"required": []` added | ✅ 200 OK | | All 33 tools together after fix | ✅ 200 OK | Tested with both `claude-opus-4-6` and `claude-opus-4-6-thinking` models through new-api (OpenAI-compatible proxy). ## Related - Similar issue in Claude Code: https://github.com/anthropics/claude-code/issues/586
yindo added the pull-request label 2026-02-16 18:19:30 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14740