Skip to content

Commit 004bc92

Browse files
chore(internal): add core support for deserializing into number response (#1219)
1 parent 4f5ff29 commit 004bc92

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/openai/_legacy_response.py

+8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ class MyModel(BaseModel):
107107
- `list`
108108
- `Union`
109109
- `str`
110+
- `int`
111+
- `float`
110112
- `httpx.Response`
111113
"""
112114
cache_key = to if to is not None else self._cast_to
@@ -220,6 +222,12 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
220222
if cast_to == str:
221223
return cast(R, response.text)
222224

225+
if cast_to == int:
226+
return cast(R, int(response.text))
227+
228+
if cast_to == float:
229+
return cast(R, float(response.text))
230+
223231
origin = get_origin(cast_to) or cast_to
224232

225233
if inspect.isclass(origin) and issubclass(origin, HttpxBinaryResponseContent):

src/openai/_response.py

+8
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
172172
if cast_to == bytes:
173173
return cast(R, response.content)
174174

175+
if cast_to == int:
176+
return cast(R, int(response.text))
177+
178+
if cast_to == float:
179+
return cast(R, float(response.text))
180+
175181
origin = get_origin(cast_to) or cast_to
176182

177183
# handle the legacy binary response case
@@ -277,6 +283,8 @@ class MyModel(BaseModel):
277283
- `list`
278284
- `Union`
279285
- `str`
286+
- `int`
287+
- `float`
280288
- `httpx.Response`
281289
"""
282290
cache_key = to if to is not None else self._cast_to

0 commit comments

Comments
 (0)