|
1 | 1 | import json
|
2 |
| -from typing import List, cast |
| 2 | +from typing import Any, List, Union, cast |
3 | 3 | from typing_extensions import Annotated
|
4 | 4 |
|
5 | 5 | import httpx
|
@@ -188,3 +188,40 @@ async def test_async_response_parse_annotated_type(async_client: AsyncOpenAI) ->
|
188 | 188 | )
|
189 | 189 | assert obj.foo == "hello!"
|
190 | 190 | assert obj.bar == 2
|
| 191 | + |
| 192 | + |
| 193 | +class OtherModel(BaseModel): |
| 194 | + a: str |
| 195 | + |
| 196 | + |
| 197 | +@pytest.mark.parametrize("client", [False], indirect=True) # loose validation |
| 198 | +def test_response_parse_expect_model_union_non_json_content(client: OpenAI) -> None: |
| 199 | + response = APIResponse( |
| 200 | + raw=httpx.Response(200, content=b"foo", headers={"Content-Type": "application/text"}), |
| 201 | + client=client, |
| 202 | + stream=False, |
| 203 | + stream_cls=None, |
| 204 | + cast_to=str, |
| 205 | + options=FinalRequestOptions.construct(method="get", url="/foo"), |
| 206 | + ) |
| 207 | + |
| 208 | + obj = response.parse(to=cast(Any, Union[CustomModel, OtherModel])) |
| 209 | + assert isinstance(obj, str) |
| 210 | + assert obj == "foo" |
| 211 | + |
| 212 | + |
| 213 | +@pytest.mark.asyncio |
| 214 | +@pytest.mark.parametrize("async_client", [False], indirect=True) # loose validation |
| 215 | +async def test_async_response_parse_expect_model_union_non_json_content(async_client: AsyncOpenAI) -> None: |
| 216 | + response = AsyncAPIResponse( |
| 217 | + raw=httpx.Response(200, content=b"foo", headers={"Content-Type": "application/text"}), |
| 218 | + client=async_client, |
| 219 | + stream=False, |
| 220 | + stream_cls=None, |
| 221 | + cast_to=str, |
| 222 | + options=FinalRequestOptions.construct(method="get", url="/foo"), |
| 223 | + ) |
| 224 | + |
| 225 | + obj = await response.parse(to=cast(Any, Union[CustomModel, OtherModel])) |
| 226 | + assert isinstance(obj, str) |
| 227 | + assert obj == "foo" |
0 commit comments