Debian Codegraph @

tests/api_resources/messages/test_batches.py

raw
1 from __future__ import annotations
2
3 import os
4 import json
5 from typing import Any, cast
6
7 import httpx2
8 import pytest
9 from respx import MockRouter
10
11 from anthropic import Anthropic, AsyncAnthropic
12 from tests.utils import assert_matches_type
13 from anthropic.pagination import SyncPage, AsyncPage
14 from anthropic.types.messages import (
15 MessageBatch,
16 DeletedMessageBatch,
17 MessageBatchIndividualResponse,
18 )
19
20 base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
21
22
23 class TestBatches:
24 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
25
26 @parametrize
27 def test_method_create(self, client: Anthropic) -> None:
28 batch = client.messages.batches.create(
29 requests=[
30 {
31 "custom_id": "my-custom-id-1",
32 "params": {
33 "max_tokens": 1024,
34 "messages": [
35 {
36 "content": "Hello, world",
37 "role": "user",
38 }
39 ],
40 "model": "claude-opus-5",
41 },
42 }
43 ],
44 )
45 assert_matches_type(MessageBatch, batch, path=["response"])
46
47 @parametrize
48 def test_method_create_with_all_params(self, client: Anthropic) -> None:
49 batch = client.messages.batches.create(
50 requests=[
51 {
52 "custom_id": "my-custom-id-1",
53 "params": {
54 "max_tokens": 1024,
55 "messages": [
56 {
57 "content": "Hello, world",
58 "role": "user",
59 }
60 ],
61 "model": "claude-opus-5",
62 "cache_control": {
63 "type": "ephemeral",
64 "ttl": "5m",
65 },
66 "container": {
67 "id": "id",
68 "skills": [
69 {
70 "skill_id": "pdf",
71 "type": "anthropic",
72 "version": "latest",
73 }
74 ],
75 },
76 "inference_geo": "inference_geo",
77 "metadata": {"user_id": "13803d75-b4b5-4c3e-b2a2-6f21399b021b"},
78 "output_config": {
79 "effort": "low",
80 "format": {
81 "schema": {"foo": "bar"},
82 "type": "json_schema",
83 },
84 },
85 "service_tier": "auto",
86 "stop_sequences": ["string"],
87 "stream": False,
88 "system": [
89 {
90 "text": "Today's date is 2024-06-01.",
91 "type": "text",
92 "cache_control": {
93 "type": "ephemeral",
94 "ttl": "5m",
95 },
96 "citations": [
97 {
98 "cited_text": "The grass is green. The sky is blue.",
99 "document_index": 0,
100 "document_title": "x",
101 "end_char_index": 0,
102 "start_char_index": 0,
103 "type": "char_location",
104 }
105 ],
106 }
107 ],
108 "thinking": {
109 "type": "adaptive",
110 "display": "summarized",
111 },
112 "tool_choice": {
113 "type": "auto",
114 "disable_parallel_tool_use": True,
115 },
116 "tools": [
117 {
118 "input_schema": {
119 "type": "object",
120 "properties": {
121 "location": "bar",
122 "unit": "bar",
123 },
124 "required": ["location"],
125 },
126 "name": "name",
127 "allowed_callers": ["direct"],
128 "cache_control": {
129 "type": "ephemeral",
130 "ttl": "5m",
131 },
132 "defer_loading": True,
133 "description": "Get the current weather in a given location",
134 "eager_input_streaming": True,
135 "input_examples": [{"foo": "bar"}],
136 "strict": True,
137 "type": "custom",
138 }
139 ],
140 },
141 }
142 ],
143 user_profile_id="anthropic-user-profile-id",
144 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
145 )
146 assert_matches_type(MessageBatch, batch, path=["response"])
147
148 @parametrize
149 def test_raw_response_create(self, client: Anthropic) -> None:
150 response = client.messages.batches.with_raw_response.create(
151 requests=[
152 {
153 "custom_id": "my-custom-id-1",
154 "params": {
155 "max_tokens": 1024,
156 "messages": [
157 {
158 "content": "Hello, world",
159 "role": "user",
160 }
161 ],
162 "model": "claude-opus-5",
163 },
164 }
165 ],
166 )
167
168 assert response.is_closed is True
169 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
170 batch = response.parse()
171 assert_matches_type(MessageBatch, batch, path=["response"])
172
173 @parametrize
174 def test_streaming_response_create(self, client: Anthropic) -> None:
175 with client.messages.batches.with_streaming_response.create(
176 requests=[
177 {
178 "custom_id": "my-custom-id-1",
179 "params": {
180 "max_tokens": 1024,
181 "messages": [
182 {
183 "content": "Hello, world",
184 "role": "user",
185 }
186 ],
187 "model": "claude-opus-5",
188 },
189 }
190 ],
191 ) as response:
192 assert not response.is_closed
193 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
194
195 batch = response.parse()
196 assert_matches_type(MessageBatch, batch, path=["response"])
197
198 assert cast(Any, response.is_closed) is True
199
200 @parametrize
201 def test_method_retrieve(self, client: Anthropic) -> None:
202 batch = client.messages.batches.retrieve(
203 message_batch_id="message_batch_id",
204 )
205 assert_matches_type(MessageBatch, batch, path=["response"])
206
207 @parametrize
208 def test_method_retrieve_with_all_params(self, client: Anthropic) -> None:
209 batch = client.messages.batches.retrieve(
210 message_batch_id="message_batch_id",
211 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
212 )
213 assert_matches_type(MessageBatch, batch, path=["response"])
214
215 @parametrize
216 def test_raw_response_retrieve(self, client: Anthropic) -> None:
217 response = client.messages.batches.with_raw_response.retrieve(
218 message_batch_id="message_batch_id",
219 )
220
221 assert response.is_closed is True
222 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
223 batch = response.parse()
224 assert_matches_type(MessageBatch, batch, path=["response"])
225
226 @parametrize
227 def test_streaming_response_retrieve(self, client: Anthropic) -> None:
228 with client.messages.batches.with_streaming_response.retrieve(
229 message_batch_id="message_batch_id",
230 ) as response:
231 assert not response.is_closed
232 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
233
234 batch = response.parse()
235 assert_matches_type(MessageBatch, batch, path=["response"])
236
237 assert cast(Any, response.is_closed) is True
238
239 @parametrize
240 def test_path_params_retrieve(self, client: Anthropic) -> None:
241 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_batch_id` but received ''"):
242 client.messages.batches.with_raw_response.retrieve(
243 message_batch_id="",
244 )
245
246 @parametrize
247 def test_method_list(self, client: Anthropic) -> None:
248 batch = client.messages.batches.list()
249 assert_matches_type(SyncPage[MessageBatch], batch, path=["response"])
250
251 @parametrize
252 def test_method_list_with_all_params(self, client: Anthropic) -> None:
253 batch = client.messages.batches.list(
254 after_id="after_id",
255 before_id="before_id",
256 limit=1,
257 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
258 )
259 assert_matches_type(SyncPage[MessageBatch], batch, path=["response"])
260
261 @parametrize
262 def test_raw_response_list(self, client: Anthropic) -> None:
263 response = client.messages.batches.with_raw_response.list()
264
265 assert response.is_closed is True
266 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
267 batch = response.parse()
268 assert_matches_type(SyncPage[MessageBatch], batch, path=["response"])
269
270 @parametrize
271 def test_streaming_response_list(self, client: Anthropic) -> None:
272 with client.messages.batches.with_streaming_response.list() as response:
273 assert not response.is_closed
274 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
275
276 batch = response.parse()
277 assert_matches_type(SyncPage[MessageBatch], batch, path=["response"])
278
279 assert cast(Any, response.is_closed) is True
280
281 @parametrize
282 def test_method_delete(self, client: Anthropic) -> None:
283 batch = client.messages.batches.delete(
284 message_batch_id="message_batch_id",
285 )
286 assert_matches_type(DeletedMessageBatch, batch, path=["response"])
287
288 @parametrize
289 def test_method_delete_with_all_params(self, client: Anthropic) -> None:
290 batch = client.messages.batches.delete(
291 message_batch_id="message_batch_id",
292 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
293 )
294 assert_matches_type(DeletedMessageBatch, batch, path=["response"])
295
296 @parametrize
297 def test_raw_response_delete(self, client: Anthropic) -> None:
298 response = client.messages.batches.with_raw_response.delete(
299 message_batch_id="message_batch_id",
300 )
301
302 assert response.is_closed is True
303 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
304 batch = response.parse()
305 assert_matches_type(DeletedMessageBatch, batch, path=["response"])
306
307 @parametrize
308 def test_streaming_response_delete(self, client: Anthropic) -> None:
309 with client.messages.batches.with_streaming_response.delete(
310 message_batch_id="message_batch_id",
311 ) as response:
312 assert not response.is_closed
313 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
314
315 batch = response.parse()
316 assert_matches_type(DeletedMessageBatch, batch, path=["response"])
317
318 assert cast(Any, response.is_closed) is True
319
320 @parametrize
321 def test_path_params_delete(self, client: Anthropic) -> None:
322 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_batch_id` but received ''"):
323 client.messages.batches.with_raw_response.delete(
324 message_batch_id="",
325 )
326
327 @parametrize
328 def test_method_cancel(self, client: Anthropic) -> None:
329 batch = client.messages.batches.cancel(
330 message_batch_id="message_batch_id",
331 )
332 assert_matches_type(MessageBatch, batch, path=["response"])
333
334 @parametrize
335 def test_method_cancel_with_all_params(self, client: Anthropic) -> None:
336 batch = client.messages.batches.cancel(
337 message_batch_id="message_batch_id",
338 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
339 )
340 assert_matches_type(MessageBatch, batch, path=["response"])
341
342 @parametrize
343 def test_raw_response_cancel(self, client: Anthropic) -> None:
344 response = client.messages.batches.with_raw_response.cancel(
345 message_batch_id="message_batch_id",
346 )
347
348 assert response.is_closed is True
349 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
350 batch = response.parse()
351 assert_matches_type(MessageBatch, batch, path=["response"])
352
353 @parametrize
354 def test_streaming_response_cancel(self, client: Anthropic) -> None:
355 with client.messages.batches.with_streaming_response.cancel(
356 message_batch_id="message_batch_id",
357 ) as response:
358 assert not response.is_closed
359 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
360
361 batch = response.parse()
362 assert_matches_type(MessageBatch, batch, path=["response"])
363
364 assert cast(Any, response.is_closed) is True
365
366 @parametrize
367 def test_path_params_cancel(self, client: Anthropic) -> None:
368 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_batch_id` but received ''"):
369 client.messages.batches.with_raw_response.cancel(
370 message_batch_id="",
371 )
372
373 @pytest.mark.respx(base_url=base_url)
374 @pytest.mark.parametrize("client", [False], indirect=True)
375 def test_method_results(self, client: Anthropic, respx_mock: MockRouter) -> None:
376 respx_mock.get("/v1/messages/batches/message_batch_id").mock(
377 return_value=httpx2.Response(200, json={"results_url": "/v1/messages/batches/message_batch_id/results"})
378 )
379 respx_mock.get("/v1/messages/batches/message_batch_id/results").mock(
380 return_value=httpx2.Response(
381 200, content="\n".join([json.dumps({"foo": "bar"}), json.dumps({"bar": "baz"})])
382 )
383 )
384 results = client.messages.batches.results(
385 message_batch_id="message_batch_id",
386 )
387 assert results.http_response is not None
388 assert not results.http_response.is_stream_consumed
389
390 i = -1
391 for result in results:
392 i += 1
393 if i == 0:
394 assert result.to_dict() == {"foo": "bar"}
395 elif i == 1:
396 assert result.to_dict() == {"bar": "baz"}
397 else:
398 raise RuntimeError(f"iterated too many times, expected 2 times but got {i + 1}")
399
400 assert i == 1
401 assert results.http_response.is_stream_consumed
402
403 @parametrize
404 @pytest.mark.skip(reason="somehow hitting prod endpoint")
405 def test_raw_response_results(self, client: Anthropic) -> None:
406 response = client.messages.batches.with_raw_response.results(
407 message_batch_id="message_batch_id",
408 )
409
410 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
411 stream = response.parse()
412 for item in stream:
413 assert_matches_type(MessageBatchIndividualResponse, item, path=["line"])
414
415 @parametrize
416 @pytest.mark.skip(reason="somehow hitting prod endpoint")
417 def test_streaming_response_results(self, client: Anthropic) -> None:
418 with client.messages.batches.with_streaming_response.results(
419 message_batch_id="message_batch_id",
420 ) as response:
421 assert not response.is_closed
422 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
423
424 stream = response.parse()
425 for item in stream:
426 assert_matches_type(MessageBatchIndividualResponse, item, path=["item"])
427
428 assert cast(Any, response.is_closed) is True
429
430 @parametrize
431 def test_path_params_results(self, client: Anthropic) -> None:
432 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_batch_id` but received ''"):
433 client.messages.batches.with_raw_response.results(
434 message_batch_id="",
435 )
436
437
438 class TestAsyncBatches:
439 parametrize = pytest.mark.parametrize(
440 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
441 )
442
443 @parametrize
444 async def test_method_create(self, async_client: AsyncAnthropic) -> None:
445 batch = await async_client.messages.batches.create(
446 requests=[
447 {
448 "custom_id": "my-custom-id-1",
449 "params": {
450 "max_tokens": 1024,
451 "messages": [
452 {
453 "content": "Hello, world",
454 "role": "user",
455 }
456 ],
457 "model": "claude-opus-5",
458 },
459 }
460 ],
461 )
462 assert_matches_type(MessageBatch, batch, path=["response"])
463
464 @parametrize
465 async def test_method_create_with_all_params(self, async_client: AsyncAnthropic) -> None:
466 batch = await async_client.messages.batches.create(
467 requests=[
468 {
469 "custom_id": "my-custom-id-1",
470 "params": {
471 "max_tokens": 1024,
472 "messages": [
473 {
474 "content": "Hello, world",
475 "role": "user",
476 }
477 ],
478 "model": "claude-opus-5",
479 "cache_control": {
480 "type": "ephemeral",
481 "ttl": "5m",
482 },
483 "container": {
484 "id": "id",
485 "skills": [
486 {
487 "skill_id": "pdf",
488 "type": "anthropic",
489 "version": "latest",
490 }
491 ],
492 },
493 "inference_geo": "inference_geo",
494 "metadata": {"user_id": "13803d75-b4b5-4c3e-b2a2-6f21399b021b"},
495 "output_config": {
496 "effort": "low",
497 "format": {
498 "schema": {"foo": "bar"},
499 "type": "json_schema",
500 },
501 },
502 "service_tier": "auto",
503 "stop_sequences": ["string"],
504 "stream": False,
505 "system": [
506 {
507 "text": "Today's date is 2024-06-01.",
508 "type": "text",
509 "cache_control": {
510 "type": "ephemeral",
511 "ttl": "5m",
512 },
513 "citations": [
514 {
515 "cited_text": "The grass is green. The sky is blue.",
516 "document_index": 0,
517 "document_title": "x",
518 "end_char_index": 0,
519 "start_char_index": 0,
520 "type": "char_location",
521 }
522 ],
523 }
524 ],
525 "thinking": {
526 "type": "adaptive",
527 "display": "summarized",
528 },
529 "tool_choice": {
530 "type": "auto",
531 "disable_parallel_tool_use": True,
532 },
533 "tools": [
534 {
535 "input_schema": {
536 "type": "object",
537 "properties": {
538 "location": "bar",
539 "unit": "bar",
540 },
541 "required": ["location"],
542 },
543 "name": "name",
544 "allowed_callers": ["direct"],
545 "cache_control": {
546 "type": "ephemeral",
547 "ttl": "5m",
548 },
549 "defer_loading": True,
550 "description": "Get the current weather in a given location",
551 "eager_input_streaming": True,
552 "input_examples": [{"foo": "bar"}],
553 "strict": True,
554 "type": "custom",
555 }
556 ],
557 },
558 }
559 ],
560 user_profile_id="anthropic-user-profile-id",
561 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
562 )
563 assert_matches_type(MessageBatch, batch, path=["response"])
564
565 @parametrize
566 async def test_raw_response_create(self, async_client: AsyncAnthropic) -> None:
567 response = await async_client.messages.batches.with_raw_response.create(
568 requests=[
569 {
570 "custom_id": "my-custom-id-1",
571 "params": {
572 "max_tokens": 1024,
573 "messages": [
574 {
575 "content": "Hello, world",
576 "role": "user",
577 }
578 ],
579 "model": "claude-opus-5",
580 },
581 }
582 ],
583 )
584
585 assert response.is_closed is True
586 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
587 batch = await response.parse()
588 assert_matches_type(MessageBatch, batch, path=["response"])
589
590 @parametrize
591 async def test_streaming_response_create(self, async_client: AsyncAnthropic) -> None:
592 async with async_client.messages.batches.with_streaming_response.create(
593 requests=[
594 {
595 "custom_id": "my-custom-id-1",
596 "params": {
597 "max_tokens": 1024,
598 "messages": [
599 {
600 "content": "Hello, world",
601 "role": "user",
602 }
603 ],
604 "model": "claude-opus-5",
605 },
606 }
607 ],
608 ) as response:
609 assert not response.is_closed
610 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
611
612 batch = await response.parse()
613 assert_matches_type(MessageBatch, batch, path=["response"])
614
615 assert cast(Any, response.is_closed) is True
616
617 @parametrize
618 async def test_method_retrieve(self, async_client: AsyncAnthropic) -> None:
619 batch = await async_client.messages.batches.retrieve(
620 message_batch_id="message_batch_id",
621 )
622 assert_matches_type(MessageBatch, batch, path=["response"])
623
624 @parametrize
625 async def test_method_retrieve_with_all_params(self, async_client: AsyncAnthropic) -> None:
626 batch = await async_client.messages.batches.retrieve(
627 message_batch_id="message_batch_id",
628 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
629 )
630 assert_matches_type(MessageBatch, batch, path=["response"])
631
632 @parametrize
633 async def test_raw_response_retrieve(self, async_client: AsyncAnthropic) -> None:
634 response = await async_client.messages.batches.with_raw_response.retrieve(
635 message_batch_id="message_batch_id",
636 )
637
638 assert response.is_closed is True
639 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
640 batch = await response.parse()
641 assert_matches_type(MessageBatch, batch, path=["response"])
642
643 @parametrize
644 async def test_streaming_response_retrieve(self, async_client: AsyncAnthropic) -> None:
645 async with async_client.messages.batches.with_streaming_response.retrieve(
646 message_batch_id="message_batch_id",
647 ) as response:
648 assert not response.is_closed
649 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
650
651 batch = await response.parse()
652 assert_matches_type(MessageBatch, batch, path=["response"])
653
654 assert cast(Any, response.is_closed) is True
655
656 @parametrize
657 async def test_path_params_retrieve(self, async_client: AsyncAnthropic) -> None:
658 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_batch_id` but received ''"):
659 await async_client.messages.batches.with_raw_response.retrieve(
660 message_batch_id="",
661 )
662
663 @parametrize
664 async def test_method_list(self, async_client: AsyncAnthropic) -> None:
665 batch = await async_client.messages.batches.list()
666 assert_matches_type(AsyncPage[MessageBatch], batch, path=["response"])
667
668 @parametrize
669 async def test_method_list_with_all_params(self, async_client: AsyncAnthropic) -> None:
670 batch = await async_client.messages.batches.list(
671 after_id="after_id",
672 before_id="before_id",
673 limit=1,
674 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
675 )
676 assert_matches_type(AsyncPage[MessageBatch], batch, path=["response"])
677
678 @parametrize
679 async def test_raw_response_list(self, async_client: AsyncAnthropic) -> None:
680 response = await async_client.messages.batches.with_raw_response.list()
681
682 assert response.is_closed is True
683 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
684 batch = await response.parse()
685 assert_matches_type(AsyncPage[MessageBatch], batch, path=["response"])
686
687 @parametrize
688 async def test_streaming_response_list(self, async_client: AsyncAnthropic) -> None:
689 async with async_client.messages.batches.with_streaming_response.list() as response:
690 assert not response.is_closed
691 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
692
693 batch = await response.parse()
694 assert_matches_type(AsyncPage[MessageBatch], batch, path=["response"])
695
696 assert cast(Any, response.is_closed) is True
697
698 @parametrize
699 async def test_method_delete(self, async_client: AsyncAnthropic) -> None:
700 batch = await async_client.messages.batches.delete(
701 message_batch_id="message_batch_id",
702 )
703 assert_matches_type(DeletedMessageBatch, batch, path=["response"])
704
705 @parametrize
706 async def test_method_delete_with_all_params(self, async_client: AsyncAnthropic) -> None:
707 batch = await async_client.messages.batches.delete(
708 message_batch_id="message_batch_id",
709 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
710 )
711 assert_matches_type(DeletedMessageBatch, batch, path=["response"])
712
713 @parametrize
714 async def test_raw_response_delete(self, async_client: AsyncAnthropic) -> None:
715 response = await async_client.messages.batches.with_raw_response.delete(
716 message_batch_id="message_batch_id",
717 )
718
719 assert response.is_closed is True
720 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
721 batch = await response.parse()
722 assert_matches_type(DeletedMessageBatch, batch, path=["response"])
723
724 @parametrize
725 async def test_streaming_response_delete(self, async_client: AsyncAnthropic) -> None:
726 async with async_client.messages.batches.with_streaming_response.delete(
727 message_batch_id="message_batch_id",
728 ) as response:
729 assert not response.is_closed
730 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
731
732 batch = await response.parse()
733 assert_matches_type(DeletedMessageBatch, batch, path=["response"])
734
735 assert cast(Any, response.is_closed) is True
736
737 @parametrize
738 async def test_path_params_delete(self, async_client: AsyncAnthropic) -> None:
739 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_batch_id` but received ''"):
740 await async_client.messages.batches.with_raw_response.delete(
741 message_batch_id="",
742 )
743
744 @parametrize
745 async def test_method_cancel(self, async_client: AsyncAnthropic) -> None:
746 batch = await async_client.messages.batches.cancel(
747 message_batch_id="message_batch_id",
748 )
749 assert_matches_type(MessageBatch, batch, path=["response"])
750
751 @parametrize
752 async def test_method_cancel_with_all_params(self, async_client: AsyncAnthropic) -> None:
753 batch = await async_client.messages.batches.cancel(
754 message_batch_id="message_batch_id",
755 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
756 )
757 assert_matches_type(MessageBatch, batch, path=["response"])
758
759 @parametrize
760 async def test_raw_response_cancel(self, async_client: AsyncAnthropic) -> None:
761 response = await async_client.messages.batches.with_raw_response.cancel(
762 message_batch_id="message_batch_id",
763 )
764
765 assert response.is_closed is True
766 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
767 batch = await response.parse()
768 assert_matches_type(MessageBatch, batch, path=["response"])
769
770 @parametrize
771 async def test_streaming_response_cancel(self, async_client: AsyncAnthropic) -> None:
772 async with async_client.messages.batches.with_streaming_response.cancel(
773 message_batch_id="message_batch_id",
774 ) as response:
775 assert not response.is_closed
776 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
777
778 batch = await response.parse()
779 assert_matches_type(MessageBatch, batch, path=["response"])
780
781 assert cast(Any, response.is_closed) is True
782
783 @parametrize
784 async def test_path_params_cancel(self, async_client: AsyncAnthropic) -> None:
785 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_batch_id` but received ''"):
786 await async_client.messages.batches.with_raw_response.cancel(
787 message_batch_id="",
788 )
789
790 @pytest.mark.respx(base_url=base_url)
791 @pytest.mark.parametrize("async_client", [False], indirect=True)
792 async def test_method_results(self, async_client: AsyncAnthropic, respx_mock: MockRouter) -> None:
793 respx_mock.get("/v1/messages/batches/message_batch_id").mock(
794 return_value=httpx2.Response(200, json={"results_url": "/v1/messages/batches/message_batch_id/results"})
795 )
796 respx_mock.get("/v1/messages/batches/message_batch_id/results").mock(
797 return_value=httpx2.Response(
798 200, content="\n".join([json.dumps({"foo": "bar"}), json.dumps({"bar": "baz"})])
799 )
800 )
801 results = await async_client.messages.batches.results(
802 message_batch_id="message_batch_id",
803 )
804 assert results.http_response is not None
805 assert not results.http_response.is_stream_consumed
806
807 i = -1
808 async for result in results:
809 i += 1
810 if i == 0:
811 assert result.to_dict() == {"foo": "bar"}
812 elif i == 1:
813 assert result.to_dict() == {"bar": "baz"}
814 else:
815 raise RuntimeError(f"iterated too many times, expected 2 times but got {i + 1}")
816
817 assert i == 1
818 assert results.http_response.is_stream_consumed
819
820 @parametrize
821 @pytest.mark.skip(reason="somehow hitting prod endpoint")
822 async def test_raw_response_results(self, async_client: AsyncAnthropic) -> None:
823 response = await async_client.messages.batches.with_raw_response.results(
824 message_batch_id="message_batch_id",
825 )
826
827 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
828 stream = await response.parse()
829 async for item in stream:
830 assert_matches_type(MessageBatchIndividualResponse, item, path=["line"])
831
832 @parametrize
833 @pytest.mark.skip(reason="somehow hitting prod endpoint")
834 async def test_streaming_response_results(self, async_client: AsyncAnthropic) -> None:
835 async with async_client.messages.batches.with_streaming_response.results(
836 message_batch_id="message_batch_id",
837 ) as response:
838 assert not response.is_closed
839 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
840
841 stream = await response.parse()
842 async for item in stream:
843 assert_matches_type(MessageBatchIndividualResponse, item, path=["item"])
844
845 assert cast(Any, response.is_closed) is True
846
847 @parametrize
848 async def test_path_params_results(self, async_client: AsyncAnthropic) -> None:
849 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_batch_id` but received ''"):
850 await async_client.messages.batches.with_raw_response.results(
851 message_batch_id="",
852 )