Skip to content

Commit 33ef09f

Browse files
committed
Add IOBase support to FileServiceClient.create_file
1 parent 4f42118 commit 33ef09f

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

google/generativeai/client.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Any, cast
99
from collections.abc import Sequence
1010
import httplib2
11+
from io import IOBase
1112

1213
import google.ai.generativelanguage as glm
1314
import google.generativeai.protos as protos
@@ -88,7 +89,7 @@ def _setup_discovery_api(self, metadata: dict | Sequence[tuple[str, str]] = ()):
8889

8990
def create_file(
9091
self,
91-
path: str | pathlib.Path | os.PathLike,
92+
path: str | pathlib.Path | os.PathLike | IOBase,
9293
*,
9394
mime_type: str | None = None,
9495
name: str | None = None,
@@ -104,10 +105,16 @@ def create_file(
104105
file["name"] = name
105106
if display_name is not None:
106107
file["displayName"] = display_name
107-
108-
media = googleapiclient.http.MediaFileUpload(
109-
filename=path, mimetype=mime_type, resumable=resumable
110-
)
108+
109+
if isinstance(path, IOBase):
110+
media = googleapiclient.http.MediaIoBaseUpload(
111+
fd=path, mimetype=mime_type, resumable=resumable
112+
)
113+
else:
114+
media = googleapiclient.http.MediaFileUpload(
115+
filename=path, mimetype=mime_type, resumable=resumable
116+
)
117+
111118
request = self._discovery_api.media().upload(body={"file": file}, media_body=media)
112119
for key, value in metadata:
113120
request.headers[key] = value

0 commit comments

Comments
 (0)