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