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 @@ -63,16 +63,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 @@ -105,16 +105,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
54 changes: 54 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ def test_tuple_input() -> None:
assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes())))


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


def test_sequence_tuple_pathlike_content() -> None:
result = to_httpx_files([("file", ("custom.txt", readme_path))])
print(result)
assert result == IsList(IsTuple("file", IsTuple("custom.txt", IsBytes())))


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


def test_tuple_pathlike_content_with_headers() -> None:
result = to_httpx_files({"file": ("custom.txt", readme_path, "text/markdown", {"X-Test": "1"})})
print(result)
assert result == IsDict({"file": IsTuple("custom.txt", IsBytes(), "text/markdown", {"X-Test": "1"})})


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


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


@pytest.mark.asyncio
async def test_async_sequence_tuple_pathlike_content() -> None:
result = await async_to_httpx_files([("file", ("custom.txt", anyio.Path(readme_path)))])
print(result)
assert result == IsList(IsTuple("file", IsTuple("custom.txt", IsBytes())))


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


@pytest.mark.asyncio
async def test_async_tuple_pathlike_content_with_headers() -> None:
result = await async_to_httpx_files(
{"file": ("custom.txt", anyio.Path(readme_path), "text/markdown", {"X-Test": "1"})}
)
print(result)
assert result == IsDict({"file": IsTuple("custom.txt", IsBytes(), "text/markdown", {"X-Test": "1"})})


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