Skip to content

fix(utils): recursively copy tuples in deepcopy_minimal to prevent in-place mutation#1270

Open
tao-to-python wants to merge 1 commit intoanthropics:mainfrom
tao-to-python:fix/deepcopy-minimal-tuple
Open

fix(utils): recursively copy tuples in deepcopy_minimal to prevent in-place mutation#1270
tao-to-python wants to merge 1 commit intoanthropics:mainfrom
tao-to-python:fix/deepcopy-minimal-tuple

Conversation

@tao-to-python
Copy link

Description

Fixes in-place mutation of user-provided data when using files.beta.upload, as reported in #1202.

Problem

deepcopy_minimal only recursively copies dict and list. When a FileTypes tuple like (filename, content, mime_type, headers_dict) is passed, the tuple is returned as-is. The headers_dict (4th element) shares the same reference, so internal SDK mutations propagate back to the caller.

Solution

Add tuple handling to deepcopy_minimal:

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

This ensures mutable objects inside tuples (like the headers dict) are properly copied.

Changes

File Change
src/anthropic/_utils/_utils.py Add tuple support to deepcopy_minimal

Fixes #1202

…-place mutation

deepcopy_minimal only handled dicts and lists, so tuples containing
mutable objects (like dicts) were returned as-is. When files.beta.upload
receives a FileTypes tuple like (filename, content, mime, headers_dict),
the headers dict inside the tuple could be mutated in place and returned
to the caller changed.

Add tuple support so mutable elements inside tuples are properly copied.

Fixes anthropics#1202
@tao-to-python tao-to-python requested a review from a team as a code owner March 20, 2026 02:24
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