Skip to content

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

@proco-company

Description

@proco-company

Hello all,

We have noticed that the files.beta.upload was mutating in-place some elements of the File parameter we were passing as arguement to the said function.

In particular we were passing a tuple of the form Tuple[Optional[str], FileContent, Optional[str], Mapping[str, str]], which you can see in

Tuple[Optional[str], FileContent, Optional[str], Mapping[str, str]],
is a legal entry for the FileTypes type.

The problem arises in the fact that deepcopy_minimal does not recursively copy anything except lists or dicts. AS such, when it encoutners the tuple that composes the entire object it stops. However this means the 4th element of this tupel (a mapping) may be mutated in-place and returned to the original caller (in this case, me) changed.

https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/_utils/_utils.py#L187

I have verified empirically this is the case (indeed it was a bug that initially drew my attention to this issue) with a simple experiment that shows in this case the "headers" dict that makes up part of the file entry has been modified in place.

Image Image

IN any case, the bug can be easily avoided on our side and I have fixed it but I thought I would raise the issue since I assume this would not be the intended behavior of the files.beta.upload method. I have not yet checked the other usages of deepcopy_minimal but I would advise someone check that in case there are other such corner cases. My suggestion would be to change the function to admit recurison on tuples as well but you likely know your system better than I do.

Cheer!

body = deepcopy_minimal({"file": file})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions