|
23 | 23 |
|
24 | 24 | from openai import OpenAI, AsyncOpenAI, APIResponseValidationError |
25 | 25 | from openai._types import Omit |
26 | | -from openai._utils import maybe_transform |
27 | 26 | from openai._models import BaseModel, FinalRequestOptions |
28 | | -from openai._constants import RAW_RESPONSE_HEADER |
29 | 27 | from openai._streaming import Stream, AsyncStream |
30 | 28 | from openai._exceptions import OpenAIError, APIStatusError, APITimeoutError, APIResponseValidationError |
31 | 29 | from openai._base_client import ( |
|
36 | 34 | DefaultAsyncHttpxClient, |
37 | 35 | make_request_options, |
38 | 36 | ) |
39 | | -from openai.types.chat.completion_create_params import CompletionCreateParamsNonStreaming |
40 | 37 |
|
41 | 38 | from .utils import update_env |
42 | 39 |
|
@@ -725,60 +722,37 @@ def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str |
725 | 722 |
|
726 | 723 | @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) |
727 | 724 | @pytest.mark.respx(base_url=base_url) |
728 | | - def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) -> None: |
| 725 | + def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter, client: OpenAI) -> None: |
729 | 726 | respx_mock.post("/chat/completions").mock(side_effect=httpx.TimeoutException("Test timeout error")) |
730 | 727 |
|
731 | 728 | with pytest.raises(APITimeoutError): |
732 | | - self.client.post( |
733 | | - "/chat/completions", |
734 | | - body=cast( |
735 | | - object, |
736 | | - maybe_transform( |
737 | | - dict( |
738 | | - messages=[ |
739 | | - { |
740 | | - "role": "user", |
741 | | - "content": "Say this is a test", |
742 | | - } |
743 | | - ], |
744 | | - model="gpt-4o", |
745 | | - ), |
746 | | - CompletionCreateParamsNonStreaming, |
747 | | - ), |
748 | | - ), |
749 | | - cast_to=httpx.Response, |
750 | | - options={"headers": {RAW_RESPONSE_HEADER: "stream"}}, |
751 | | - ) |
| 729 | + client.chat.completions.with_streaming_response.create( |
| 730 | + messages=[ |
| 731 | + { |
| 732 | + "content": "string", |
| 733 | + "role": "developer", |
| 734 | + } |
| 735 | + ], |
| 736 | + model="gpt-4o", |
| 737 | + ).__enter__() |
752 | 738 |
|
753 | 739 | assert _get_open_connections(self.client) == 0 |
754 | 740 |
|
755 | 741 | @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) |
756 | 742 | @pytest.mark.respx(base_url=base_url) |
757 | | - def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> None: |
| 743 | + def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter, client: OpenAI) -> None: |
758 | 744 | respx_mock.post("/chat/completions").mock(return_value=httpx.Response(500)) |
759 | 745 |
|
760 | 746 | with pytest.raises(APIStatusError): |
761 | | - self.client.post( |
762 | | - "/chat/completions", |
763 | | - body=cast( |
764 | | - object, |
765 | | - maybe_transform( |
766 | | - dict( |
767 | | - messages=[ |
768 | | - { |
769 | | - "role": "user", |
770 | | - "content": "Say this is a test", |
771 | | - } |
772 | | - ], |
773 | | - model="gpt-4o", |
774 | | - ), |
775 | | - CompletionCreateParamsNonStreaming, |
776 | | - ), |
777 | | - ), |
778 | | - cast_to=httpx.Response, |
779 | | - options={"headers": {RAW_RESPONSE_HEADER: "stream"}}, |
780 | | - ) |
781 | | - |
| 747 | + client.chat.completions.with_streaming_response.create( |
| 748 | + messages=[ |
| 749 | + { |
| 750 | + "content": "string", |
| 751 | + "role": "developer", |
| 752 | + } |
| 753 | + ], |
| 754 | + model="gpt-4o", |
| 755 | + ).__enter__() |
782 | 756 | assert _get_open_connections(self.client) == 0 |
783 | 757 |
|
784 | 758 | @pytest.mark.parametrize("failures_before_success", [0, 2, 4]) |
@@ -1647,60 +1621,37 @@ async def test_parse_retry_after_header(self, remaining_retries: int, retry_afte |
1647 | 1621 |
|
1648 | 1622 | @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) |
1649 | 1623 | @pytest.mark.respx(base_url=base_url) |
1650 | | - async def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) -> None: |
| 1624 | + async def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter, async_client: AsyncOpenAI) -> None: |
1651 | 1625 | respx_mock.post("/chat/completions").mock(side_effect=httpx.TimeoutException("Test timeout error")) |
1652 | 1626 |
|
1653 | 1627 | with pytest.raises(APITimeoutError): |
1654 | | - await self.client.post( |
1655 | | - "/chat/completions", |
1656 | | - body=cast( |
1657 | | - object, |
1658 | | - maybe_transform( |
1659 | | - dict( |
1660 | | - messages=[ |
1661 | | - { |
1662 | | - "role": "user", |
1663 | | - "content": "Say this is a test", |
1664 | | - } |
1665 | | - ], |
1666 | | - model="gpt-4o", |
1667 | | - ), |
1668 | | - CompletionCreateParamsNonStreaming, |
1669 | | - ), |
1670 | | - ), |
1671 | | - cast_to=httpx.Response, |
1672 | | - options={"headers": {RAW_RESPONSE_HEADER: "stream"}}, |
1673 | | - ) |
| 1628 | + await async_client.chat.completions.with_streaming_response.create( |
| 1629 | + messages=[ |
| 1630 | + { |
| 1631 | + "content": "string", |
| 1632 | + "role": "developer", |
| 1633 | + } |
| 1634 | + ], |
| 1635 | + model="gpt-4o", |
| 1636 | + ).__aenter__() |
1674 | 1637 |
|
1675 | 1638 | assert _get_open_connections(self.client) == 0 |
1676 | 1639 |
|
1677 | 1640 | @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) |
1678 | 1641 | @pytest.mark.respx(base_url=base_url) |
1679 | | - async def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> None: |
| 1642 | + async def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter, async_client: AsyncOpenAI) -> None: |
1680 | 1643 | respx_mock.post("/chat/completions").mock(return_value=httpx.Response(500)) |
1681 | 1644 |
|
1682 | 1645 | with pytest.raises(APIStatusError): |
1683 | | - await self.client.post( |
1684 | | - "/chat/completions", |
1685 | | - body=cast( |
1686 | | - object, |
1687 | | - maybe_transform( |
1688 | | - dict( |
1689 | | - messages=[ |
1690 | | - { |
1691 | | - "role": "user", |
1692 | | - "content": "Say this is a test", |
1693 | | - } |
1694 | | - ], |
1695 | | - model="gpt-4o", |
1696 | | - ), |
1697 | | - CompletionCreateParamsNonStreaming, |
1698 | | - ), |
1699 | | - ), |
1700 | | - cast_to=httpx.Response, |
1701 | | - options={"headers": {RAW_RESPONSE_HEADER: "stream"}}, |
1702 | | - ) |
1703 | | - |
| 1646 | + await async_client.chat.completions.with_streaming_response.create( |
| 1647 | + messages=[ |
| 1648 | + { |
| 1649 | + "content": "string", |
| 1650 | + "role": "developer", |
| 1651 | + } |
| 1652 | + ], |
| 1653 | + model="gpt-4o", |
| 1654 | + ).__aenter__() |
1704 | 1655 | assert _get_open_connections(self.client) == 0 |
1705 | 1656 |
|
1706 | 1657 | @pytest.mark.parametrize("failures_before_success", [0, 2, 4]) |
|
0 commit comments