|
23 | 23 |
|
24 | 24 | from anthropic import Anthropic, AsyncAnthropic, APIResponseValidationError |
25 | 25 | from anthropic._types import Omit |
26 | | -from anthropic._utils import maybe_transform |
27 | 26 | from anthropic._models import BaseModel, FinalRequestOptions |
28 | | -from anthropic._constants import RAW_RESPONSE_HEADER |
29 | 27 | from anthropic._streaming import Stream, AsyncStream |
30 | 28 | from anthropic._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError |
31 | 29 | from anthropic._base_client import ( |
|
36 | 34 | DefaultAsyncHttpxClient, |
37 | 35 | make_request_options, |
38 | 36 | ) |
39 | | -from anthropic.types.message_create_params import MessageCreateParamsNonStreaming |
40 | 37 |
|
41 | 38 | from .utils import update_env |
42 | 39 |
|
@@ -734,62 +731,39 @@ def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str |
734 | 731 |
|
735 | 732 | @mock.patch("anthropic._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) |
736 | 733 | @pytest.mark.respx(base_url=base_url) |
737 | | - def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) -> None: |
| 734 | + def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter, client: Anthropic) -> None: |
738 | 735 | respx_mock.post("/v1/messages").mock(side_effect=httpx.TimeoutException("Test timeout error")) |
739 | 736 |
|
740 | 737 | with pytest.raises(APITimeoutError): |
741 | | - self.client.post( |
742 | | - "/v1/messages", |
743 | | - body=cast( |
744 | | - object, |
745 | | - maybe_transform( |
746 | | - dict( |
747 | | - max_tokens=1024, |
748 | | - messages=[ |
749 | | - { |
750 | | - "role": "user", |
751 | | - "content": "Hello, Claude", |
752 | | - } |
753 | | - ], |
754 | | - model="claude-3-5-sonnet-latest", |
755 | | - ), |
756 | | - MessageCreateParamsNonStreaming, |
757 | | - ), |
758 | | - ), |
759 | | - cast_to=httpx.Response, |
760 | | - options={"headers": {RAW_RESPONSE_HEADER: "stream"}}, |
761 | | - ) |
| 738 | + 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-3-7-sonnet-20250219", |
| 747 | + ).__enter__() |
762 | 748 |
|
763 | 749 | assert _get_open_connections(self.client) == 0 |
764 | 750 |
|
765 | 751 | @mock.patch("anthropic._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) |
766 | 752 | @pytest.mark.respx(base_url=base_url) |
767 | | - def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> None: |
| 753 | + def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter, client: Anthropic) -> None: |
768 | 754 | respx_mock.post("/v1/messages").mock(return_value=httpx.Response(500)) |
769 | 755 |
|
770 | 756 | with pytest.raises(APIStatusError): |
771 | | - self.client.post( |
772 | | - "/v1/messages", |
773 | | - body=cast( |
774 | | - object, |
775 | | - maybe_transform( |
776 | | - dict( |
777 | | - max_tokens=1024, |
778 | | - messages=[ |
779 | | - { |
780 | | - "role": "user", |
781 | | - "content": "Hello, Claude", |
782 | | - } |
783 | | - ], |
784 | | - model="claude-3-5-sonnet-latest", |
785 | | - ), |
786 | | - MessageCreateParamsNonStreaming, |
787 | | - ), |
788 | | - ), |
789 | | - cast_to=httpx.Response, |
790 | | - options={"headers": {RAW_RESPONSE_HEADER: "stream"}}, |
791 | | - ) |
792 | | - |
| 757 | + client.messages.with_streaming_response.create( |
| 758 | + max_tokens=1024, |
| 759 | + messages=[ |
| 760 | + { |
| 761 | + "content": "Hello, world", |
| 762 | + "role": "user", |
| 763 | + } |
| 764 | + ], |
| 765 | + model="claude-3-7-sonnet-20250219", |
| 766 | + ).__enter__() |
793 | 767 | assert _get_open_connections(self.client) == 0 |
794 | 768 |
|
795 | 769 | @pytest.mark.parametrize("failures_before_success", [0, 2, 4]) |
@@ -1669,62 +1643,43 @@ async def test_parse_retry_after_header(self, remaining_retries: int, retry_afte |
1669 | 1643 |
|
1670 | 1644 | @mock.patch("anthropic._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) |
1671 | 1645 | @pytest.mark.respx(base_url=base_url) |
1672 | | - async def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) -> None: |
| 1646 | + async def test_retrying_timeout_errors_doesnt_leak( |
| 1647 | + self, respx_mock: MockRouter, async_client: AsyncAnthropic |
| 1648 | + ) -> None: |
1673 | 1649 | respx_mock.post("/v1/messages").mock(side_effect=httpx.TimeoutException("Test timeout error")) |
1674 | 1650 |
|
1675 | 1651 | with pytest.raises(APITimeoutError): |
1676 | | - await self.client.post( |
1677 | | - "/v1/messages", |
1678 | | - body=cast( |
1679 | | - object, |
1680 | | - maybe_transform( |
1681 | | - dict( |
1682 | | - max_tokens=1024, |
1683 | | - messages=[ |
1684 | | - { |
1685 | | - "role": "user", |
1686 | | - "content": "Hello, Claude", |
1687 | | - } |
1688 | | - ], |
1689 | | - model="claude-3-5-sonnet-latest", |
1690 | | - ), |
1691 | | - MessageCreateParamsNonStreaming, |
1692 | | - ), |
1693 | | - ), |
1694 | | - cast_to=httpx.Response, |
1695 | | - options={"headers": {RAW_RESPONSE_HEADER: "stream"}}, |
1696 | | - ) |
| 1652 | + await async_client.messages.with_streaming_response.create( |
| 1653 | + max_tokens=1024, |
| 1654 | + messages=[ |
| 1655 | + { |
| 1656 | + "content": "Hello, world", |
| 1657 | + "role": "user", |
| 1658 | + } |
| 1659 | + ], |
| 1660 | + model="claude-3-7-sonnet-20250219", |
| 1661 | + ).__aenter__() |
1697 | 1662 |
|
1698 | 1663 | assert _get_open_connections(self.client) == 0 |
1699 | 1664 |
|
1700 | 1665 | @mock.patch("anthropic._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) |
1701 | 1666 | @pytest.mark.respx(base_url=base_url) |
1702 | | - async def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> None: |
| 1667 | + async def test_retrying_status_errors_doesnt_leak( |
| 1668 | + self, respx_mock: MockRouter, async_client: AsyncAnthropic |
| 1669 | + ) -> None: |
1703 | 1670 | respx_mock.post("/v1/messages").mock(return_value=httpx.Response(500)) |
1704 | 1671 |
|
1705 | 1672 | with pytest.raises(APIStatusError): |
1706 | | - await self.client.post( |
1707 | | - "/v1/messages", |
1708 | | - body=cast( |
1709 | | - object, |
1710 | | - maybe_transform( |
1711 | | - dict( |
1712 | | - max_tokens=1024, |
1713 | | - messages=[ |
1714 | | - { |
1715 | | - "role": "user", |
1716 | | - "content": "Hello, Claude", |
1717 | | - } |
1718 | | - ], |
1719 | | - model="claude-3-5-sonnet-latest", |
1720 | | - ), |
1721 | | - MessageCreateParamsNonStreaming, |
1722 | | - ), |
1723 | | - ), |
1724 | | - cast_to=httpx.Response, |
1725 | | - options={"headers": {RAW_RESPONSE_HEADER: "stream"}}, |
1726 | | - ) |
1727 | | - |
| 1673 | + await async_client.messages.with_streaming_response.create( |
| 1674 | + max_tokens=1024, |
| 1675 | + messages=[ |
| 1676 | + { |
| 1677 | + "content": "Hello, world", |
| 1678 | + "role": "user", |
| 1679 | + } |
| 1680 | + ], |
| 1681 | + model="claude-3-7-sonnet-20250219", |
| 1682 | + ).__aenter__() |
1728 | 1683 | assert _get_open_connections(self.client) == 0 |
1729 | 1684 |
|
1730 | 1685 | @pytest.mark.parametrize("failures_before_success", [0, 2, 4]) |
|
0 commit comments