MCPcopy
hub / github.com/anthropics/anthropic-sdk-python / _parse

Method _parse

src/anthropic/_legacy_response.py:199–364  ·  view source on GitHub ↗
(self, *, to: type[_T] | None = None)

Source from the content-addressed store, hash-verified

197 return self.http_response.elapsed
198
199 def _parse(self, *, to: type[_T] | None = None) -> R | _T:
200 cast_to = to if to is not None else self._cast_to
201
202 # unwrap `TypeAlias('Name', T)` -> `T`
203 if is_type_alias_type(cast_to):
204 cast_to = cast_to.__value__ # type: ignore[unreachable]
205
206 # unwrap `Annotated[T, ...]` -> `T`
207 if cast_to and is_annotated_type(cast_to):
208 cast_to = extract_type_arg(cast_to, 0)
209
210 origin = get_origin(cast_to) or cast_to
211
212 if inspect.isclass(origin):
213 if issubclass(cast(Any, origin), JSONLDecoder):
214 return cast(
215 R,
216 cast("type[JSONLDecoder[Any]]", cast_to)(
217 raw_iterator=self.http_response.iter_bytes(chunk_size=64),
218 line_type=extract_type_arg(cast_to, 0),
219 http_response=self.http_response,
220 ),
221 )
222
223 if issubclass(cast(Any, origin), AsyncJSONLDecoder):
224 return cast(
225 R,
226 cast("type[AsyncJSONLDecoder[Any]]", cast_to)(
227 raw_iterator=self.http_response.aiter_bytes(chunk_size=64),
228 line_type=extract_type_arg(cast_to, 0),
229 http_response=self.http_response,
230 ),
231 )
232
233 if self._stream:
234 if to:
235 if not is_stream_class_type(to):
236 raise TypeError(f"Expected custom parse type to be a subclass of {Stream} or {AsyncStream}")
237
238 return cast(
239 _T,
240 to(
241 cast_to=extract_stream_chunk_type(
242 to,
243 failure_message="Expected custom stream type to be passed with a type argument, e.g. Stream[ChunkType]",
244 ),
245 response=self.http_response,
246 client=cast(Any, self._client),
247 options=self._options,
248 ),
249 )
250
251 if self._stream_cls:
252 return cast(
253 R,
254 self._stream_cls(
255 cast_to=extract_stream_chunk_type(self._stream_cls),
256 response=self.http_response,

Callers 1

parseMethod · 0.95

Calls 14

is_type_alias_typeFunction · 0.85
is_annotated_typeFunction · 0.85
extract_type_argFunction · 0.85
is_stream_class_typeFunction · 0.85
is_basemodelFunction · 0.85
get_originFunction · 0.70
iter_bytesMethod · 0.45
aiter_bytesMethod · 0.45

Tested by

no test coverage detected