Skip to content

fix(moonshot): handle union type arrays in sanitize_moonshot_tools (#30095)#30226

Closed
Tranquil-Flow wants to merge 1 commit into
NousResearch:mainfrom
Tranquil-Flow:fix/30095-moonshot-union-type
Closed

fix(moonshot): handle union type arrays in sanitize_moonshot_tools (#30095)#30226
Tranquil-Flow wants to merge 1 commit into
NousResearch:mainfrom
Tranquil-Flow:fix/30095-moonshot-union-type

Conversation

@Tranquil-Flow

Copy link
Copy Markdown
Contributor

Summary

Fixes #30095: Kimi K2.6 crashes with TypeError: unhashable type 'list' when a tool has a parameter with union type (e.g., {"type": ["string", "null"]}).

Root Cause

In sanitize_moonshot_tools(), _fill_missing_type iterated over each parameter dict looking for anyOf/oneOf/etc., but when the type field is a list (JSON Schema union syntax), the function treats each element of the list as a dict and crashes trying to call .get() on a string.

Fix

Early-return in _fill_missing_type when param_value is not a dict, so union-type arrays and other non-dict parameter values are passed through without attempting property fill.

Changes

agent/moonshot_schema.py            |  17 ++++-
 tests/agent/test_moonshot_schema.py | 128 ++++++++++++++++++++++++++++++++++++
 2 files changed, 144 insertions(+), 1 deletion(-)

Closes #30095

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists provider/kimi Kimi / Moonshot comp/agent Core agent loop, run_agent.py, prompt builder labels May 22, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #28422 which fixes both crash sites in moonshot_schema.py (L147 _repair_schema enum cleanup + L169 _fill_missing_type). This PR only addresses the _fill_missing_type crash site. See also #28322 (same single-site scope). Root issue: #28291.

@Tranquil-Flow

Copy link
Copy Markdown
Contributor Author

Thanks for the heads-up @alt-glitch — I've reviewed #28422 and #28322 and updated this branch.

How this PR differs from #28422

Both #28322 and #28422 preserve union type lists as-is (pass-through). This PR takes a different approach: it normalises the union type to the first concrete scalar.

Why normalisation matters: Moonshot's API rejects union type arrays ("type": ["number", "string"]). Preserving the list through the sanitizer means the downstream API call still receives an invalid schema. Normalising to the first concrete type (e.g. "number") produces a schema Moonshot actually accepts.

What changed (commit 3eaa5da)

  • Added isinstance(node_type, str) guard to _repair_schema's enum-cleanup block (L147) — matching the pattern from fix: handle JSON Schema union type lists in moonshot_schema (both crash sites) #28422
  • In normal operation _fill_missing_type already normalises the type before enum cleanup, so this guard is defensive (belt-and-suspenders)
  • Two new regression tests: direct _repair_schema call with union type + enum, and end-to-end sanitize_moonshot_tool_parameters with union type + enum parameter
  • Full suite: 52 passed (42 existing + 10 new across both commits)

Scope

#28322 #28422 This PR
_fill_missing_type site ✅ (normalise, not just preserve)
_repair_schema site ✅ (defensive guard)
Approach Preserve list Preserve list Normalise to first concrete
Tests 1 4 10

This PR now covers both crash sites with the more semantically correct approach.

@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #45579 — your commit was cherry-picked onto current main with your authorship preserved in git log (5acd185f7). Thanks!

#45579

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

Labels

comp/agent Core agent loop, run_agent.py, prompt builder P2 Medium — degraded but workaround exists provider/kimi Kimi / Moonshot type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kimi-k2.6: TypeError: unhashable type 'list' in sanitize_moonshot_tools when tool has union type parameter

3 participants