Skip to content

fix(provider): sanitize xAI tool schemas for OpenRouter and native xai provider - #23706

Closed
dotCipher wants to merge 1 commit into
anomalyco:devfrom
dotCipher:fix/openrouter-xai-tool-schema-sanitization
Closed

dotCipher wants to merge 1 commit into
anomalyco:devfrom
dotCipher:fix/openrouter-xai-tool-schema-sanitization

Conversation

@dotCipher

@dotCipher dotCipher commented Apr 21, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #23704

Type of change

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

What does this PR do?

Tool calling fails for xAI models (e.g. grok-4.1-fast) when used through OpenRouter or the native xai provider. The error is:

[xAI] Invalid request content: Schema validation failed: [standard_violation] /properties/properties/additionalProperties: property schema 'false' is not supported

xAI structured output docs state:

  • additionalProperties defaults to false already
  • schemas with boolean property schemas like false are rejected

So the fix strips additionalProperties: false from tool/input JSON schemas before sending to xAI. This preserves the intended strictness (since false is the default) while avoiding the validator rejection.

The change is scoped to:

  • providerID === "xai" (native xAI provider)
  • providerID === "openrouter" with model.api.id.startsWith("x-ai/") (xAI models via OpenRouter)

Non-xAI models are unaffected.

How did you verify your code works?

Added unit tests in packages/opencode/test/provider/transform.test.ts:

  • Verifies recursive removal of additionalProperties: false for both OpenRouter xAI and native xai provider
  • Verifies object-valued and true additionalProperties are preserved
  • Verifies non-xAI OpenRouter models are not affected

Screenshots / recordings

N/A — no UI changes.

Checklist

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

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

POTENTIAL DUPLICATE FOUND:

PR #23707 - "fix(provider): sanitize xAI tool schemas for OpenRouter"

Note: PR #23706 is the current PR you're analyzing, so #23707 is a potential duplicate that should be reviewed for consolidation or coordination.

…i provider

Strip additionalProperties: false from tool/input JSON schemas
when targeting xAI models via OpenRouter or the native xai provider.
xAI rejects this keyword while their validator already defaults to false.

Fixes anomalyco#23704
@dotCipher
dotCipher force-pushed the fix/openrouter-xai-tool-schema-sanitization branch from 5bd4988 to c8692ce Compare April 21, 2026 15:45
@dotCipher dotCipher changed the title fix(provider): sanitize xAI tool schemas for OpenRouter fix(provider): sanitize xAI tool schemas for OpenRouter and native xai provider Apr 21, 2026
@dotCipher dotCipher changed the title fix(provider): sanitize xAI tool schemas for OpenRouter fix(provider): sanitize xAI tool schemas for OpenRouter and native xai provider Apr 21, 2026
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@dotCipher

Copy link
Copy Markdown
Author

Closing this PR after extensive investigation. The issue is not fixable at the OpenCode level.

Investigation Summary

We spent significant time debugging why xAI/Grok rejects tool schemas with additionalProperties: false when used via OpenRouter. Here's what we found:

Root Cause

The Vercel AI SDK intentionally adds additionalProperties: false to all object schemas during tool serialization (via addAdditionalPropertiesToJsonSchema in @ai-sdk/provider-utils). This is a deliberate design choice for strict JSON validation.

xAI's validator explicitly rejects additionalProperties: false in tool schemas, creating an incompatibility:

  • AI SDK: Adds it for strict validation (intentional)
  • xAI: Rejects it (their validator rule)

Why OpenCode Can't Fix This

  1. The AI SDK re-adds additionalProperties: false AFTER any sanitization we do in OpenCode
  2. The binary bundles all dependencies at build time, so runtime interceptors don't work
  3. The conflict exists at the AI SDK level, not OpenCode's code

Where This Needs To Be Fixed

  1. @openrouter/ai-sdk-provider — Most likely candidate; they translate between OpenAI format and xAI
  2. Vercel AI SDK — Could add provider-specific schema serialization options
  3. xAI — Could change their validator to accept additionalProperties: false

Workarounds

  • Use Grok without tools (tool_call: false in config)
  • Use a different provider (Claude, GPT-4, etc.)
  • File an issue with OpenRouter to add xAI-specific schema handling

1 similar comment
@dotCipher

Copy link
Copy Markdown
Author

Closing this PR after extensive investigation. The issue is not fixable at the OpenCode level.

Investigation Summary

We spent significant time debugging why xAI/Grok rejects tool schemas with additionalProperties: false when used via OpenRouter. Here's what we found:

Root Cause

The Vercel AI SDK intentionally adds additionalProperties: false to all object schemas during tool serialization (via addAdditionalPropertiesToJsonSchema in @ai-sdk/provider-utils). This is a deliberate design choice for strict JSON validation.

xAI's validator explicitly rejects additionalProperties: false in tool schemas, creating an incompatibility:

  • AI SDK: Adds it for strict validation (intentional)
  • xAI: Rejects it (their validator rule)

Why OpenCode Can't Fix This

  1. The AI SDK re-adds additionalProperties: false AFTER any sanitization we do in OpenCode
  2. The binary bundles all dependencies at build time, so runtime interceptors don't work
  3. The conflict exists at the AI SDK level, not OpenCode's code

Where This Needs To Be Fixed

  1. @openrouter/ai-sdk-provider — Most likely candidate; they translate between OpenAI format and xAI
  2. Vercel AI SDK — Could add provider-specific schema serialization options
  3. xAI — Could change their validator to accept additionalProperties: false

Workarounds

  • Use Grok without tools (tool_call: false in config)
  • Use a different provider (Claude, GPT-4, etc.)
  • File an issue with OpenRouter to add xAI-specific schema handling

@dotCipher dotCipher closed this Apr 21, 2026
@dotCipher

Copy link
Copy Markdown
Author

Investigation Summary

After extensive debugging, this issue is not fixable at the OpenCode level. Here's what we found:

Root Cause

The Vercel AI SDK intentionally adds additionalProperties: false to all object schemas during tool serialization via addAdditionalPropertiesToJsonSchema in @ai-sdk/provider-utils. This is a deliberate design choice for strict JSON validation.

xAI's validator explicitly rejects additionalProperties: false in tool schemas, creating an incompatibility:

  • AI SDK: Adds it for strict validation (intentional)
  • xAI: Rejects it (their validator rule)

Why OpenCode Can't Fix This

  1. The AI SDK re-adds additionalProperties: false AFTER any sanitization we do in OpenCode
  2. The binary bundles all dependencies at build time, so runtime interceptors don't work
  3. The conflict exists at the AI SDK level, not OpenCode's code

Where This Needs To Be Fixed

  1. @openrouter/ai-sdk-provider — Most likely candidate; they translate between OpenAI format and xAI
  2. Vercel AI SDK — Could add provider-specific schema serialization options
  3. xAI — Could change their validator to accept additionalProperties: false

Workarounds

  • Use Grok without tools (tool_call: false in config)
  • Use a different provider (Claude, GPT-4, etc.)
  • File an issue with OpenRouter to add xAI-specific schema handling in their provider package

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.

OpenRouter xAI/Grok tool schemas fail when additionalProperties: false is emitted

1 participant