Commit cbdafb59

Stainless Bot <107565488+stainless-bot@users.noreply.github.com>
2023-11-09 06:27:27
feat(client): support passing chunk size for binary responses (#747)
1 parent 48834e4
Changed files (2)
src/openai/_base_client.py
@@ -1727,9 +1727,14 @@ class HttpxBinaryResponseContent(BinaryResponseContent):
         return self.response.iter_raw(chunk_size)
 
     @override
-    def stream_to_file(self, file: str | os.PathLike[str]) -> None:
+    def stream_to_file(
+        self,
+        file: str | os.PathLike[str],
+        *,
+        chunk_size: int | None = None,
+    ) -> None:
         with open(file, mode="wb") as f:
-            for data in self.response.iter_bytes():
+            for data in self.response.iter_bytes(chunk_size):
                 f.write(data)
 
     @override
@@ -1757,10 +1762,15 @@ class HttpxBinaryResponseContent(BinaryResponseContent):
         return self.response.aiter_raw(chunk_size)
 
     @override
-    async def astream_to_file(self, file: str | os.PathLike[str]) -> None:
+    async def astream_to_file(
+        self,
+        file: str | os.PathLike[str],
+        *,
+        chunk_size: int | None = None,
+    ) -> None:
         path = anyio.Path(file)
         async with await path.open(mode="wb") as f:
-            async for data in self.response.aiter_bytes():
+            async for data in self.response.aiter_bytes(chunk_size):
                 await f.write(data)
 
     @override
src/openai/_types.py
@@ -123,7 +123,12 @@ class BinaryResponseContent(ABC):
         pass
 
     @abstractmethod
-    def stream_to_file(self, file: str | PathLike[str]) -> None:
+    def stream_to_file(
+        self,
+        file: str | PathLike[str],
+        *,
+        chunk_size: int | None = None,
+    ) -> None:
         """
         Stream the output to the given file.
         """
@@ -172,7 +177,13 @@ class BinaryResponseContent(ABC):
         """
         pass
 
-    async def astream_to_file(self, file: str | PathLike[str]) -> None:
+    @abstractmethod
+    async def astream_to_file(
+        self,
+        file: str | PathLike[str],
+        *,
+        chunk_size: int | None = None,
+    ) -> None:
         """
         Stream the output to the given file.
         """