From e18c24bdaa814adb3db32c9087ea31d727f36bea Mon Sep 17 00:00:00 2001 From: Stainless Bot <107565488+stainless-bot@users.noreply.github.com> Date: Wed, 6 Mar 2024 22:02:09 +0100 Subject: [PATCH] chore(internal): add core support for deserializing into number response (#373) --- src/anthropic/_legacy_response.py | 8 ++++++++ src/anthropic/_response.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/anthropic/_legacy_response.py b/src/anthropic/_legacy_response.py index 02ce2b0f..910e975b 100644 --- a/src/anthropic/_legacy_response.py +++ b/src/anthropic/_legacy_response.py @@ -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 @@ -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): diff --git a/src/anthropic/_response.py b/src/anthropic/_response.py index 0a0702a7..4abc0d49 100644 --- a/src/anthropic/_response.py +++ b/src/anthropic/_response.py @@ -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 @@ -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