2
2
3
3
import os
4
4
from typing import Any , Union , Mapping , TypeVar
5
- from typing_extensions import override , get_origin
5
+ from typing_extensions import override
6
6
7
7
import httpx
8
8
9
9
from ... import _exceptions
10
- from ._stream import BedrockStream , AsyncBedrockStream
11
- from ..._types import NOT_GIVEN , NotGiven , ResponseT
10
+ from ..._types import NOT_GIVEN , NotGiven
12
11
from ..._utils import is_dict
13
12
from ..._version import __version__
14
- from ..._response import extract_stream_chunk_type
15
13
from ..._streaming import Stream , AsyncStream
16
14
from ..._exceptions import APIStatusError
17
15
from ..._base_client import DEFAULT_MAX_RETRIES , BaseClient , SyncAPIClient , AsyncAPIClient , FinalRequestOptions
16
+ from ._stream_decoder import AWSEventStreamDecoder
18
17
from ...resources .completions import Completions , AsyncCompletions
19
18
20
19
DEFAULT_VERSION = "bedrock-2023-05-31"
@@ -131,10 +130,12 @@ def __init__(
131
130
_strict_response_validation = _strict_response_validation ,
132
131
)
133
132
134
- self ._default_stream_cls = BedrockStream
135
-
136
133
self .completions = Completions (self )
137
134
135
+ @override
136
+ def _make_sse_decoder (self ) -> AWSEventStreamDecoder :
137
+ return AWSEventStreamDecoder ()
138
+
138
139
@override
139
140
def _prepare_request (self , request : httpx .Request ) -> None :
140
141
from ._auth import get_auth_headers
@@ -153,31 +154,6 @@ def _prepare_request(self, request: httpx.Request) -> None:
153
154
)
154
155
request .headers .update (headers )
155
156
156
- @override
157
- def _process_response (
158
- self ,
159
- * ,
160
- cast_to : type [ResponseT ],
161
- options : FinalRequestOptions ,
162
- response : httpx .Response ,
163
- stream : bool ,
164
- stream_cls : type [Stream [Any ]] | type [AsyncStream [Any ]] | None ,
165
- ) -> ResponseT :
166
- if stream_cls is not None and get_origin (stream_cls ) == Stream :
167
- chunk_type = extract_stream_chunk_type (stream_cls )
168
-
169
- # the type: ignore is required as mypy doesn't like us
170
- # dynamically created a concrete type like this
171
- stream_cls = BedrockStream [chunk_type ] # type: ignore
172
-
173
- return super ()._process_response (
174
- cast_to = cast_to ,
175
- options = options ,
176
- response = response ,
177
- stream = stream ,
178
- stream_cls = stream_cls ,
179
- )
180
-
181
157
182
158
class AsyncAnthropicBedrock (BaseBedrockClient [httpx .AsyncClient , AsyncStream [Any ]], AsyncAPIClient ):
183
159
completions : AsyncCompletions
@@ -231,10 +207,12 @@ def __init__(
231
207
_strict_response_validation = _strict_response_validation ,
232
208
)
233
209
234
- self ._default_stream_cls = AsyncBedrockStream
235
-
236
210
self .completions = AsyncCompletions (self )
237
211
212
+ @override
213
+ def _make_sse_decoder (self ) -> AWSEventStreamDecoder :
214
+ return AWSEventStreamDecoder ()
215
+
238
216
@override
239
217
async def _prepare_request (self , request : httpx .Request ) -> None :
240
218
from ._auth import get_auth_headers
@@ -252,28 +230,3 @@ async def _prepare_request(self, request: httpx.Request) -> None:
252
230
data = data ,
253
231
)
254
232
request .headers .update (headers )
255
-
256
- @override
257
- async def _process_response (
258
- self ,
259
- * ,
260
- cast_to : type [ResponseT ],
261
- options : FinalRequestOptions ,
262
- response : httpx .Response ,
263
- stream : bool ,
264
- stream_cls : type [Stream [Any ]] | type [AsyncStream [Any ]] | None ,
265
- ) -> ResponseT :
266
- if stream_cls is not None and get_origin (stream_cls ) == AsyncStream :
267
- chunk_type = extract_stream_chunk_type (stream_cls )
268
-
269
- # the type: ignore is required as mypy doesn't like us
270
- # dynamically created a concrete type like this
271
- stream_cls = AsyncBedrockStream [chunk_type ] # type: ignore
272
-
273
- return await super ()._process_response (
274
- cast_to = cast_to ,
275
- options = options ,
276
- response = response ,
277
- stream = stream ,
278
- stream_cls = stream_cls ,
279
- )
0 commit comments