Skip to content

Commit 26efb5c

Browse files
committed
feat(api): batch list endpoint (openai#1338)
1 parent f1b85e2 commit 26efb5c

File tree

6 files changed

+213
-2
lines changed

6 files changed

+213
-2
lines changed

.stats.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
configured_endpoints: 62
1+
configured_endpoints: 63

api.md

+1
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,5 @@ Methods:
405405

406406
- <code title="post /batches">client.batches.<a href="./src/openai/resources/batches.py">create</a>(\*\*<a href="src/openai/types/batch_create_params.py">params</a>) -> <a href="./src/openai/types/batch.py">Batch</a></code>
407407
- <code title="get /batches/{batch_id}">client.batches.<a href="./src/openai/resources/batches.py">retrieve</a>(batch_id) -> <a href="./src/openai/types/batch.py">Batch</a></code>
408+
- <code title="get /batches">client.batches.<a href="./src/openai/resources/batches.py">list</a>(\*\*<a href="src/openai/types/batch_list_params.py">params</a>) -> <a href="./src/openai/types/batch.py">SyncCursorPage[Batch]</a></code>
408409
- <code title="post /batches/{batch_id}/cancel">client.batches.<a href="./src/openai/resources/batches.py">cancel</a>(batch_id) -> <a href="./src/openai/types/batch.py">Batch</a></code>

src/openai/resources/batches.py

+119-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import httpx
99

1010
from .. import _legacy_response
11-
from ..types import Batch, batch_create_params
11+
from ..types import Batch, batch_list_params, batch_create_params
1212
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven
1313
from .._utils import (
1414
maybe_transform,
@@ -17,7 +17,9 @@
1717
from .._compat import cached_property
1818
from .._resource import SyncAPIResource, AsyncAPIResource
1919
from .._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
20+
from ..pagination import SyncCursorPage, AsyncCursorPage
2021
from .._base_client import (
22+
AsyncPaginator,
2123
make_request_options,
2224
)
2325

@@ -125,6 +127,58 @@ def retrieve(
125127
cast_to=Batch,
126128
)
127129

130+
def list(
131+
self,
132+
*,
133+
after: str | NotGiven = NOT_GIVEN,
134+
limit: int | NotGiven = NOT_GIVEN,
135+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
136+
# The extra values given here take precedence over values defined on the client or passed to this method.
137+
extra_headers: Headers | None = None,
138+
extra_query: Query | None = None,
139+
extra_body: Body | None = None,
140+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
141+
) -> SyncCursorPage[Batch]:
142+
"""List your organization's batches.
143+
144+
Args:
145+
after: A cursor for use in pagination.
146+
147+
`after` is an object ID that defines your place
148+
in the list. For instance, if you make a list request and receive 100 objects,
149+
ending with obj_foo, your subsequent call can include after=obj_foo in order to
150+
fetch the next page of the list.
151+
152+
limit: A limit on the number of objects to be returned. Limit can range between 1 and
153+
100, and the default is 20.
154+
155+
extra_headers: Send extra headers
156+
157+
extra_query: Add additional query parameters to the request
158+
159+
extra_body: Add additional JSON properties to the request
160+
161+
timeout: Override the client-level default timeout for this request, in seconds
162+
"""
163+
return self._get_api_list(
164+
"/batches",
165+
page=SyncCursorPage[Batch],
166+
options=make_request_options(
167+
extra_headers=extra_headers,
168+
extra_query=extra_query,
169+
extra_body=extra_body,
170+
timeout=timeout,
171+
query=maybe_transform(
172+
{
173+
"after": after,
174+
"limit": limit,
175+
},
176+
batch_list_params.BatchListParams,
177+
),
178+
),
179+
model=Batch,
180+
)
181+
128182
def cancel(
129183
self,
130184
batch_id: str,
@@ -260,6 +314,58 @@ async def retrieve(
260314
cast_to=Batch,
261315
)
262316

317+
def list(
318+
self,
319+
*,
320+
after: str | NotGiven = NOT_GIVEN,
321+
limit: int | NotGiven = NOT_GIVEN,
322+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
323+
# The extra values given here take precedence over values defined on the client or passed to this method.
324+
extra_headers: Headers | None = None,
325+
extra_query: Query | None = None,
326+
extra_body: Body | None = None,
327+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
328+
) -> AsyncPaginator[Batch, AsyncCursorPage[Batch]]:
329+
"""List your organization's batches.
330+
331+
Args:
332+
after: A cursor for use in pagination.
333+
334+
`after` is an object ID that defines your place
335+
in the list. For instance, if you make a list request and receive 100 objects,
336+
ending with obj_foo, your subsequent call can include after=obj_foo in order to
337+
fetch the next page of the list.
338+
339+
limit: A limit on the number of objects to be returned. Limit can range between 1 and
340+
100, and the default is 20.
341+
342+
extra_headers: Send extra headers
343+
344+
extra_query: Add additional query parameters to the request
345+
346+
extra_body: Add additional JSON properties to the request
347+
348+
timeout: Override the client-level default timeout for this request, in seconds
349+
"""
350+
return self._get_api_list(
351+
"/batches",
352+
page=AsyncCursorPage[Batch],
353+
options=make_request_options(
354+
extra_headers=extra_headers,
355+
extra_query=extra_query,
356+
extra_body=extra_body,
357+
timeout=timeout,
358+
query=maybe_transform(
359+
{
360+
"after": after,
361+
"limit": limit,
362+
},
363+
batch_list_params.BatchListParams,
364+
),
365+
),
366+
model=Batch,
367+
)
368+
263369
async def cancel(
264370
self,
265371
batch_id: str,
@@ -304,6 +410,9 @@ def __init__(self, batches: Batches) -> None:
304410
self.retrieve = _legacy_response.to_raw_response_wrapper(
305411
batches.retrieve,
306412
)
413+
self.list = _legacy_response.to_raw_response_wrapper(
414+
batches.list,
415+
)
307416
self.cancel = _legacy_response.to_raw_response_wrapper(
308417
batches.cancel,
309418
)
@@ -319,6 +428,9 @@ def __init__(self, batches: AsyncBatches) -> None:
319428
self.retrieve = _legacy_response.async_to_raw_response_wrapper(
320429
batches.retrieve,
321430
)
431+
self.list = _legacy_response.async_to_raw_response_wrapper(
432+
batches.list,
433+
)
322434
self.cancel = _legacy_response.async_to_raw_response_wrapper(
323435
batches.cancel,
324436
)
@@ -334,6 +446,9 @@ def __init__(self, batches: Batches) -> None:
334446
self.retrieve = to_streamed_response_wrapper(
335447
batches.retrieve,
336448
)
449+
self.list = to_streamed_response_wrapper(
450+
batches.list,
451+
)
337452
self.cancel = to_streamed_response_wrapper(
338453
batches.cancel,
339454
)
@@ -349,6 +464,9 @@ def __init__(self, batches: AsyncBatches) -> None:
349464
self.retrieve = async_to_streamed_response_wrapper(
350465
batches.retrieve,
351466
)
467+
self.list = async_to_streamed_response_wrapper(
468+
batches.list,
469+
)
352470
self.cancel = async_to_streamed_response_wrapper(
353471
batches.cancel,
354472
)

src/openai/types/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .images_response import ImagesResponse as ImagesResponse
2323
from .completion_usage import CompletionUsage as CompletionUsage
2424
from .file_list_params import FileListParams as FileListParams
25+
from .batch_list_params import BatchListParams as BatchListParams
2526
from .completion_choice import CompletionChoice as CompletionChoice
2627
from .image_edit_params import ImageEditParams as ImageEditParams
2728
from .file_create_params import FileCreateParams as FileCreateParams

src/openai/types/batch_list_params.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from __future__ import annotations
4+
5+
from typing_extensions import TypedDict
6+
7+
__all__ = ["BatchListParams"]
8+
9+
10+
class BatchListParams(TypedDict, total=False):
11+
after: str
12+
"""A cursor for use in pagination.
13+
14+
`after` is an object ID that defines your place in the list. For instance, if
15+
you make a list request and receive 100 objects, ending with obj_foo, your
16+
subsequent call can include after=obj_foo in order to fetch the next page of the
17+
list.
18+
"""
19+
20+
limit: int
21+
"""A limit on the number of objects to be returned.
22+
23+
Limit can range between 1 and 100, and the default is 20.
24+
"""

tests/api_resources/test_batches.py

+67
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from openai import OpenAI, AsyncOpenAI
1111
from tests.utils import assert_matches_type
1212
from openai.types import Batch
13+
from openai.pagination import SyncCursorPage, AsyncCursorPage
1314

1415
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
1516

@@ -102,6 +103,39 @@ def test_path_params_retrieve(self, client: OpenAI) -> None:
102103
"",
103104
)
104105

106+
@parametrize
107+
def test_method_list(self, client: OpenAI) -> None:
108+
batch = client.batches.list()
109+
assert_matches_type(SyncCursorPage[Batch], batch, path=["response"])
110+
111+
@parametrize
112+
def test_method_list_with_all_params(self, client: OpenAI) -> None:
113+
batch = client.batches.list(
114+
after="string",
115+
limit=0,
116+
)
117+
assert_matches_type(SyncCursorPage[Batch], batch, path=["response"])
118+
119+
@parametrize
120+
def test_raw_response_list(self, client: OpenAI) -> None:
121+
response = client.batches.with_raw_response.list()
122+
123+
assert response.is_closed is True
124+
assert response.http_request.headers.get("X-Stainless-Lang") == "python"
125+
batch = response.parse()
126+
assert_matches_type(SyncCursorPage[Batch], batch, path=["response"])
127+
128+
@parametrize
129+
def test_streaming_response_list(self, client: OpenAI) -> None:
130+
with client.batches.with_streaming_response.list() as response:
131+
assert not response.is_closed
132+
assert response.http_request.headers.get("X-Stainless-Lang") == "python"
133+
134+
batch = response.parse()
135+
assert_matches_type(SyncCursorPage[Batch], batch, path=["response"])
136+
137+
assert cast(Any, response.is_closed) is True
138+
105139
@parametrize
106140
def test_method_cancel(self, client: OpenAI) -> None:
107141
batch = client.batches.cancel(
@@ -229,6 +263,39 @@ async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
229263
"",
230264
)
231265

266+
@parametrize
267+
async def test_method_list(self, async_client: AsyncOpenAI) -> None:
268+
batch = await async_client.batches.list()
269+
assert_matches_type(AsyncCursorPage[Batch], batch, path=["response"])
270+
271+
@parametrize
272+
async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
273+
batch = await async_client.batches.list(
274+
after="string",
275+
limit=0,
276+
)
277+
assert_matches_type(AsyncCursorPage[Batch], batch, path=["response"])
278+
279+
@parametrize
280+
async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
281+
response = await async_client.batches.with_raw_response.list()
282+
283+
assert response.is_closed is True
284+
assert response.http_request.headers.get("X-Stainless-Lang") == "python"
285+
batch = response.parse()
286+
assert_matches_type(AsyncCursorPage[Batch], batch, path=["response"])
287+
288+
@parametrize
289+
async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
290+
async with async_client.batches.with_streaming_response.list() as response:
291+
assert not response.is_closed
292+
assert response.http_request.headers.get("X-Stainless-Lang") == "python"
293+
294+
batch = await response.parse()
295+
assert_matches_type(AsyncCursorPage[Batch], batch, path=["response"])
296+
297+
assert cast(Any, response.is_closed) is True
298+
232299
@parametrize
233300
async def test_method_cancel(self, async_client: AsyncOpenAI) -> None:
234301
batch = await async_client.batches.cancel(

0 commit comments

Comments
 (0)