Debian Codegraph @

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