Skip to content

Commit

Permalink
chore(internal): add core support for deserializing into number respo…
Browse files Browse the repository at this point in the history
…nse (#1219)
  • Loading branch information
stainless-bot committed Mar 6, 2024
1 parent 4f5ff29 commit 004bc92
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/openai/_legacy_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class MyModel(BaseModel):
- `list`
- `Union`
- `str`
- `int`
- `float`
- `httpx.Response`
"""
cache_key = to if to is not None else self._cast_to
Expand Down Expand Up @@ -220,6 +222,12 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
if cast_to == str:
return cast(R, response.text)

if cast_to == int:
return cast(R, int(response.text))

if cast_to == float:
return cast(R, float(response.text))

origin = get_origin(cast_to) or cast_to

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

if cast_to == int:
return cast(R, int(response.text))

if cast_to == float:
return cast(R, float(response.text))

origin = get_origin(cast_to) or cast_to

# handle the legacy binary response case
Expand Down Expand Up @@ -277,6 +283,8 @@ class MyModel(BaseModel):
- `list`
- `Union`
- `str`
- `int`
- `float`
- `httpx.Response`
"""
cache_key = to if to is not None else self._cast_to
Expand Down

0 comments on commit 004bc92

Please sign in to comment.