anthropics / anthropics/anthropic-sdk-python

Use of `deepcopy_minimal` in `files.beta.upload` mutates dict in place

未关闭
#1,202 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
3.9k
派生
853
平均合并
1 天 18 小时
30 天内合并 PR
11

描述

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 https://github.com/anthropics/anthropic-sdk-python/blob/7a6c642f51b603f9ef46c5603f48ccd55371e4cb/src/anthropic/_types.py#L77 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!

https://github.com/anthropics/anthropic-sdk-python/blob/7a6c642f51b603f9ef46c5603f48ccd55371e4cb/src/anthropic/resources/beta/files.py#L314

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。