anthropics / anthropics/anthropic-sdk-python

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

Abierto
#1,202 7 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Python
Estrellas
3.9k
Forks
853
Merge medio
1 d 18 h
PR fusionados (30 d)
11

Descripción

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

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.