From d11cb9ac740f14a80385ef930012d238e811cb40 Mon Sep 17 00:00:00 2001 From: "2285166171@qq.com" <2285166171@qq.com> Date: Fri, 20 Mar 2026 10:24:09 +0800 Subject: [PATCH] fix(utils): recursively copy tuples in deepcopy_minimal to prevent in-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 #1202 --- src/anthropic/_utils/_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/anthropic/_utils/_utils.py b/src/anthropic/_utils/_utils.py index eec7f4a1..4e581694 100644 --- a/src/anthropic/_utils/_utils.py +++ b/src/anthropic/_utils/_utils.py @@ -181,6 +181,7 @@ def deepcopy_minimal(item: _T) -> _T: - mappings, e.g. `dict` - list + - tuple (recursively copies elements so mutable items inside are not shared) This is done for performance reasons. """ @@ -188,6 +189,8 @@ def deepcopy_minimal(item: _T) -> _T: return cast(_T, {k: deepcopy_minimal(v) for k, v in item.items()}) if is_list(item): return cast(_T, [deepcopy_minimal(entry) for entry in item]) + if isinstance(item, tuple): + return cast(_T, tuple(deepcopy_minimal(entry) for entry in item)) return item