openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/test_legacy_response.py
128lines · modecode
| 1 | import json |
| 2 | from typing import Any, Union, cast |
| 3 | from typing_extensions import Annotated |
| 4 | |
| 5 | import httpx |
| 6 | import pytest |
| 7 | import pydantic |
| 8 | |
| 9 | from openai import OpenAI, BaseModel |
| 10 | from openai._streaming import Stream |
| 11 | from openai._base_client import FinalRequestOptions |
| 12 | from openai._legacy_response import LegacyAPIResponse |
| 13 | |
| 14 | |
| 15 | class PydanticModel(pydantic.BaseModel): ... |
| 16 | |
| 17 | |
| 18 | def test_response_parse_mismatched_basemodel(client: OpenAI) -> None: |
| 19 | response = LegacyAPIResponse( |
| 20 | raw=httpx.Response(200, content=b"foo"), |
| 21 | client=client, |
| 22 | stream=False, |
| 23 | stream_cls=None, |
| 24 | cast_to=str, |
| 25 | options=FinalRequestOptions.construct(method="get", url="/foo"), |
| 26 | ) |
| 27 | |
| 28 | with pytest.raises( |
| 29 | TypeError, |
| 30 | match="Pydantic models must subclass our base model type, e.g. `from openai import BaseModel`", |
| 31 | ): |
| 32 | response.parse(to=PydanticModel) |
| 33 | |
| 34 | |
| 35 | @pytest.mark.parametrize( |
| 36 | "content, expected", |
| 37 | [ |
| 38 | ("false", False), |
| 39 | ("true", True), |
| 40 | ("False", False), |
| 41 | ("True", True), |
| 42 | ("TrUe", True), |
| 43 | ("FalSe", False), |
| 44 | ], |
| 45 | ) |
| 46 | def test_response_parse_bool(client: OpenAI, content: str, expected: bool) -> None: |
| 47 | response = LegacyAPIResponse( |
| 48 | raw=httpx.Response(200, content=content), |
| 49 | client=client, |
| 50 | stream=False, |
| 51 | stream_cls=None, |
| 52 | cast_to=str, |
| 53 | options=FinalRequestOptions.construct(method="get", url="/foo"), |
| 54 | ) |
| 55 | |
| 56 | result = response.parse(to=bool) |
| 57 | assert result is expected |
| 58 | |
| 59 | |
| 60 | def test_response_parse_custom_stream(client: OpenAI) -> None: |
| 61 | response = LegacyAPIResponse( |
| 62 | raw=httpx.Response(200, content=b"foo"), |
| 63 | client=client, |
| 64 | stream=True, |
| 65 | stream_cls=None, |
| 66 | cast_to=str, |
| 67 | options=FinalRequestOptions.construct(method="get", url="/foo"), |
| 68 | ) |
| 69 | |
| 70 | stream = response.parse(to=Stream[int]) |
| 71 | assert stream._cast_to == int |
| 72 | |
| 73 | |
| 74 | class CustomModel(BaseModel): |
| 75 | foo: str |
| 76 | bar: int |
| 77 | |
| 78 | |
| 79 | def test_response_parse_custom_model(client: OpenAI) -> None: |
| 80 | response = LegacyAPIResponse( |
| 81 | raw=httpx.Response(200, content=json.dumps({"foo": "hello!", "bar": 2})), |
| 82 | client=client, |
| 83 | stream=False, |
| 84 | stream_cls=None, |
| 85 | cast_to=str, |
| 86 | options=FinalRequestOptions.construct(method="get", url="/foo"), |
| 87 | ) |
| 88 | |
| 89 | obj = response.parse(to=CustomModel) |
| 90 | assert obj.foo == "hello!" |
| 91 | assert obj.bar == 2 |
| 92 | |
| 93 | |
| 94 | def test_response_parse_annotated_type(client: OpenAI) -> None: |
| 95 | response = LegacyAPIResponse( |
| 96 | raw=httpx.Response(200, content=json.dumps({"foo": "hello!", "bar": 2})), |
| 97 | client=client, |
| 98 | stream=False, |
| 99 | stream_cls=None, |
| 100 | cast_to=str, |
| 101 | options=FinalRequestOptions.construct(method="get", url="/foo"), |
| 102 | ) |
| 103 | |
| 104 | obj = response.parse( |
| 105 | to=cast("type[CustomModel]", Annotated[CustomModel, "random metadata"]), |
| 106 | ) |
| 107 | assert obj.foo == "hello!" |
| 108 | assert obj.bar == 2 |
| 109 | |
| 110 | |
| 111 | class OtherModel(pydantic.BaseModel): |
| 112 | a: str |
| 113 | |
| 114 | |
| 115 | @pytest.mark.parametrize("client", [False], indirect=True) # loose validation |
| 116 | def test_response_parse_expect_model_union_non_json_content(client: OpenAI) -> None: |
| 117 | response = LegacyAPIResponse( |
| 118 | raw=httpx.Response(200, content=b"foo", headers={"Content-Type": "application/text"}), |
| 119 | client=client, |
| 120 | stream=False, |
| 121 | stream_cls=None, |
| 122 | cast_to=str, |
| 123 | options=FinalRequestOptions.construct(method="get", url="/foo"), |
| 124 | ) |
| 125 | |
| 126 | obj = response.parse(to=cast(Any, Union[CustomModel, OtherModel])) |
| 127 | assert isinstance(obj, str) |
| 128 | assert obj == "foo" |
| 129 | |