Debian Codegraph @

src/anthropic/resources/messages/batches.py

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