Debian Codegraph @

tests/api_resources/test_messages.py

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