fix: prevent dict mutation in deepcopy_minimal#1218
Open
abdelhadi703 wants to merge 1 commit intoanthropics:mainfrom
Open
fix: prevent dict mutation in deepcopy_minimal#1218abdelhadi703 wants to merge 1 commit intoanthropics:mainfrom
abdelhadi703 wants to merge 1 commit intoanthropics:mainfrom
Conversation
deepcopy_minimal previously ignored tuples, leaving nested dicts inside them (e.g. headers in FileTypes tuples) as shared references. This caused user-provided dictionaries to be mutated in place when extract_files modified the body dict during file uploads. Add tuple support to deepcopy_minimal so that nested structures within tuples are properly deep-copied, preventing unintended mutation of user data. Fixes anthropics#1202 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Fix
deepcopy_minimalmutating user-provided dictionaries in place when used infiles.beta.upload.The function previously only deep-copied
dictandlisttypes, but ignoredtuple. SinceFileTypesvalues are commonly tuples like(filename, content, content_type, headers_dict), any nested dicts inside these tuples (such as the headers mapping) were shared by reference between the original and the "copy". Whenextract_filessubsequently mutated the body dict, it could inadvertently modify the user's original data structures.This fix adds tuple support to
deepcopy_minimalso that all nested structures within tuples are properly deep-copied.Fixes #1202
Changes
deepcopy_minimalinsrc/anthropic/_utils/_utils.pytests/test_deepcopy.pyTest plan
test_simple_tupleverifies tuples are copiedtest_tuple_with_nested_dictverifies dicts inside tuples get their own identitytest_dict_with_tuple_valueverifies the full scenario (dict -> tuple -> dict)🤖 Generated with Claude Code