Skip to content

Commit

Permalink
chore(internal): add support for parsing bool response content (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Oct 4, 2024
1 parent 20f5224 commit bf315ab
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/anthropic/_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/anthropic/_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: Anthropic) -> 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: Anthropic, 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: Anthropic) -> 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: AsyncAnthropic)
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: Anthropic, 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: AsyncAnthropic, 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

0 comments on commit bf315ab

Please sign in to comment.