Skip to content

AttributeError: 'WindowsPath' object has no attribute 'read' #1769

Description

@kannanwisen

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

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