Skip to content

fix(anthropic): sanitize tool_use/tool_result IDs to API pattern#4356

Merged
Re-bin merged 3 commits into
HKUDS:mainfrom
franciscomaestre:pr/sanitize-tool-ids
Jun 17, 2026
Merged

fix(anthropic): sanitize tool_use/tool_result IDs to API pattern#4356
Re-bin merged 3 commits into
HKUDS:mainfrom
franciscomaestre:pr/sanitize-tool-ids

Conversation

@franciscomaestre

Copy link
Copy Markdown

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 the tool_use id and the matching tool_result tool_use_id, so the pair stays consistent and valid.

Notes

  • Deterministic mapping keeps the tool_use/tool_result pair matched.
  • No-op for IDs that are already valid.

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 Re-bin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chengyongru chengyongru added bug Something isn't working valid labels Jun 17, 2026
@Re-bin Re-bin merged commit bdf21c9 into HKUDS:main Jun 17, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working valid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants