[PR #13823] fix: sanitize tool schemas for strict JSON Schema validators (#13737) #14826

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

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

State: open
Merged: No


Problem

Built-in tool schemas (like question) break on providers with strict JSON Schema validation — Codex, Vertex AI OpenAI-compatible, SGLang, etc.

Two issues:

  1. Zod .meta() pollution: Injects $schema and ref keywords that are invalid in OpenAI function parameter schemas
  2. Missing required fields: When additionalProperties: false is set, strict validators require ALL property keys to be in the required array. Optional fields like multiple are missing.

Error from Codex:

Invalid schema for function 'question': In context=('properties', 'questions',
'items'), 'required' is required to be supplied and to be an array including
every key in properties. Missing 'multiple'.

Fix

Add universal schema sanitization in ProviderTransform.schema() that runs before any provider-specific transforms:

  1. Strip Zod meta fields ($schema, ref) recursively from all schemas
  2. Fix strict mode compliance: When additionalProperties: false is set, ensure all property keys are in required. Optional properties are wrapped with anyOf: [original, {type: 'null'}] so they remain nullable while satisfying strict validators.

Changes

  • packages/opencode/src/provider/transform.ts: Add sanitizeStrict() pass in schema() function

Closes #13737
Related: #13618, #11413, #8184

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/13823 **State:** open **Merged:** No --- ## Problem Built-in tool schemas (like `question`) break on providers with strict JSON Schema validation — Codex, Vertex AI OpenAI-compatible, SGLang, etc. Two issues: 1. **Zod `.meta()` pollution**: Injects `$schema` and `ref` keywords that are invalid in OpenAI function parameter schemas 2. **Missing `required` fields**: When `additionalProperties: false` is set, strict validators require ALL property keys to be in the `required` array. Optional fields like `multiple` are missing. Error from Codex: ``` Invalid schema for function 'question': In context=('properties', 'questions', 'items'), 'required' is required to be supplied and to be an array including every key in properties. Missing 'multiple'. ``` ## Fix Add universal schema sanitization in `ProviderTransform.schema()` that runs before any provider-specific transforms: 1. **Strip Zod meta fields** (`$schema`, `ref`) recursively from all schemas 2. **Fix strict mode compliance**: When `additionalProperties: false` is set, ensure all property keys are in `required`. Optional properties are wrapped with `anyOf: [original, {type: 'null'}]` so they remain nullable while satisfying strict validators. ## Changes - `packages/opencode/src/provider/transform.ts`: Add `sanitizeStrict()` pass in `schema()` function Closes #13737 Related: #13618, #11413, #8184
yindo added the pull-request label 2026-02-16 18:19:34 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14826