Skip to content

fix: sanitize tool schemas for strict JSON Schema validators (#13737)#13823

Open
echoVic wants to merge 1 commit intoanomalyco:devfrom
echoVic:fix/13737-tool-schema-strict-validation
Open

fix: sanitize tool schemas for strict JSON Schema validators (#13737)#13823
echoVic wants to merge 1 commit intoanomalyco:devfrom
echoVic:fix/13737-tool-schema-strict-validation

Conversation

@echoVic
Copy link

@echoVic echoVic commented Feb 16, 2026

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

…co#13737)

Built-in tool schemas (like `question`) break on providers with strict
JSON Schema validation (Codex, Vertex AI, SGLang) due to two issues:

1. Zod `.meta()` injects `$schema` and `ref` keywords that are invalid
   in OpenAI function parameter schemas
2. When `additionalProperties: false` is set, strict validators require
   ALL property keys to be in the `required` array

Fix: Add universal schema sanitization in `ProviderTransform.schema()`
that runs before provider-specific transforms:
- Strip Zod meta fields (`$schema`, `ref`)
- For strict mode objects, ensure all properties are in `required` and
  wrap optional ones with `anyOf: [original, {type: 'null'}]`

Closes anomalyco#13737
@github-actions
Copy link
Contributor

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

I found a potential related PR:

PR #13738: fix(provider): sanitize tool schemas for strict validators
#13738

This appears to be addressing the same issue - sanitizing tool schemas for strict JSON Schema validators. Since PR #13823 is the current PR and PR #13738 has a similar title and purpose, these are likely duplicate efforts on the same problem.

You may want to check if one of these PRs should be closed or if they're being developed in parallel with different approaches.

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.

Tool schemas cause 400 errors on strict validators (Codex, Vertex AI)

1 participant

Comments