Description
The file= parameter of client.beta.files.upload() is annotated as FileTypes, which includes Tuple[Optional[str], FileContent], and FileContent includes os.PathLike. Based on that, passing a filename together with a Path object should be valid — and it passes type checking.
At runtime it fails with an AttributeError.
Repro
from pathlib import Path
from anthropic import Anthropic
client = Anthropic()
path = Path("Wisen.pdf")
file_obj = client.beta.files.upload(file=("Wisen-IT-Solutions.pdf", path))
Actual
AttributeError: 'WindowsPath' object has no attribute 'read'
Expected
One of:
- the
Path is read and uploaded (consistent with passing a bare Path, which works fine), or
- a clear SDK-level error explaining that a
Path is not supported inside the tuple form.
The current AttributeError doesn't mention the SDK, the file= parameter,
or what was wrong with the input, which makes it hard to diagnose.
Working alternatives
Passing the Path on its own works:
client.beta.files.upload(file=path) # OK
Passing a handle or bytes inside the tuple also works:
client.beta.files.upload(file=("Wisen-IT-Solutions.pdf", open("Wisen.pdf", "rb"))) # OK
client.beta.files.upload(file=("Wisen-IT-Solutions.pdf", path.read_bytes())) # OK
Only the Path-inside-a-tuple combination fails.
Environment
- anthropic 0.117.0
- Windows
- Python 3.11.15
Description
The
file=parameter ofclient.beta.files.upload()is annotated asFileTypes, which includesTuple[Optional[str], FileContent], andFileContentincludesos.PathLike. Based on that, passing a filename together with aPathobject should be valid — and it passes type checking.At runtime it fails with an
AttributeError.Repro
Actual
AttributeError: 'WindowsPath' object has no attribute 'read'
Expected
One of:
Pathis read and uploaded (consistent with passing a barePath, which works fine), orPathis not supported inside the tuple form.The current
AttributeErrordoesn't mention the SDK, thefile=parameter,or what was wrong with the input, which makes it hard to diagnose.
Working alternatives
Passing the
Pathon its own works:Passing a handle or bytes inside the tuple also works:
Only the
Path-inside-a-tuple combination fails.Environment