anthropic-sdk-python icon indicating copy to clipboard operation
anthropic-sdk-python copied to clipboard

[BUG] `client.beta.files.upload` `PathLike` error

Open tl-hbk opened this issue 8 months ago • 2 comments

Based on the function signatures, client.beta.files.upload should support a PathLike as the second element in the tuple.

https://github.com/anthropics/anthropic-sdk-python/blob/d7c0974ce6ab94ca135b10b2a45308203c0e0c7e/src/anthropic/resources/beta/files.py#L571-L582 https://github.com/anthropics/anthropic-sdk-python/blob/d7c0974ce6ab94ca135b10b2a45308203c0e0c7e/src/anthropic/_types.py#L69-L78 https://github.com/anthropics/anthropic-sdk-python/blob/d7c0974ce6ab94ca135b10b2a45308203c0e0c7e/src/anthropic/_types.py#L58

But if I pass something like file=("foo.txt", Path("foo.txt"), "text/plain") to upload it fails because in to_httpx_files -> _transform_file the first if block catches the tuple so the second if in the function that handles reading in the PathLike contents for tuples is unreachable.

https://github.com/anthropics/anthropic-sdk-python/blob/d7c0974ce6ab94ca135b10b2a45308203c0e0c7e/src/anthropic/_files.py#L49-L74 https://github.com/anthropics/anthropic-sdk-python/blob/d7c0974ce6ab94ca135b10b2a45308203c0e0c7e/src/anthropic/_files.py#L27-L30

tl-hbk avatar Mar 31 '26 18:03 tl-hbk

Hi! I'd like to fix this.

I traced the exact bug: is_file_content() returns True for any tuple (it checks isinstance(obj, tuple)), so when you pass ("foo.txt", Path("foo.txt"), "text/plain") the function hits the is_file_content branch first and returns the raw tuple unchanged — the is_tuple_t branch below is unreachable.

Fix: swap the order in both _transform_file and _async_transform_file — check is_tuple_t first, then is_file_content. Tuples will then be routed through read_file_content(file[1]) which already handles PathLike correctly.

Would a PR be welcome? I can add a test covering (name, Path(...), mime) tuples as well.

nileshpatil6 avatar Apr 19 '26 20:04 nileshpatil6

Will be fixed in the next release (https://github.com/anthropics/anthropic-sdk-python/pull/1810/changes/070f95332daea9dc3ed19fe91251d6e5285e5560)

tomer-ant avatar Aug 11 '26 19:08 tomer-ant