Based on the function signatures, client.beta.files.upload should support a PathLike as the second element in the tuple.
|
async def upload( |
|
self, |
|
*, |
|
file: FileTypes, |
|
betas: List[AnthropicBetaParam] | Omit = omit, |
|
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
|
# The extra values given here take precedence over values defined on the client or passed to this method. |
|
extra_headers: Headers | None = None, |
|
extra_query: Query | None = None, |
|
extra_body: Body | None = None, |
|
timeout: float | httpx.Timeout | None | NotGiven = not_given, |
|
) -> FileMetadata: |
|
FileTypes = Union[ |
|
# file (or bytes) |
|
FileContent, |
|
# (filename, file (or bytes)) |
|
Tuple[Optional[str], FileContent], |
|
# (filename, file (or bytes), content_type) |
|
Tuple[Optional[str], FileContent, Optional[str]], |
|
# (filename, file (or bytes), content_type, headers) |
|
Tuple[Optional[str], FileContent, Optional[str], Mapping[str, str]], |
|
] |
|
FileContent = Union[IO[bytes], bytes, PathLike[str]] |
But if I pass something like file=("foo.txt", Path("foo.txt"), "text/plain") to upload it fails because in to_httpx_files -> _transform_file the first if block catches the tuple so the second if in the function that handles reading in the PathLike contents for tuples is unreachable.
|
def to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles | None: |
|
if files is None: |
|
return None |
|
|
|
if is_mapping_t(files): |
|
files = {key: _transform_file(file) for key, file in files.items()} |
|
elif is_sequence_t(files): |
|
files = [(key, _transform_file(file)) for key, file in files] |
|
else: |
|
raise TypeError(f"Unexpected file type input {type(files)}, expected mapping or sequence") |
|
|
|
return files |
|
|
|
|
|
def _transform_file(file: FileTypes) -> HttpxFileTypes: |
|
if is_file_content(file): |
|
if isinstance(file, os.PathLike): |
|
path = pathlib.Path(file) |
|
return (path.name, path.read_bytes()) |
|
|
|
return file |
|
|
|
if is_tuple_t(file): |
|
return (file[0], read_file_content(file[1]), *file[2:]) |
|
|
|
raise TypeError(f"Expected file types input to be a FileContent type or to be a tuple") |
|
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) |
|
) |
Based on the function signatures,
client.beta.files.uploadshould support aPathLikeas the second element in the tuple.anthropic-sdk-python/src/anthropic/resources/beta/files.py
Lines 571 to 582 in d7c0974
anthropic-sdk-python/src/anthropic/_types.py
Lines 69 to 78 in d7c0974
anthropic-sdk-python/src/anthropic/_types.py
Line 58 in d7c0974
But if I pass something like
file=("foo.txt", Path("foo.txt"), "text/plain")touploadit fails because into_httpx_files->_transform_filethe first if block catches the tuple so the second if in the function that handles reading in thePathLikecontents for tuples is unreachable.anthropic-sdk-python/src/anthropic/_files.py
Lines 49 to 74 in d7c0974
anthropic-sdk-python/src/anthropic/_files.py
Lines 27 to 30 in d7c0974