Repository navigation
fix(ai): add types to untyped enums in Moonshot tool schemas - #50961
Merged
Merged
Conversation
Moonshot's Chat Completions API rejects any tool schema node with an
`enum` but no `type` ("type is not defined"), wherever it appears:
properties, anyOf branches, items, additionalProperties, and $defs.
OpenAPI-derived MCP tools often omit the type, so one such tool fails
every Kimi request.
The Moonshot projection now derives the type from the enum values. Moonshot
accepts one type or one type plus "null", so enums mixing other types are
left unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Moonshot's Chat Completions API rejects any tool schema node that has an
enumbut notype:The whole request fails, not just the tool. OpenAPI-derived MCP tools often omit the type. For example, Vercel's MCP
create_flagtool haskind: {"description": "The kind of flag", "enum": ["boolean", "string", "number", "json"]}, so enabling that server breaks every Kimi request on the Moonshot chat route, which is the default route formoonshotai. Found while surveying provider schema limits for #50297.Verified live against
kimi-k3andkimi-k2.6:properties,anyOfbranches,items,additionalProperties, and$defs.constwithout a type,{},$ref, and typedanyOfbranches are accepted.typemay be one type, or a pair with"null"(["string", "null"]). Other type arrays such as["string", "number"]are rejected.Change
packages/ai/src/protocols/utils/tool-schema.ts: themoonshotprojection adds atypederived from the enum values to any node that has anenumand notype:"number")null→[type, "null"]The added type matches every enum value, so the schema still accepts the same values.
Verification
test/tool-schema-projection.test.ts: untyped enums under properties,anyOf,items,additionalProperties, and$defs; numeric, boolean, and nullable enums; typed and mixed enums left unchanged. Fails without the change.Moonshot.chat("kimi-k3")), each of the 21 Vercel MCP request bodies that contain an untyped enum as a tool: 11/21 rejected before, 21/21 accepted after.kimi-k3andkimi-k2.6(3 trials each): the models called the projected tools, and every call validated against the original schema.create_flagfor an on/off flag pickedkind: "boolean"in 6/6. A synthetic tool with untyped integer, nullable,anyOf,items,additionalProperties, and boolean enums produced exactly the expected arguments in 12/12, including3(not"3"),null(not"null"), andfalse(not"false").bun testinpackages/ai: 1497 pass.bun run check: 35/35.