Skip to content

fix(models): resolve ToolReferenceContentBlock forward reference causing 422 on tool_result validation - #241

Open
zouchao wants to merge 1 commit into
jwadow:mainfrom
zouchao:main
Open

fix(models): resolve ToolReferenceContentBlock forward reference causing 422 on tool_result validation#241
zouchao wants to merge 1 commit into
jwadow:mainfrom
zouchao:main

Conversation

@zouchao

@zouchao zouchao commented Jun 30, 2026

Copy link
Copy Markdown

Summary

  • Fix Pydantic v2 forward reference resolution for ToolReferenceContentBlock, which caused 422 validation errors when Claude Code sends tool_reference blocks inside tool_result content

Problem

Claude Code v2.1+ uses the ToolSearch deferred tool mechanism, which places tool_reference content blocks inside tool_result messages:

{
  "type": "tool_result",
  "tool_use_id": "tooluse_xxx",
  "content": [
    {"type": "tool_reference", "tool_name": "mcp__codebase-memory-mcp__index_repository"}
  ]
}

ToolResultContentBlock.content is typed as:
content: Optional[Union[str, List[Union["TextContentBlock", "ImageContentBlock", "ToolReferenceContentBlock"]]]]

However, Pydantic v2 never resolved the "ToolReferenceContentBlock" forward reference string into the actual class. At validation time, the Union only recognized TextContentBlock and ImageContentBlock, rejecting tool_reference blocks with a 422 error.

Solution

Added model_rebuild() calls at the end of models_anthropic.py after all classes are defined:

ToolResultContentBlock.model_rebuild()
AnthropicMessage.model_rebuild()

This forces Pydantic v2 to resolve all forward reference strings in Union type annotations, allowing tool_reference blocks to pass validation correctly.

Test plan

  • Validated payload containing tool_reference inside tool_result now parses successfully
  • Existing test suite passes (235 passed)

@cla-bot

cla-bot Bot commented Jun 30, 2026

Copy link
Copy Markdown

Thanks for the PR! 🎉

Before merge, we need a one-time CLA confirmation.
It confirms that you have the right to contribute this code and allow the project to use it.

Full CLA text:
https://github.com/jwadow/kiro-gateway/blob/main/CLA.md

Please reply once with:

I have read the CLA and I accept its terms

You need to write once, all further messages from me can be ignored.

@zouchao

zouchao commented Jun 30, 2026

Copy link
Copy Markdown
Author

I have read the CLA and I accept its terms

zhujunsan added a commit to zhujunsan/kiro-gateway that referenced this pull request Jul 14, 2026
Cherry-pick upstream PR jwadow#241:Pydantic v2 需显式 model_rebuild,否则
Claude Code tool_result 内的 tool_reference 会触发 422。
@ankitcharolia

Copy link
Copy Markdown

@zouchao @sean-aligntech could you give a try to this gateway: https://github.com/ankitcharolia/kiro-gateway

It works quite well with All AI harness and actively being developed. The most important thing is that it is ACP compliant

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.

3 participants