Skip to content

kimi-k3 (Moonshot) always fails with Upstream request failed - tool schemas containing $ref are rejected #39995

Closed as not planned

Description

@fireNing

Describe the bug

When using kimi-k3 (Moonshot) via OpenCode Go, every conversation with tools enabled fails immediately with:

Error from provider (Console Go): Upstream request failed

The exact same configuration works fine with glm-5.2, grok-4.5, deepseek-*. This is not a config/auth/region issue — it is model-specific to Moonshot.

Root cause (confirmed by direct API testing against the provider gateway)

Moonshot's upstream API rejects tool definitions containing:

  1. $ref — even a bare {"$ref": "#/components/schemas/Foo"} with no sibling keys is rejected
  2. tuple-style items arrays — "items": ["string", "number"] is rejected

opencode's sanitizeMoonshot sanitizer (packages/opencode/src/session/transform.ts, ~line 1524-1540) removes sibling keys of $ref and converts tuple items to items[0], but keeps the $ref itself. After sanitization the tool schema still contains $ref, which Moonshot still rejects — so the sanitizer never actually fixes the request.

Other providers (Zhipu/GLM, xAI/Grok) accept the same tool schemas, which is why only Moonshot models break.

Reproduction

  1. Configure a Moonshot-backed model (e.g. kimi-k3) through any OpenAI-compatible gateway
  2. Start any conversation — the main agent always includes tool definitions
  3. Every request fails with Upstream request failed

Direct API test (same key + baseURL, only the tool schema differs):

request body kimi-k3 (Moonshot) glm-5.2 (Zhipu)
simple tool, no $ref ✅ 200 stream ✅ 200 stream
tool with anyOf ✅ 200 stream ✅ 200 stream
tool with tuple items: ["string","number"] ❌ upstream request failed ✅ 200 stream
tool with bare $ref (no siblings) ❌ upstream request failed ✅ 200 stream

Expected behavior

kimi-k3 (and other Moonshot models) should work in tool-enabled conversations, like glm-5.2 / grok-4.5 do.

Suggested fix

sanitizeMoonshot must strip $ref entirely, not just its sibling keys — Moonshot rejects even a bare $ref. Either:

  • resolve $ref to the actual schema before sending, or
  • drop properties that are pure $ref references

Environment

  • OS: Windows
  • Provider: OpenCode Go (https://opencode.ai/zen/go/v1)
  • Model: kimi-k3

Activity

  1. github-actions commented on Aug 1, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

  2. github-actions commented on Oct 1, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions