Skip to content

fix: prevent dict mutation in deepcopy_minimal#1218

Open
abdelhadi703 wants to merge 1 commit intoanthropics:mainfrom
abdelhadi703:fix/deepcopy-minimal-mutation
Open

fix: prevent dict mutation in deepcopy_minimal#1218
abdelhadi703 wants to merge 1 commit intoanthropics:mainfrom
abdelhadi703:fix/deepcopy-minimal-mutation

Conversation

@abdelhadi703
Copy link

Summary

Fix deepcopy_minimal mutating user-provided dictionaries in place when used in files.beta.upload.

The function previously only deep-copied dict and list types, but ignored tuple. Since FileTypes values 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". When extract_files subsequently mutated the body dict, it could inadvertently modify the user's original data structures.

This fix adds tuple support to deepcopy_minimal so that all nested structures within tuples are properly deep-copied.

Fixes #1202

Changes

  • Add tuple copying support to deepcopy_minimal in src/anthropic/_utils/_utils.py
  • Add tests for tuple deep-copying in tests/test_deepcopy.py
  • Remove outdated test that asserted tuples should NOT be copied

Test plan

  • New test test_simple_tuple verifies tuples are copied
  • New test test_tuple_with_nested_dict verifies dicts inside tuples get their own identity
  • New test test_dict_with_tuple_value verifies the full scenario (dict -> tuple -> dict)
  • All existing deepcopy tests continue to pass

🤖 Generated with Claude Code

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>
@abdelhadi703 abdelhadi703 requested a review from a team as a code owner March 3, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use of deepcopy_minimal in files.beta.upload mutates dict in place

1 participant