Skip to content

fix(provider): limit JSON schema depth for Moonshot/Kimi - #25492

Closed
nateGeorge wants to merge 1 commit into
anomalyco:devfrom
nateGeorge:fix/moonshot-schema-depth
Closed

nateGeorge wants to merge 1 commit into
anomalyco:devfrom
nateGeorge:fix/moonshot-schema-depth

Conversation

@nateGeorge

@nateGeorge nateGeorge commented May 2, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #25495

Type of change

  • Bug fix

What does this PR do?

Moonshot/Kimi K2.6 enforces a JSON schema depth limit of 10 levels. Tools with deeply nested schemas (e.g., Klaviyo campaign message definitions with 11-12 levels of nested anyOf / properties) cause all tool calls to fail with schema depth exceeds maximum limit of 10.

This PR adds depth-aware schema flattening to sanitizeMoonshot() in transform.ts:

  • MAX_DEPTH = 9 (1 level headroom under Kimi's 10 limit)
  • isComplex() distinguishes structural nodes (objects with properties, anyOf, oneOf, allOf, items, $defs) from primitives
  • Complex nodes at depth >= 9 are flattened to { type: "object", additionalProperties: true } or { type: "array", items: { type: "object", additionalProperties: true } }
  • Primitives (string, number, boolean) pass through untouched at any depth

This preserves type safety for simple parameters while preventing the depth error on complex nested schemas. All 2,347 tests pass, including 2 new tests specifically verifying deep nesting flattening and Klaviyo-like anyOf schema handling.

How did you verify your code works?

  • bun test in packages/opencode: 2,347 pass, 0 fail, 11 skip, 1 todo
  • 2 new tests added in transform.test.ts:
    1. Deeply nested schema (>12 levels) is flattened at depth >= 9
    2. Klaviyo-like anyOf badgeConfig schema is preserved within depth limit

Checklist

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

…exceeds maximum limit of 10' errors

Moonshot/Kimi K2.6 rejects tool schemas with nesting depth > 10. Klaviyo and
other APIs define deeply nested anyOf structures (11-12 levels) that break
all tool calls on Kimi models.

Changes:
- add MAX_DEPTH = 9 (1 level headroom) to sanitizeMoonshot()
- add isComplex() to distinguish structural nodes from primitives
- flatten complex nodes at depth >= 9 to {type: object, additionalProperties: true}
- preserve primitives (string, number, boolean) untouched at any depth

Fixes schema depth errors without losing type safety for simple parameters.

Closes anomalyco#1010
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels May 2, 2026
@github-actions

github-actions Bot commented May 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

github-actions Bot commented May 2, 2026

Copy link
Copy Markdown
Contributor

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

Potential Related PR Found

PR #23667: "fix: expose actual Kimi/Moonshot API errors and resolve $ref schema"

This PR is related as it also addresses Moonshot/Kimi schema handling. While it focuses on exposing API errors and resolving $ref schemas, it may be relevant context for the current PR's JSON schema depth limiting fix.

The search results mostly return PR #25492 itself, indicating no other currently open PRs with identical focus on limiting JSON schema depth for Moonshot/Kimi.

@github-actions github-actions Bot removed needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels May 2, 2026
@github-actions

github-actions Bot commented May 2, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Moonshot/Kimi K2.6 rejects tool schemas with nesting depth > 10, breaking all tool calls

1 participant