Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Dec 24, 2025

Related GitHub Issue

Closes: #10320

Description

This PR fixes the JSON Schema compatibility issue reported in #10320 where Claude models accessed through OpenAI-compatible proxies were failing with "JSON schema is invalid. It must match JSON Schema draft 2020-12" errors.

Root Cause:

  • Several native tool definitions use type arrays like type: ["string", "null"] for nullable parameters
  • This syntax is valid in JSON Schema draft-07 but NOT in draft 2020-12 (which requires anyOf format instead)
  • The convertToolSchemaForOpenAI function in base-provider.ts was converting nullable types by simply removing null from the type array, rather than converting to the proper anyOf format

Implementation:

  • Modified convertToolSchemaForOpenAI in src/api/providers/base-provider.ts to use the existing normalizeToolSchema function from src/utils/json-schema.ts
  • The normalizeToolSchema function already properly handles:
    • Converting type: ["T", "null"] array syntax to anyOf: [{type: "T"}, {type: "null"}] format
    • Moving array-specific properties (items, minItems, etc.) inside the array variant when using anyOf
    • Setting additionalProperties: false for object types (OpenAI strict mode)
    • Stripping unsupported format values
  • Updated Cerebras provider's stripUnsupportedSchemaFields to handle anyOf/oneOf/allOf arrays

Test Procedure

  1. Added comprehensive tests in src/api/providers/__tests__/base-provider.spec.ts covering:

    • JSON Schema draft 2020-12 compliance (type array to anyOf conversion)
    • OpenAI strict mode compatibility (additionalProperties: false)
    • Format field handling
    • Edge cases (null input, deeply nested structures)
  2. All existing tests continue to pass:

    • json-schema.spec.ts - 35 tests pass
    • cerebras.spec.ts - all tests pass
    • base-openai-compatible-provider.spec.ts - all tests pass

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

This fix reuses the existing, well-tested normalizeToolSchema function rather than implementing new conversion logic, ensuring consistency across all providers that use OpenAI-compatible format.

…e providers

- Update convertToolSchemaForOpenAI in base-provider.ts to use normalizeToolSchema
  which properly converts type arrays (e.g., ["string", "null"]) to anyOf format
  required by JSON Schema draft 2020-12
- Update Cerebras stripUnsupportedSchemaFields to handle anyOf/oneOf/allOf arrays
- Add comprehensive tests for base-provider convertToolSchemaForOpenAI function

Fixes #10320
@roomote
Copy link
Contributor Author

roomote bot commented Dec 24, 2025

Rooviewer Clock   See task on Roo Cloud

Reviewed. No issues found.

The implementation correctly addresses the JSON Schema draft 2020-12 compliance issue by reusing the existing normalizeToolSchema function, which properly converts type arrays like ["string", "null"] to anyOf format. The Cerebras provider update to handle anyOf/oneOf/allOf arrays is also necessary and correct.

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Dec 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

工具调用(tool / function calling)JSON Schema 不合法

3 participants