[PR #82] fix(models): sanitize unknown content block types to prevent 422 errors #79

Open
opened 2026-02-27 07:17:52 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/jwadow/kiro-gateway/pull/82
Author: @XuNeo
Created: 2/22/2026
Status: 🔄 Open

Base: mainHead: fix/sanitize-unknown-content-blocks


📝 Commits (2)

  • 459be05 feat(config): add claude-opus-4.6 to fallback models
  • a6f95c4 fix(models): sanitize unknown content block types to prevent 422 errors

📊 Changes

2 files changed (+56 additions, -1 deletions)

View changed files

📝 kiro/config.py (+1 -0)
📝 kiro/models_anthropic.py (+55 -1)

📄 Description

Summary

  • Claude Code's ToolSearch (deferred tool loading) returns tool_reference content blocks inside tool_result messages. These are an internal Claude Code content type not recognized by the Anthropic Messages API schema.
  • When the conversation history containing these blocks is forwarded through the gateway, Pydantic validation rejects them with a 422 error, breaking all subsequent API calls in the session.
  • This PR adds model_validator(mode="before") to ToolResultContentBlock and AnthropicMessage that converts unrecognized block types (e.g. tool_reference) into harmless text blocks before Pydantic validation runs.

Example

Before (causes 422):

{"type": "tool_reference", "tool_name": "mcp__gdbmcp__gdb_connect"}

After (passes validation):

{"type": "text", "text": "[tool_reference: mcp__gdbmcp__gdb_connect]"}

Test plan

  • All 147 existing unit tests pass
  • Manually verified with Claude Code using ToolSearch to load deferred MCP tools — no more 422 errors

🤖 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/jwadow/kiro-gateway/pull/82 **Author:** [@XuNeo](https://github.com/XuNeo) **Created:** 2/22/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/sanitize-unknown-content-blocks` --- ### 📝 Commits (2) - [`459be05`](https://github.com/jwadow/kiro-gateway/commit/459be0540994724b1e5ac4356b439a9decf54223) feat(config): add claude-opus-4.6 to fallback models - [`a6f95c4`](https://github.com/jwadow/kiro-gateway/commit/a6f95c428f34af87ee8b348f4e94860239159f96) fix(models): sanitize unknown content block types to prevent 422 errors ### 📊 Changes **2 files changed** (+56 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `kiro/config.py` (+1 -0) 📝 `kiro/models_anthropic.py` (+55 -1) </details> ### 📄 Description ## Summary - Claude Code's `ToolSearch` (deferred tool loading) returns `tool_reference` content blocks inside `tool_result` messages. These are an internal Claude Code content type not recognized by the Anthropic Messages API schema. - When the conversation history containing these blocks is forwarded through the gateway, Pydantic validation rejects them with a 422 error, breaking all subsequent API calls in the session. - This PR adds `model_validator(mode="before")` to `ToolResultContentBlock` and `AnthropicMessage` that converts unrecognized block types (e.g. `tool_reference`) into harmless text blocks before Pydantic validation runs. ## Example Before (causes 422): ```json {"type": "tool_reference", "tool_name": "mcp__gdbmcp__gdb_connect"} ``` After (passes validation): ```json {"type": "text", "text": "[tool_reference: mcp__gdbmcp__gdb_connect]"} ``` ## Test plan - [x] All 147 existing unit tests pass - [x] Manually verified with Claude Code using `ToolSearch` to load deferred MCP tools — no more 422 errors 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/kiro-gateway-jwadow#79
No description provided.