Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions src/openai/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ def to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles | None:


def _transform_file(file: FileTypes) -> HttpxFileTypes:
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 +103,16 @@ async def async_to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles


async def _async_transform_file(file: FileTypes) -> HttpxFileTypes:
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
13 changes: 13 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def test_tuple_input() -> None:
assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes())))


def test_tuple_content_supports_pathlike() -> None:
result = to_httpx_files({"file": ("custom.txt", readme_path)})
print(result)
assert result == IsDict({"file": IsTuple("custom.txt", IsBytes())})


@pytest.mark.asyncio
async def test_async_pathlib_includes_file_name() -> None:
result = await async_to_httpx_files({"file": readme_path})
Expand All @@ -42,6 +48,13 @@ async def test_async_tuple_input() -> None:
assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes())))


@pytest.mark.asyncio
async def test_async_tuple_content_supports_pathlike() -> None:
result = await async_to_httpx_files({"file": ("custom.txt", readme_path)})
print(result)
assert result == IsDict({"file": IsTuple("custom.txt", IsBytes())})


def test_string_not_allowed() -> None:
with pytest.raises(TypeError, match="Expected file types input to be a FileContent type or to be a tuple"):
to_httpx_files(
Expand Down