fix(models): resolve ToolReferenceContentBlock forward reference causing 422 on tool_result validation - #241
Open
zouchao wants to merge 1 commit into
Open
fix(models): resolve ToolReferenceContentBlock forward reference causing 422 on tool_result validation#241zouchao wants to merge 1 commit into
zouchao wants to merge 1 commit into
Conversation
…ing 422 on tool_result validation
|
Thanks for the PR! 🎉 Before merge, we need a one-time CLA confirmation. Full CLA text: Please reply once with: You need to write once, all further messages from me can be ignored. |
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。
|
@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 |
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.
Summary
ToolReferenceContentBlock, which caused 422 validation errors when Claude Code sendstool_referenceblocks insidetool_resultcontentProblem
Claude Code v2.1+ uses the ToolSearch deferred tool mechanism, which places
tool_referencecontent blocks insidetool_resultmessages:{ "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