fix: read file contents when tuple file inputs use PathLike#1474
Open
jay-dhamale wants to merge 1 commit intoanthropics:mainfrom
Open
fix: read file contents when tuple file inputs use PathLike#1474jay-dhamale wants to merge 1 commit intoanthropics:mainfrom
jay-dhamale wants to merge 1 commit intoanthropics:mainfrom
Conversation
`_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
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.
Summary
Fixes #1318.
_transform_fileand_async_transform_fileinsrc/anthropic/_files.pycheckedis_file_contentbeforeis_tuple_t. Becauseis_file_contentreturnsTruefor anytuple(it just doesisinstance(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 callsread_file_contenton element[1]was unreachable for tuples.As a result,
client.beta.files.upload(file=("foo.txt", Path("foo.txt"), "text/plain"))failed downstream because thePathLikewas passed through tohttpxinstead of being read into bytes, even though the type signature advertisesPathLikesupport inside the tuple form.Fix
Swap the order in both
_transform_fileand_async_transform_file— checkis_tuple_tfirst, thenis_file_content. Tuples now go throughread_file_content/async_read_file_content, which already handlePathLikecorrectly.Test plan
test_tuple_with_pathlike_reads_contentsandtest_tuple_with_pathlike_two_elements(sync)test_async_tuple_with_pathlike_reads_contentsandtest_async_tuple_with_pathlike_two_elementsuv run pytest tests/test_files.py -v— 18 passed./scripts/lint— ruff, pyright, and mypy all clean