fix(anthropic): sanitize tool_use/tool_result IDs to API pattern#4356
Merged
Conversation
The Anthropic Messages API rejects tool IDs that don't match `^[a-zA-Z0-9_-]+$` with a 400 error. Tool IDs originating from other providers or restored multi-turn sessions can contain invalid characters (pipes, dots). Add a deterministic _sanitize_tool_id() and apply it to both the tool_use id and the matching tool_result tool_use_id so the pair stays consistent.
Re-bin
approved these changes
Jun 17, 2026
Re-bin
left a comment
Collaborator
There was a problem hiding this comment.
LGTM. The Anthropic tool-id fix now preserves tool_use/tool_result pairing while avoiding sanitized-id collisions, and the added regression tests cover both invalid restored IDs and simple collision cases. CI is green across the full matrix.
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.
Problem
The Anthropic Messages API rejects tool IDs that don't match
^[a-zA-Z0-9_-]+$with a 400 ("String should match pattern") error. Tool IDs originating from other providers or restored multi-turn sessions can contain invalid characters (pipes, dots).Fix
Add a deterministic
_sanitize_tool_id()and apply it to both thetool_useid and the matchingtool_resulttool_use_id, so the pair stays consistent and valid.Notes