Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/anthropic/_utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,16 @@ def deepcopy_minimal(item: _T) -> _T:

- mappings, e.g. `dict`
- list
- tuple

This is done for performance reasons.
"""
if is_mapping(item):
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 is_tuple(item):
return cast(_T, tuple(deepcopy_minimal(entry) for entry in item))
return item


Expand Down
47 changes: 45 additions & 2 deletions tests/test_deepcopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,50 @@ def test_ignores_other_types() -> None:
assert_different_identities(obj1, obj2)
assert obj1["foo"] is my_obj

# tuples
# tuples (immutable contents only — still creates new tuple object)
obj3 = ("a", "b")
obj4 = deepcopy_minimal(obj3)
assert obj3 is obj4
assert obj3 is not obj4
assert obj3 == obj4


def test_tuple_with_mutable_contents() -> None:
"""Tuples containing mutable objects (like dicts) should have their contents copied.

This prevents in-place mutation of the original data when the copy is modified.
See: https://github.com/anthropics/anthropic-sdk-python/issues/1202
"""
inner_dict = {"content-type": "application/json"}
obj1 = ("filename.txt", b"content", "application/json", inner_dict)
obj2 = deepcopy_minimal(obj1)
assert obj1 == obj2
# The tuple itself should be a new object
assert obj1 is not obj2
# The inner dict should be a different object (deep copied)
assert obj1[3] is not obj2[3]
assert obj1[3] == obj2[3]
# Mutating the copy should not affect the original
obj2[3]["x-custom"] = "value"
assert "x-custom" not in obj1[3]


def test_nested_tuple_in_dict() -> None:
"""Tuples nested inside dicts should also have their mutable contents copied."""
inner_dict = {"key": "value"}
obj1 = {"file": ("name.txt", b"data", "text/plain", inner_dict)}
obj2 = deepcopy_minimal(obj1)
assert_different_identities(obj1, obj2)
# The tuple should be a new object
assert obj1["file"] is not obj2["file"]
# The inner dict should be a new object
assert obj1["file"][3] is not obj2["file"][3]


def test_tuple_in_list() -> None:
"""Tuples inside lists should also be deep copied."""
inner_dict = {"header": "value"}
obj1 = [("file.txt", b"content", "text/plain", inner_dict)]
obj2 = deepcopy_minimal(obj1)
assert_different_identities(obj1, obj2)
assert obj1[0] is not obj2[0]
assert obj1[0][3] is not obj2[0][3]