Skip to content

Commit e4a3404

Browse files
committed
fix(files): read PathLike contents in upload tuples
1 parent ab76ab5 commit e4a3404

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎src/openai/_files.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ def to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles | None:
6363

6464

6565
def _transform_file(file: FileTypes) -> HttpxFileTypes:
66+
if is_tuple_t(file):
67+
return (file[0], read_file_content(file[1]), *file[2:])
68+
6669
if is_file_content(file):
6770
if isinstance(file, os.PathLike):
6871
path = pathlib.Path(file)
6972
return (path.name, path.read_bytes())
7073

7174
return file
7275

73-
if is_tuple_t(file):
74-
return (file[0], read_file_content(file[1]), *file[2:])
75-
7676
raise TypeError(f"Expected file types input to be a FileContent type or to be a tuple")
7777

7878

@@ -105,16 +105,16 @@ async def async_to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles
105105

106106

107107
async def _async_transform_file(file: FileTypes) -> HttpxFileTypes:
108+
if is_tuple_t(file):
109+
return (file[0], await async_read_file_content(file[1]), *file[2:])
110+
108111
if is_file_content(file):
109112
if isinstance(file, os.PathLike):
110113
path = anyio.Path(file)
111114
return (path.name, await path.read_bytes())
112115

113116
return file
114117

115-
if is_tuple_t(file):
116-
return (file[0], await async_read_file_content(file[1]), *file[2:])
117-
118118
raise TypeError(f"Expected file types input to be a FileContent type or to be a tuple")
119119

120120

‎tests/test_files.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ def test_tuple_input() -> None:
1919
assert result == [("file", ("README.md", readme_path.read_bytes()))]
2020

2121

22+
def test_tuple_input_reads_pathlike_content() -> None:
23+
result = to_httpx_files({"file": ("custom.txt", readme_path)})
24+
assert result == {"file": ("custom.txt", readme_path.read_bytes())}
25+
26+
2227
@pytest.mark.asyncio
2328
async def test_async_pathlib_includes_file_name() -> None:
2429
result = await async_to_httpx_files({"file": readme_path})
@@ -37,6 +42,12 @@ async def test_async_tuple_input() -> None:
3742
assert result == [("file", ("README.md", readme_path.read_bytes()))]
3843

3944

45+
@pytest.mark.asyncio
46+
async def test_async_tuple_input_reads_pathlike_content() -> None:
47+
result = await async_to_httpx_files({"file": ("custom.txt", readme_path)})
48+
assert result == {"file": ("custom.txt", readme_path.read_bytes())}
49+
50+
4051
def test_string_not_allowed() -> None:
4152
with pytest.raises(TypeError, match="Expected file types input to be a FileContent type or to be a tuple"):
4253
to_httpx_files(

0 commit comments

Comments
 (0)