Debian Codegraph @

src/anthropic/resources/beta/messages/batches.py

raw
1 from __future__ import annotations
2
3 from typing import List, Iterable
4 from itertools import chain
5
6 import httpx2
7
8 from ...._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
9 from ...._utils import is_given, path_template, strip_not_given
10 from ...._compat import cached_property
11 from ...._resource import SyncAPIResource, AsyncAPIResource
12 from ...._response import (
13 to_raw_response_wrapper,
14 to_streamed_response_wrapper,
15 async_to_raw_response_wrapper,
16 async_to_streamed_response_wrapper,
17 )
18 from ....pagination import SyncPage, AsyncPage
19 from ...._exceptions import AnthropicError
20 from ...._base_client import AsyncPaginator, make_request_options
21 from ...._decoders.jsonl import JSONLDecoder, AsyncJSONLDecoder
22 from ....types.beta.messages import batch_create_params
23 from ....types.anthropic_beta_param import AnthropicBetaParam
24 from ....types.beta.messages.beta_message_batch import BetaMessageBatch
25 from ....types.beta.messages.beta_deleted_message_batch import BetaDeletedMessageBatch
26 from ....types.beta.messages.beta_message_batch_individual_response import BetaMessageBatchIndividualResponse
27
28 __all__ = ["Batches", "AsyncBatches"]
29
30
31 class Batches(SyncAPIResource):
32 @cached_property
33 def with_raw_response(self) -> BatchesWithRawResponse:
34 """
35 This property can be used as a prefix for any HTTP method call to return
36 the raw response object instead of the parsed content.
37
38 For more information, see https://www.github.com/anthropics/anthropic-sdk-python#accessing-raw-response-data-eg-headers
39 """
40 return BatchesWithRawResponse(self)
41
42 @cached_property
43 def with_streaming_response(self) -> BatchesWithStreamingResponse:
44 """
45 An alternative to `.with_raw_response` that doesn't eagerly read the response body.
46
47 For more information, see https://www.github.com/anthropics/anthropic-sdk-python#with_streaming_response
48 """
49 return BatchesWithStreamingResponse(self)
50
51 def create(
52 self,
53 *,
54 requests: Iterable[batch_create_params.Request],
55 betas: List[AnthropicBetaParam] | Omit = omit,
56 user_profile_id: str | Omit = omit,
57 workspace_id: str | Omit = omit,
58 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
59 # The extra values given here take precedence over values defined on the client or passed to this method.
60 extra_headers: Headers | None = None,
61 extra_query: Query | None = None,
62 extra_body: Body | None = None,
63 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
64 ) -> BetaMessageBatch:
65 """
66 Send a batch of Message creation requests.
67
68 The Message Batches API can be used to process multiple Messages API requests at
69 once. Once a Message Batch is created, it begins processing immediately. Batches
70 can take up to 24 hours to complete.
71
72 Learn more about the Message Batches API in our
73 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
74
75 Args:
76 requests: List of requests for prompt completion. Each is an individual request to create
77 a Message.
78
79 betas: Optional header to specify the beta version(s) you want to use.
80
81 user_profile_id: The user profile ID to attribute the requests in this batch to. Use when acting
82 on behalf of a party other than your organization. Requires the `user-profiles`
83 beta header. Applies to every request in the batch; an individual request whose
84 `user_profile_id` body field conflicts with this header is errored.
85
86 workspace_id: Optional header to select the Workspace for this request. The value is a
87 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
88
89 Only needed for credentials that can act on more than one Workspace. A
90 credential that belongs to a specific Workspace may omit it; if sent, it must
91 match that Workspace.
92
93 extra_headers: Send extra headers
94
95 extra_query: Add additional query parameters to the request
96
97 extra_body: Add additional JSON properties to the request
98
99 timeout: Override the client-level default timeout for this request, in seconds
100 """
101 extra_headers = {
102 **strip_not_given(
103 {
104 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
105 if is_given(betas)
106 else not_given,
107 "anthropic-user-profile-id": user_profile_id,
108 "anthropic-workspace-id": workspace_id,
109 }
110 ),
111 **(extra_headers or {}),
112 }
113 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
114 return self._post(
115 "/v1/messages/batches?beta=true",
116 body={"requests": requests},
117 options=make_request_options(
118 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
119 ),
120 cast_to=BetaMessageBatch,
121 )
122
123 def retrieve(
124 self,
125 message_batch_id: str,
126 *,
127 betas: List[AnthropicBetaParam] | Omit = omit,
128 workspace_id: str | Omit = omit,
129 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
130 # The extra values given here take precedence over values defined on the client or passed to this method.
131 extra_headers: Headers | None = None,
132 extra_query: Query | None = None,
133 extra_body: Body | None = None,
134 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
135 ) -> BetaMessageBatch:
136 """This endpoint is idempotent and can be used to poll for Message Batch
137 completion.
138
139 To access the results of a Message Batch, make a request to the
140 `results_url` field in the response.
141
142 Learn more about the Message Batches API in our
143 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
144
145 Args:
146 message_batch_id: ID of the Message Batch.
147
148 betas: Optional header to specify the beta version(s) you want to use.
149
150 workspace_id: Optional header to select the Workspace for this request. The value is a
151 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
152
153 Only needed for credentials that can act on more than one Workspace. A
154 credential that belongs to a specific Workspace may omit it; if sent, it must
155 match that Workspace.
156
157 extra_headers: Send extra headers
158
159 extra_query: Add additional query parameters to the request
160
161 extra_body: Add additional JSON properties to the request
162
163 timeout: Override the client-level default timeout for this request, in seconds
164 """
165 if not message_batch_id:
166 raise ValueError(f"Expected a non-empty value for `message_batch_id` but received {message_batch_id!r}")
167 extra_headers = {
168 **strip_not_given(
169 {
170 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
171 if is_given(betas)
172 else not_given,
173 "anthropic-workspace-id": workspace_id,
174 }
175 ),
176 **(extra_headers or {}),
177 }
178 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
179 return self._get(
180 path_template("/v1/messages/batches/{message_batch_id}?beta=true", message_batch_id=message_batch_id),
181 options=make_request_options(
182 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
183 ),
184 cast_to=BetaMessageBatch,
185 )
186
187 def list(
188 self,
189 *,
190 after_id: str | Omit = omit,
191 before_id: str | Omit = omit,
192 limit: int | Omit = omit,
193 betas: List[AnthropicBetaParam] | Omit = omit,
194 workspace_id: str | Omit = omit,
195 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
196 # The extra values given here take precedence over values defined on the client or passed to this method.
197 extra_headers: Headers | None = None,
198 extra_query: Query | None = None,
199 extra_body: Body | None = None,
200 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
201 ) -> SyncPage[BetaMessageBatch]:
202 """List all Message Batches within a Workspace.
203
204 Most recently created batches are
205 returned first.
206
207 Learn more about the Message Batches API in our
208 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
209
210 Args:
211 after_id: ID of the object to use as a cursor for pagination. When provided, returns the
212 page of results immediately after this object.
213
214 before_id: ID of the object to use as a cursor for pagination. When provided, returns the
215 page of results immediately before this object.
216
217 limit: Number of items to return per page.
218
219 Defaults to `20`. Ranges from `1` to `1000`.
220
221 betas: Optional header to specify the beta version(s) you want to use.
222
223 workspace_id: Optional header to select the Workspace for this request. The value is a
224 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
225
226 Only needed for credentials that can act on more than one Workspace. A
227 credential that belongs to a specific Workspace may omit it; if sent, it must
228 match that Workspace.
229
230 extra_headers: Send extra headers
231
232 extra_query: Add additional query parameters to the request
233
234 extra_body: Add additional JSON properties to the request
235
236 timeout: Override the client-level default timeout for this request, in seconds
237 """
238 extra_headers = {
239 **strip_not_given(
240 {
241 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
242 if is_given(betas)
243 else not_given,
244 "anthropic-workspace-id": workspace_id,
245 }
246 ),
247 **(extra_headers or {}),
248 }
249 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
250 return self._get_api_list(
251 "/v1/messages/batches?beta=true",
252 page=SyncPage[BetaMessageBatch],
253 options=make_request_options(
254 extra_headers=extra_headers,
255 extra_query=extra_query,
256 extra_body=extra_body,
257 timeout=timeout,
258 query={
259 "after_id": after_id,
260 "before_id": before_id,
261 "limit": limit,
262 },
263 ),
264 model=BetaMessageBatch,
265 )
266
267 def delete(
268 self,
269 message_batch_id: str,
270 *,
271 betas: List[AnthropicBetaParam] | Omit = omit,
272 workspace_id: str | Omit = omit,
273 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
274 # The extra values given here take precedence over values defined on the client or passed to this method.
275 extra_headers: Headers | None = None,
276 extra_query: Query | None = None,
277 extra_body: Body | None = None,
278 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
279 ) -> BetaDeletedMessageBatch:
280 """
281 Delete a Message Batch.
282
283 Message Batches can only be deleted once they've finished processing. If you'd
284 like to delete an in-progress batch, you must first cancel it.
285
286 Learn more about the Message Batches API in our
287 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
288
289 Args:
290 message_batch_id: ID of the Message Batch.
291
292 betas: Optional header to specify the beta version(s) you want to use.
293
294 workspace_id: Optional header to select the Workspace for this request. The value is a
295 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
296
297 Only needed for credentials that can act on more than one Workspace. A
298 credential that belongs to a specific Workspace may omit it; if sent, it must
299 match that Workspace.
300
301 extra_headers: Send extra headers
302
303 extra_query: Add additional query parameters to the request
304
305 extra_body: Add additional JSON properties to the request
306
307 timeout: Override the client-level default timeout for this request, in seconds
308 """
309 if not message_batch_id:
310 raise ValueError(f"Expected a non-empty value for `message_batch_id` but received {message_batch_id!r}")
311 extra_headers = {
312 **strip_not_given(
313 {
314 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
315 if is_given(betas)
316 else not_given,
317 "anthropic-workspace-id": workspace_id,
318 }
319 ),
320 **(extra_headers or {}),
321 }
322 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
323 return self._delete(
324 path_template("/v1/messages/batches/{message_batch_id}?beta=true", message_batch_id=message_batch_id),
325 options=make_request_options(
326 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
327 ),
328 cast_to=BetaDeletedMessageBatch,
329 )
330
331 def cancel(
332 self,
333 message_batch_id: str,
334 *,
335 betas: List[AnthropicBetaParam] | Omit = omit,
336 workspace_id: str | Omit = omit,
337 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
338 # The extra values given here take precedence over values defined on the client or passed to this method.
339 extra_headers: Headers | None = None,
340 extra_query: Query | None = None,
341 extra_body: Body | None = None,
342 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
343 ) -> BetaMessageBatch:
344 """Batches may be canceled any time before processing ends.
345
346 Once cancellation is
347 initiated, the batch enters a `canceling` state, at which time the system may
348 complete any in-progress, non-interruptible requests before finalizing
349 cancellation.
350
351 The number of canceled requests is specified in `request_counts`. To determine
352 which requests were canceled, check the individual results within the batch.
353 Note that cancellation may not result in any canceled requests if they were
354 non-interruptible.
355
356 Learn more about the Message Batches API in our
357 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
358
359 Args:
360 message_batch_id: ID of the Message Batch.
361
362 betas: Optional header to specify the beta version(s) you want to use.
363
364 workspace_id: Optional header to select the Workspace for this request. The value is a
365 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
366
367 Only needed for credentials that can act on more than one Workspace. A
368 credential that belongs to a specific Workspace may omit it; if sent, it must
369 match that Workspace.
370
371 extra_headers: Send extra headers
372
373 extra_query: Add additional query parameters to the request
374
375 extra_body: Add additional JSON properties to the request
376
377 timeout: Override the client-level default timeout for this request, in seconds
378 """
379 if not message_batch_id:
380 raise ValueError(f"Expected a non-empty value for `message_batch_id` but received {message_batch_id!r}")
381 extra_headers = {
382 **strip_not_given(
383 {
384 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
385 if is_given(betas)
386 else not_given,
387 "anthropic-workspace-id": workspace_id,
388 }
389 ),
390 **(extra_headers or {}),
391 }
392 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
393 return self._post(
394 path_template(
395 "/v1/messages/batches/{message_batch_id}/cancel?beta=true", message_batch_id=message_batch_id
396 ),
397 options=make_request_options(
398 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
399 ),
400 cast_to=BetaMessageBatch,
401 )
402
403 def results(
404 self,
405 message_batch_id: str,
406 *,
407 betas: List[AnthropicBetaParam] | Omit = omit,
408 workspace_id: str | Omit = omit,
409 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
410 # The extra values given here take precedence over values defined on the client or passed to this method.
411 extra_headers: Headers | None = None,
412 extra_query: Query | None = None,
413 extra_body: Body | None = None,
414 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
415 ) -> JSONLDecoder[BetaMessageBatchIndividualResponse]:
416 """
417 Streams the results of a Message Batch as a `.jsonl` file.
418
419 Each line in the file is a JSON object containing the result of a single request
420 in the Message Batch. Results are not guaranteed to be in the same order as
421 requests. Use the `custom_id` field to match results to requests.
422
423 Learn more about the Message Batches API in our
424 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
425
426 Args:
427 message_batch_id: ID of the Message Batch.
428
429 betas: Optional header to specify the beta version(s) you want to use.
430
431 workspace_id: Optional header to select the Workspace for this request. The value is a
432 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
433
434 Only needed for credentials that can act on more than one Workspace. A
435 credential that belongs to a specific Workspace may omit it; if sent, it must
436 match that Workspace.
437
438 extra_headers: Send extra headers
439
440 extra_query: Add additional query parameters to the request
441
442 extra_body: Add additional JSON properties to the request
443
444 timeout: Override the client-level default timeout for this request, in seconds
445 """
446 if not message_batch_id:
447 raise ValueError(f"Expected a non-empty value for `message_batch_id` but received {message_batch_id!r}")
448
449 batch = self.retrieve(message_batch_id=message_batch_id, workspace_id=workspace_id)
450 if not batch.results_url:
451 raise AnthropicError(
452 f"No `results_url` for the given batch; Has it finished processing? {batch.processing_status}"
453 )
454
455 extra_headers = {"Accept": "application/binary", **(extra_headers or {})}
456 extra_headers = {
457 **strip_not_given(
458 {
459 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
460 if is_given(betas)
461 else not_given,
462 "anthropic-workspace-id": workspace_id,
463 }
464 ),
465 **(extra_headers or {}),
466 }
467 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
468 return self._get(
469 path_template(batch.results_url, message_batch_id=message_batch_id),
470 options=make_request_options(
471 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
472 ),
473 cast_to=JSONLDecoder[BetaMessageBatchIndividualResponse],
474 stream=True,
475 )
476
477
478 class AsyncBatches(AsyncAPIResource):
479 @cached_property
480 def with_raw_response(self) -> AsyncBatchesWithRawResponse:
481 """
482 This property can be used as a prefix for any HTTP method call to return
483 the raw response object instead of the parsed content.
484
485 For more information, see https://www.github.com/anthropics/anthropic-sdk-python#accessing-raw-response-data-eg-headers
486 """
487 return AsyncBatchesWithRawResponse(self)
488
489 @cached_property
490 def with_streaming_response(self) -> AsyncBatchesWithStreamingResponse:
491 """
492 An alternative to `.with_raw_response` that doesn't eagerly read the response body.
493
494 For more information, see https://www.github.com/anthropics/anthropic-sdk-python#with_streaming_response
495 """
496 return AsyncBatchesWithStreamingResponse(self)
497
498 async def create(
499 self,
500 *,
501 requests: Iterable[batch_create_params.Request],
502 betas: List[AnthropicBetaParam] | Omit = omit,
503 user_profile_id: str | Omit = omit,
504 workspace_id: str | Omit = omit,
505 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
506 # The extra values given here take precedence over values defined on the client or passed to this method.
507 extra_headers: Headers | None = None,
508 extra_query: Query | None = None,
509 extra_body: Body | None = None,
510 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
511 ) -> BetaMessageBatch:
512 """
513 Send a batch of Message creation requests.
514
515 The Message Batches API can be used to process multiple Messages API requests at
516 once. Once a Message Batch is created, it begins processing immediately. Batches
517 can take up to 24 hours to complete.
518
519 Learn more about the Message Batches API in our
520 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
521
522 Args:
523 requests: List of requests for prompt completion. Each is an individual request to create
524 a Message.
525
526 betas: Optional header to specify the beta version(s) you want to use.
527
528 user_profile_id: The user profile ID to attribute the requests in this batch to. Use when acting
529 on behalf of a party other than your organization. Requires the `user-profiles`
530 beta header. Applies to every request in the batch; an individual request whose
531 `user_profile_id` body field conflicts with this header is errored.
532
533 workspace_id: Optional header to select the Workspace for this request. The value is a
534 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
535
536 Only needed for credentials that can act on more than one Workspace. A
537 credential that belongs to a specific Workspace may omit it; if sent, it must
538 match that Workspace.
539
540 extra_headers: Send extra headers
541
542 extra_query: Add additional query parameters to the request
543
544 extra_body: Add additional JSON properties to the request
545
546 timeout: Override the client-level default timeout for this request, in seconds
547 """
548 extra_headers = {
549 **strip_not_given(
550 {
551 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
552 if is_given(betas)
553 else not_given,
554 "anthropic-user-profile-id": user_profile_id,
555 "anthropic-workspace-id": workspace_id,
556 }
557 ),
558 **(extra_headers or {}),
559 }
560 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
561 return await self._post(
562 "/v1/messages/batches?beta=true",
563 body={"requests": requests},
564 options=make_request_options(
565 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
566 ),
567 cast_to=BetaMessageBatch,
568 )
569
570 async def retrieve(
571 self,
572 message_batch_id: str,
573 *,
574 betas: List[AnthropicBetaParam] | Omit = omit,
575 workspace_id: str | Omit = omit,
576 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
577 # The extra values given here take precedence over values defined on the client or passed to this method.
578 extra_headers: Headers | None = None,
579 extra_query: Query | None = None,
580 extra_body: Body | None = None,
581 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
582 ) -> BetaMessageBatch:
583 """This endpoint is idempotent and can be used to poll for Message Batch
584 completion.
585
586 To access the results of a Message Batch, make a request to the
587 `results_url` field in the response.
588
589 Learn more about the Message Batches API in our
590 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
591
592 Args:
593 message_batch_id: ID of the Message Batch.
594
595 betas: Optional header to specify the beta version(s) you want to use.
596
597 workspace_id: Optional header to select the Workspace for this request. The value is a
598 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
599
600 Only needed for credentials that can act on more than one Workspace. A
601 credential that belongs to a specific Workspace may omit it; if sent, it must
602 match that Workspace.
603
604 extra_headers: Send extra headers
605
606 extra_query: Add additional query parameters to the request
607
608 extra_body: Add additional JSON properties to the request
609
610 timeout: Override the client-level default timeout for this request, in seconds
611 """
612 if not message_batch_id:
613 raise ValueError(f"Expected a non-empty value for `message_batch_id` but received {message_batch_id!r}")
614 extra_headers = {
615 **strip_not_given(
616 {
617 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
618 if is_given(betas)
619 else not_given,
620 "anthropic-workspace-id": workspace_id,
621 }
622 ),
623 **(extra_headers or {}),
624 }
625 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
626 return await self._get(
627 path_template("/v1/messages/batches/{message_batch_id}?beta=true", message_batch_id=message_batch_id),
628 options=make_request_options(
629 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
630 ),
631 cast_to=BetaMessageBatch,
632 )
633
634 def list(
635 self,
636 *,
637 after_id: str | Omit = omit,
638 before_id: str | Omit = omit,
639 limit: int | Omit = omit,
640 betas: List[AnthropicBetaParam] | Omit = omit,
641 workspace_id: str | Omit = omit,
642 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
643 # The extra values given here take precedence over values defined on the client or passed to this method.
644 extra_headers: Headers | None = None,
645 extra_query: Query | None = None,
646 extra_body: Body | None = None,
647 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
648 ) -> AsyncPaginator[BetaMessageBatch, AsyncPage[BetaMessageBatch]]:
649 """List all Message Batches within a Workspace.
650
651 Most recently created batches are
652 returned first.
653
654 Learn more about the Message Batches API in our
655 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
656
657 Args:
658 after_id: ID of the object to use as a cursor for pagination. When provided, returns the
659 page of results immediately after this object.
660
661 before_id: ID of the object to use as a cursor for pagination. When provided, returns the
662 page of results immediately before this object.
663
664 limit: Number of items to return per page.
665
666 Defaults to `20`. Ranges from `1` to `1000`.
667
668 betas: Optional header to specify the beta version(s) you want to use.
669
670 workspace_id: Optional header to select the Workspace for this request. The value is a
671 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
672
673 Only needed for credentials that can act on more than one Workspace. A
674 credential that belongs to a specific Workspace may omit it; if sent, it must
675 match that Workspace.
676
677 extra_headers: Send extra headers
678
679 extra_query: Add additional query parameters to the request
680
681 extra_body: Add additional JSON properties to the request
682
683 timeout: Override the client-level default timeout for this request, in seconds
684 """
685 extra_headers = {
686 **strip_not_given(
687 {
688 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
689 if is_given(betas)
690 else not_given,
691 "anthropic-workspace-id": workspace_id,
692 }
693 ),
694 **(extra_headers or {}),
695 }
696 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
697 return self._get_api_list(
698 "/v1/messages/batches?beta=true",
699 page=AsyncPage[BetaMessageBatch],
700 options=make_request_options(
701 extra_headers=extra_headers,
702 extra_query=extra_query,
703 extra_body=extra_body,
704 timeout=timeout,
705 query={
706 "after_id": after_id,
707 "before_id": before_id,
708 "limit": limit,
709 },
710 ),
711 model=BetaMessageBatch,
712 )
713
714 async def delete(
715 self,
716 message_batch_id: str,
717 *,
718 betas: List[AnthropicBetaParam] | Omit = omit,
719 workspace_id: str | Omit = omit,
720 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
721 # The extra values given here take precedence over values defined on the client or passed to this method.
722 extra_headers: Headers | None = None,
723 extra_query: Query | None = None,
724 extra_body: Body | None = None,
725 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
726 ) -> BetaDeletedMessageBatch:
727 """
728 Delete a Message Batch.
729
730 Message Batches can only be deleted once they've finished processing. If you'd
731 like to delete an in-progress batch, you must first cancel it.
732
733 Learn more about the Message Batches API in our
734 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
735
736 Args:
737 message_batch_id: ID of the Message Batch.
738
739 betas: Optional header to specify the beta version(s) you want to use.
740
741 workspace_id: Optional header to select the Workspace for this request. The value is a
742 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
743
744 Only needed for credentials that can act on more than one Workspace. A
745 credential that belongs to a specific Workspace may omit it; if sent, it must
746 match that Workspace.
747
748 extra_headers: Send extra headers
749
750 extra_query: Add additional query parameters to the request
751
752 extra_body: Add additional JSON properties to the request
753
754 timeout: Override the client-level default timeout for this request, in seconds
755 """
756 if not message_batch_id:
757 raise ValueError(f"Expected a non-empty value for `message_batch_id` but received {message_batch_id!r}")
758 extra_headers = {
759 **strip_not_given(
760 {
761 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
762 if is_given(betas)
763 else not_given,
764 "anthropic-workspace-id": workspace_id,
765 }
766 ),
767 **(extra_headers or {}),
768 }
769 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
770 return await self._delete(
771 path_template("/v1/messages/batches/{message_batch_id}?beta=true", message_batch_id=message_batch_id),
772 options=make_request_options(
773 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
774 ),
775 cast_to=BetaDeletedMessageBatch,
776 )
777
778 async def cancel(
779 self,
780 message_batch_id: str,
781 *,
782 betas: List[AnthropicBetaParam] | Omit = omit,
783 workspace_id: str | Omit = omit,
784 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
785 # The extra values given here take precedence over values defined on the client or passed to this method.
786 extra_headers: Headers | None = None,
787 extra_query: Query | None = None,
788 extra_body: Body | None = None,
789 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
790 ) -> BetaMessageBatch:
791 """Batches may be canceled any time before processing ends.
792
793 Once cancellation is
794 initiated, the batch enters a `canceling` state, at which time the system may
795 complete any in-progress, non-interruptible requests before finalizing
796 cancellation.
797
798 The number of canceled requests is specified in `request_counts`. To determine
799 which requests were canceled, check the individual results within the batch.
800 Note that cancellation may not result in any canceled requests if they were
801 non-interruptible.
802
803 Learn more about the Message Batches API in our
804 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
805
806 Args:
807 message_batch_id: ID of the Message Batch.
808
809 betas: Optional header to specify the beta version(s) you want to use.
810
811 workspace_id: Optional header to select the Workspace for this request. The value is a
812 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
813
814 Only needed for credentials that can act on more than one Workspace. A
815 credential that belongs to a specific Workspace may omit it; if sent, it must
816 match that Workspace.
817
818 extra_headers: Send extra headers
819
820 extra_query: Add additional query parameters to the request
821
822 extra_body: Add additional JSON properties to the request
823
824 timeout: Override the client-level default timeout for this request, in seconds
825 """
826 if not message_batch_id:
827 raise ValueError(f"Expected a non-empty value for `message_batch_id` but received {message_batch_id!r}")
828 extra_headers = {
829 **strip_not_given(
830 {
831 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
832 if is_given(betas)
833 else not_given,
834 "anthropic-workspace-id": workspace_id,
835 }
836 ),
837 **(extra_headers or {}),
838 }
839 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
840 return await self._post(
841 path_template(
842 "/v1/messages/batches/{message_batch_id}/cancel?beta=true", message_batch_id=message_batch_id
843 ),
844 options=make_request_options(
845 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
846 ),
847 cast_to=BetaMessageBatch,
848 )
849
850 async def results(
851 self,
852 message_batch_id: str,
853 *,
854 betas: List[AnthropicBetaParam] | Omit = omit,
855 workspace_id: str | Omit = omit,
856 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
857 # The extra values given here take precedence over values defined on the client or passed to this method.
858 extra_headers: Headers | None = None,
859 extra_query: Query | None = None,
860 extra_body: Body | None = None,
861 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
862 ) -> AsyncJSONLDecoder[BetaMessageBatchIndividualResponse]:
863 """
864 Streams the results of a Message Batch as a `.jsonl` file.
865
866 Each line in the file is a JSON object containing the result of a single request
867 in the Message Batch. Results are not guaranteed to be in the same order as
868 requests. Use the `custom_id` field to match results to requests.
869
870 Learn more about the Message Batches API in our
871 [user guide](https://platform.claude.com/docs/en/build-with-claude/batch-processing)
872
873 Args:
874 message_batch_id: ID of the Message Batch.
875
876 betas: Optional header to specify the beta version(s) you want to use.
877
878 workspace_id: Optional header to select the Workspace for this request. The value is a
879 Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
880
881 Only needed for credentials that can act on more than one Workspace. A
882 credential that belongs to a specific Workspace may omit it; if sent, it must
883 match that Workspace.
884
885 extra_headers: Send extra headers
886
887 extra_query: Add additional query parameters to the request
888
889 extra_body: Add additional JSON properties to the request
890
891 timeout: Override the client-level default timeout for this request, in seconds
892 """
893 if not message_batch_id:
894 raise ValueError(f"Expected a non-empty value for `message_batch_id` but received {message_batch_id!r}")
895
896 batch = await self.retrieve(message_batch_id=message_batch_id, workspace_id=workspace_id)
897 if not batch.results_url:
898 raise AnthropicError(
899 f"No `results_url` for the given batch; Has it finished processing? {batch.processing_status}"
900 )
901
902 extra_headers = {"Accept": "application/binary", **(extra_headers or {})}
903 extra_headers = {
904 **strip_not_given(
905 {
906 "anthropic-beta": ",".join(chain((str(e) for e in betas), ["message-batches-2024-09-24"]))
907 if is_given(betas)
908 else not_given,
909 "anthropic-workspace-id": workspace_id,
910 }
911 ),
912 **(extra_headers or {}),
913 }
914 extra_headers = {"anthropic-beta": "message-batches-2024-09-24", **(extra_headers or {})}
915 return await self._get(
916 path_template(batch.results_url, message_batch_id=message_batch_id),
917 options=make_request_options(
918 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
919 ),
920 cast_to=AsyncJSONLDecoder[BetaMessageBatchIndividualResponse],
921 stream=True,
922 )
923
924
925 class BatchesWithRawResponse:
926 def __init__(self, batches: Batches) -> None:
927 self._batches = batches
928
929 self.create = to_raw_response_wrapper(
930 batches.create,
931 )
932 self.retrieve = to_raw_response_wrapper(
933 batches.retrieve,
934 )
935 self.list = to_raw_response_wrapper(
936 batches.list,
937 )
938 self.delete = to_raw_response_wrapper(
939 batches.delete,
940 )
941 self.cancel = to_raw_response_wrapper(
942 batches.cancel,
943 )
944 self.results = to_raw_response_wrapper(
945 batches.results,
946 )
947
948
949 class AsyncBatchesWithRawResponse:
950 def __init__(self, batches: AsyncBatches) -> None:
951 self._batches = batches
952
953 self.create = async_to_raw_response_wrapper(
954 batches.create,
955 )
956 self.retrieve = async_to_raw_response_wrapper(
957 batches.retrieve,
958 )
959 self.list = async_to_raw_response_wrapper(
960 batches.list,
961 )
962 self.delete = async_to_raw_response_wrapper(
963 batches.delete,
964 )
965 self.cancel = async_to_raw_response_wrapper(
966 batches.cancel,
967 )
968 self.results = async_to_raw_response_wrapper(
969 batches.results,
970 )
971
972
973 class BatchesWithStreamingResponse:
974 def __init__(self, batches: Batches) -> None:
975 self._batches = batches
976
977 self.create = to_streamed_response_wrapper(
978 batches.create,
979 )
980 self.retrieve = to_streamed_response_wrapper(
981 batches.retrieve,
982 )
983 self.list = to_streamed_response_wrapper(
984 batches.list,
985 )
986 self.delete = to_streamed_response_wrapper(
987 batches.delete,
988 )
989 self.cancel = to_streamed_response_wrapper(
990 batches.cancel,
991 )
992 self.results = to_streamed_response_wrapper(
993 batches.results,
994 )
995
996
997 class AsyncBatchesWithStreamingResponse:
998 def __init__(self, batches: AsyncBatches) -> None:
999 self._batches = batches
1000
1001 self.create = async_to_streamed_response_wrapper(
1002 batches.create,
1003 )
1004 self.retrieve = async_to_streamed_response_wrapper(
1005 batches.retrieve,
1006 )
1007 self.list = async_to_streamed_response_wrapper(
1008 batches.list,
1009 )
1010 self.delete = async_to_streamed_response_wrapper(
1011 batches.delete,
1012 )
1013 self.cancel = async_to_streamed_response_wrapper(
1014 batches.cancel,
1015 )
1016 self.results = async_to_streamed_response_wrapper(
1017 batches.results,
1018 )