Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/anthropic/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ def to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles | None:


def _transform_file(file: FileTypes) -> HttpxFileTypes:
# Check tuple FIRST: is_file_content() also returns True for tuples, so placing
# the tuple branch second makes it unreachable — PathLike values inside a tuple
# would never be read. See https://github.com/anthropics/anthropic-sdk-python/issues/1318
if is_tuple_t(file):
return (file[0], read_file_content(file[1]), *file[2:])

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")


Expand Down Expand Up @@ -103,16 +106,19 @@ async def async_to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles


async def _async_transform_file(file: FileTypes) -> HttpxFileTypes:
# Check tuple FIRST: is_file_content() also returns True for tuples, so placing
# the tuple branch second makes it unreachable — PathLike values inside a tuple
# would never be read. See https://github.com/anthropics/anthropic-sdk-python/issues/1318
if is_tuple_t(file):
return (file[0], await async_read_file_content(file[1]), *file[2:])

if is_file_content(file):
if isinstance(file, os.PathLike):
path = anyio.Path(file)
return (path.name, await path.read_bytes())

return file

if is_tuple_t(file):
return (file[0], await async_read_file_content(file[1]), *file[2:])

raise TypeError(f"Expected file types input to be a FileContent type or to be a tuple")


Expand Down