Skip to content

fix(files): read PathLike content inside (filename, content) tuples (#1769) - #1789

Open
kilanassah wants to merge 1 commit into
anthropics:mainfrom
kilanassah:fix/1769-read-pathlike-in-tuple
Open

fix(files): read PathLike content inside (filename, content) tuples (#1769)#1789
kilanassah wants to merge 1 commit into
anthropics:mainfrom
kilanassah:fix/1769-read-pathlike-in-tuple

Conversation

@kilanassah

Copy link
Copy Markdown

Problem

client.beta.files.upload(file=("name.pdf", Path(...))) raises
AttributeError: 'WindowsPath' object has no attribute 'read', even though the tuple form
Tuple[Optional[str], FileContent] (with FileContent including os.PathLike) type-checks.
A bare Path works; only a Path inside the tuple fails. Reported in #1769.

Cause

In _transform_file / _async_transform_file, is_file_content() returns True for any
tuple and is checked before is_tuple_t(). So a (filename, Path) tuple enters the
file-content branch, fails the isinstance(file, os.PathLike) check, and is returned
verbatim — the PathLike content is never read. The is_tuple_t branch that routes
file[1] through read_file_content is unreachable for tuples.

Fix

Handle the tuple form first in both the sync and async transformers. Minimal reorder; no
behavior change for any already-working input.

Tests

Adds test_tuple_with_pathlib_reads_content + async counterpart. Both fail on main
(reproduce #1769) and pass with the fix. Full tests/test_files.py stays green (16 passed).

is_file_content() matches any tuple and was checked before is_tuple_t(), so a
(filename, Path) tuple was returned verbatim and its PathLike content never read,
raising AttributeError: 'WindowsPath' object has no attribute 'read' at upload time.
Handle the tuple form first (sync + async). Adds sync+async regression tests.

Fixes anthropics#1769
@kilanassah
kilanassah requested a review from a team as a code owner July 30, 2026 16:16
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.

1 participant