Skip to content

fix: OpenRouter tool_calls validation error with kimi-k2 model - #932

Closed
gabrielelanaro wants to merge 1 commit into
anomalyco:devfrom
gabrielelanaro:fix/openrouter-tool-calls-validation
Closed

gabrielelanaro wants to merge 1 commit into
anomalyco:devfrom
gabrielelanaro:fix/openrouter-tool-calls-validation

Conversation

@gabrielelanaro

Copy link
Copy Markdown

Summary

Fixes AI_TypeValidationError when using OpenRouter's moonshotai/kimi-k2 model by transforming invalid tool_call responses.

Problem

The moonshotai/kimi-k2 model through OpenRouter returns tool calls with type: "" (empty string) instead of type: "function", causing the AI SDK to throw a validation error:

AI_TypeValidationError: Type validation failed
Error message: Invalid input: expected "function"

Solution

Added a custom fetch override in the OpenRouter provider configuration that:

  • Intercepts streaming responses from OpenRouter
  • Parses each data chunk in the Server-Sent Events stream
  • Transforms any tool calls with empty type to type: "function"
  • Reconstructs the stream with corrected data

Test plan

  • Reproduced the original error with opencode run -m openrouter/moonshotai/kimi-k2 "explore this project"
  • Verified the fix resolves the validation error
  • Confirmed the model now works correctly with tool calls
  • Tested that other OpenRouter models continue to work

Fixes #914

🤖 Generated with Claude Code

Fixes AI_TypeValidationError when using OpenRouter's moonshotai/kimi-k2 model.
The model returns tool_calls with type="" instead of type="function", causing
validation failures in the AI SDK. Added response transformation to fix
empty tool_call types in streaming responses.

Fixes anomalyco#914

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@thdxr

thdxr commented Jul 15, 2025

Copy link
Copy Markdown
Member

appreciate digging into this but i don't currently want to merge such a large specific fix for a single provider (novita) - the other providers work

@thdxr thdxr closed this Jul 15, 2025
andreipromarketing-dev pushed a commit to andreipromarketing-dev/opencode that referenced this pull request Apr 7, 2026
docs: fix rule installation examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kimi K2 "AI_TypeValidationError" when using tools

2 participants