Skip to content

fix: read file contents when tuple file inputs use PathLike#1474

Open
jay-dhamale wants to merge 1 commit intoanthropics:mainfrom
jay-dhamale:fix/file-upload-pathlike-tuple
Open

fix: read file contents when tuple file inputs use PathLike#1474
jay-dhamale wants to merge 1 commit intoanthropics:mainfrom
jay-dhamale:fix/file-upload-pathlike-tuple

Conversation

@jay-dhamale
Copy link
Copy Markdown

Summary

Fixes #1318.

_transform_file and _async_transform_file in src/anthropic/_files.py checked is_file_content before is_tuple_t. Because is_file_content returns True for any tuple (it just does isinstance(obj, tuple)), a tuple file input like ("foo.txt", Path("foo.txt"), "text/plain") fell through the file-content branch and was returned unchanged — the tuple branch that actually calls read_file_content on element [1] was unreachable for tuples.

As a result, client.beta.files.upload(file=("foo.txt", Path("foo.txt"), "text/plain")) failed downstream because the PathLike was passed through to httpx instead of being read into bytes, even though the type signature advertises PathLike support inside the tuple form.

Fix

Swap the order in both _transform_file and _async_transform_file — check is_tuple_t first, then is_file_content. Tuples now go through read_file_content / async_read_file_content, which already handle PathLike correctly.

Test plan

  • Added test_tuple_with_pathlike_reads_contents and test_tuple_with_pathlike_two_elements (sync)
  • Added matching test_async_tuple_with_pathlike_reads_contents and test_async_tuple_with_pathlike_two_elements
  • uv run pytest tests/test_files.py -v — 18 passed
  • ./scripts/lint — ruff, pyright, and mypy all clean

`_transform_file` and `_async_transform_file` checked `is_file_content`
before `is_tuple_t`. Because `is_file_content` returns True for any
tuple, a `(name, Path(...), mime)` input fell through the file-content
branch and was returned unchanged — the tuple branch that actually calls
`read_file_content` on element [1] was unreachable for tuples.

Swap the order so tuples are routed through `read_file_content` /
`async_read_file_content`, which already handle `PathLike` correctly.

Fixes anthropics#1318
@jay-dhamale jay-dhamale requested a review from a team as a code owner April 30, 2026 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant