Skip to content

chore(internal): add support for parsing bool response content #1774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/openai/_legacy_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
if cast_to == float:
return cast(R, float(response.text))

if cast_to == bool:
return cast(R, response.text.lower() == "true")

origin = get_origin(cast_to) or cast_to

if inspect.isclass(origin) and issubclass(origin, HttpxBinaryResponseContent):
Expand Down
3 changes: 3 additions & 0 deletions src/openai/_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
if cast_to == float:
return cast(R, float(response.text))

if cast_to == bool:
return cast(R, response.text.lower() == "true")

origin = get_origin(cast_to) or cast_to

# handle the legacy binary response case
Expand Down
25 changes: 25 additions & 0 deletions tests/test_legacy_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ def test_response_parse_mismatched_basemodel(client: OpenAI) -> None:
response.parse(to=PydanticModel)


@pytest.mark.parametrize(
"content, expected",
[
("false", False),
("true", True),
("False", False),
("True", True),
("TrUe", True),
("FalSe", False),
],
)
def test_response_parse_bool(client: OpenAI, content: str, expected: bool) -> None:
response = LegacyAPIResponse(
raw=httpx.Response(200, content=content),
client=client,
stream=False,
stream_cls=None,
cast_to=str,
options=FinalRequestOptions.construct(method="get", url="/foo"),
)

result = response.parse(to=bool)
assert result is expected


def test_response_parse_custom_stream(client: OpenAI) -> None:
response = LegacyAPIResponse(
raw=httpx.Response(200, content=b"foo"),
Expand Down
50 changes: 50 additions & 0 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,56 @@ async def test_async_response_parse_annotated_type(async_client: AsyncOpenAI) ->
assert obj.bar == 2


@pytest.mark.parametrize(
"content, expected",
[
("false", False),
("true", True),
("False", False),
("True", True),
("TrUe", True),
("FalSe", False),
],
)
def test_response_parse_bool(client: OpenAI, content: str, expected: bool) -> None:
response = APIResponse(
raw=httpx.Response(200, content=content),
client=client,
stream=False,
stream_cls=None,
cast_to=str,
options=FinalRequestOptions.construct(method="get", url="/foo"),
)

result = response.parse(to=bool)
assert result is expected


@pytest.mark.parametrize(
"content, expected",
[
("false", False),
("true", True),
("False", False),
("True", True),
("TrUe", True),
("FalSe", False),
],
)
async def test_async_response_parse_bool(client: AsyncOpenAI, content: str, expected: bool) -> None:
response = AsyncAPIResponse(
raw=httpx.Response(200, content=content),
client=client,
stream=False,
stream_cls=None,
cast_to=str,
options=FinalRequestOptions.construct(method="get", url="/foo"),
)

result = await response.parse(to=bool)
assert result is expected


class OtherModel(BaseModel):
a: str

Expand Down