Skip to content

Commit 6228f5f

Browse files
jameszyaoSimsonW
authored andcommitted
fix: use base name when uploading files
1 parent 00de987 commit 6228f5f

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

taskingai/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__title__ = "taskingai"
2-
__version__ = "0.2.2"
2+
__version__ = "0.2.3"

taskingai/file/file.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Union, Dict, BinaryIO
2-
from io import BufferedReader
2+
import os
33

44
from taskingai.client.models import FileIdData, UploadFilePurpose, UploadFileResponse
55
from taskingai.client.utils import get_api_client
@@ -16,14 +16,25 @@ def __prepare_files(file: BinaryIO, purpose: Union[UploadFilePurpose, str]) -> D
1616
:param purpose: The purpose of the upload, either as a string or UploadFilePurpose enum.
1717
:return: A dictionary formatted for the API call.
1818
"""
19-
if not isinstance(file, BufferedReader):
20-
raise ValueError("Unsupported file type: Expected a BufferedReader")
19+
if not hasattr(file, "read"):
20+
raise ValueError("Unsupported file type: Expected a file-like object with a read method")
2121

22-
file_bytes = file.read()
23-
file_name = file.name
22+
try:
23+
file_bytes = file.read()
24+
except Exception as e:
25+
raise ValueError(f"Error reading file: {e}")
26+
27+
file_name = os.path.basename(file.name)
2428

2529
if isinstance(purpose, str):
26-
purpose = UploadFilePurpose(purpose)
30+
try:
31+
purpose = UploadFilePurpose(purpose)
32+
except ValueError:
33+
raise ValueError(f"Invalid purpose value: {purpose}")
34+
35+
if not isinstance(purpose, UploadFilePurpose):
36+
raise ValueError("Purpose must be an instance of UploadFilePurpose or a valid string")
37+
2738
return {
2839
"file": (file_name, file_bytes, "application/octet-stream"),
2940
"purpose": (None, str(purpose.value)),

0 commit comments

Comments
 (0)