fix(files): read PathLike content inside (filename, content) tuples (#1769) - #1789
Open
kilanassah wants to merge 1 commit into
Open
fix(files): read PathLike content inside (filename, content) tuples (#1769)#1789kilanassah wants to merge 1 commit into
kilanassah wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
client.beta.files.upload(file=("name.pdf", Path(...)))raisesAttributeError: 'WindowsPath' object has no attribute 'read', even though the tuple formTuple[Optional[str], FileContent](withFileContentincludingos.PathLike) type-checks.A bare
Pathworks; only aPathinside the tuple fails. Reported in #1769.Cause
In
_transform_file/_async_transform_file,is_file_content()returnsTruefor anytuple and is checked before
is_tuple_t(). So a(filename, Path)tuple enters thefile-content branch, fails the
isinstance(file, os.PathLike)check, and is returnedverbatim — the
PathLikecontent is never read. Theis_tuple_tbranch that routesfile[1]throughread_file_contentis 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 onmain(reproduce #1769) and pass with the fix. Full
tests/test_files.pystays green (16 passed).