[PR #932] [CLOSED] fix: OpenRouter tool_calls validation error with kimi-k2 model #9747

Closed
opened 2026-02-16 18:14:06 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/anomalyco/opencode/pull/932
Author: @gabrielelanaro
Created: 7/12/2025
Status: Closed

Base: devHead: fix/openrouter-tool-calls-validation


📝 Commits (1)

  • aae904f fix: OpenRouter tool_calls validation error with kimi-k2 model

📊 Changes

1 file changed (+54 additions, -0 deletions)

View changed files

📝 packages/opencode/src/provider/provider.ts (+54 -0)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/anomalyco/opencode/pull/932 **Author:** [@gabrielelanaro](https://github.com/gabrielelanaro) **Created:** 7/12/2025 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix/openrouter-tool-calls-validation` --- ### 📝 Commits (1) - [`aae904f`](https://github.com/anomalyco/opencode/commit/aae904fc9a08419415f875a2a69e64cdd85b26fc) fix: OpenRouter tool_calls validation error with kimi-k2 model ### 📊 Changes **1 file changed** (+54 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/opencode/src/provider/provider.ts` (+54 -0) </details> ### 📄 Description ## 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 - [x] Reproduced the original error with `opencode run -m openrouter/moonshotai/kimi-k2 "explore this project"` - [x] Verified the fix resolves the validation error - [x] Confirmed the model now works correctly with tool calls - [x] Tested that other OpenRouter models continue to work Fixes #914 🤖 Generated with [Claude Code](https://claude.ai/code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 18:14:06 -05:00
yindo closed this issue 2026-02-16 18:14:06 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#9747