fix: sanitize tool_use.id on session resume to prevent 400 error#876
Open
UmmeHabiba1312 wants to merge 2 commits intoanthropics:mainfrom
Open
fix: sanitize tool_use.id on session resume to prevent 400 error#876UmmeHabiba1312 wants to merge 2 commits intoanthropics:mainfrom
UmmeHabiba1312 wants to merge 2 commits intoanthropics:mainfrom
Conversation
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.
Description
This PR fixes the "Invalid tool_use.id format" 400 Bad Request error that occurs when resuming a session using
--resumeorcontinue_conversation.Root Cause
When resuming a previous session, the SDK loads the full conversation history, which may contain
tool_use.idvalues that do not follow the strict format required by the Claude API / CLI (toolu_[a-zA-Z0-9_-]+).These malformed IDs (e.g., plain UUIDs, numeric strings, or IDs with invalid characters) cause the backend to reject the request with a 400 error.
Solution
sanitize_tool_use_ids()insrc/claude_agent_sdk/_utils.py.tool_use.idand correspondingtool_result.tool_use_idfollows the correct format by:toolu_prefix if missing.SubprocessCLITransport.connect()for initial resumed history.SubprocessCLITransport.write()for any subsequent streamed messages containing tool blocks.This change is non-destructive, safe, and only modifies IDs when they are invalid.
Testing
tool_use.idvalues now resume successfully without 400 errors.Checklist
sanitize_tool_use_ids()helpersubprocess_cli.pyto apply sanitization on resume pathsCHANGELOG.mdunder Unreleased sectionRelated Issue