Skip to content

fix(files): remove erroneous tuple check from is_file_content type guard (#1318)#1408

Open
xodn348 wants to merge 1 commit intoanthropics:mainfrom
xodn348:fix/issue-1318-pathlike-upload
Open

fix(files): remove erroneous tuple check from is_file_content type guard (#1318)#1408
xodn348 wants to merge 1 commit intoanthropics:mainfrom
xodn348:fix/issue-1318-pathlike-upload

Conversation

@xodn348
Copy link
Copy Markdown

@xodn348 xodn348 commented Apr 17, 2026

Summary

Fixes #1318.

client.beta.files.upload(file=("foo.txt", Path("foo.txt"), "text/plain")) fails because the is_file_content type guard incorrectly matches tuples, preventing the tuple-specific branch in _transform_file from handling PathLike resolution.

Problem

is_file_content() included isinstance(obj, tuple) despite FileContent being defined as Union[IO[bytes], bytes, PathLike] (no tuple). This caused _transform_file to enter the FileContent branch for tuple inputs, skipping the is_tuple_t branch where read_file_content properly resolves PathLike elements.

Fix

Remove the erroneous isinstance(obj, tuple) check from is_file_content, aligning the runtime type guard with the static FileContent type definition. Tuples now correctly reach the is_tuple_t branch and have their PathLike elements opened before being passed to httpx.

Change

 def is_file_content(obj: object) -> TypeGuard[FileContent]:
-    return (
-        isinstance(obj, bytes) or isinstance(obj, tuple) or isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike)
-    )
+    return isinstance(obj, bytes) or isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike)

…ard (anthropics#1318)

The is_file_content TypeGuard included isinstance(obj, tuple) despite
FileContent being defined as Union[IO[bytes], bytes, PathLike]. This
caused _transform_file to enter the FileContent branch for tuple inputs,
skipping the tuple-specific branch where PathLike elements are resolved
via read_file_content.

Removing the tuple check allows tuples like (filename, Path(...), content_type)
to correctly reach the is_tuple_t branch and have their PathLike elements
properly opened before being passed to httpx.
@xodn348 xodn348 requested a review from a team as a code owner April 17, 2026 21:46
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