Skip to content

fix: recurse into tuples in deepcopy_minimal to prevent in-place mutation#1302

Open
Scottcjn wants to merge 3 commits intoanthropics:mainfrom
Scottcjn:fix/deepcopy-minimal-tuple-recursion
Open

fix: recurse into tuples in deepcopy_minimal to prevent in-place mutation#1302
Scottcjn wants to merge 3 commits intoanthropics:mainfrom
Scottcjn:fix/deepcopy-minimal-tuple-recursion

Conversation

@Scottcjn
Copy link
Copy Markdown

Summary

deepcopy_minimal only recursed into dicts and lists, leaving tuples as-is. When a FileTypes tuple like (name, content, mime, headers_mapping) was passed to files.beta.upload, the headers Mapping inside the tuple could be mutated in-place, corrupting the caller's original data.

Fix

Add tuple handling to deepcopy_minimal:

if isinstance(item, tuple):
    return cast(_T, tuple(deepcopy_minimal(entry) for entry in item))

One line of logic. Preserves tuple type while recursing into nested mappings.

Test plan

  • Tuples containing dicts are now deep-copied
  • Existing dict/list behavior unchanged
  • FileTypes tuple pattern (str, bytes, str, Mapping) no longer mutated

Fixes #1202

@Scottcjn Scottcjn requested a review from a team as a code owner March 26, 2026 15:24
…reaming

The beta path (_beta_messages.py) wraps from_json() with a try-except
that provides an actionable error message including the raw JSON.
The non-beta path (_messages.py) was missing this wrapper, causing
raw ValueError with no context ("expected ident at line 1 column 11").

Fixes anthropics#1265
The docstring example was copy-pasted from the sync class without
updating: wrong base class name, sync methods instead of async,
Anthropic() instead of AsyncAnthropic(), missing await.

Fixes anthropics#1290
deepcopy_minimal only copied dicts and lists, leaving tuples
as-is. When a FileTypes tuple like (name, content, mime, headers)
was passed to files.beta.upload, the headers Mapping inside the
tuple could be mutated in-place, corrupting the caller's data.

Fixes anthropics#1202
@Scottcjn Scottcjn force-pushed the fix/deepcopy-minimal-tuple-recursion branch from 7294566 to e6c07c0 Compare March 27, 2026 02:16
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