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