_parse() — anthropic-sdk-python Function Reference
Architecture documentation for the _parse() function in _response.py from the anthropic-sdk-python codebase.
Entity Profile
Dependency Diagram
graph TD 368da297_37b7_ab67_d177_ee29db8d5086["_parse()"] 6ba97a89_3a56_ffff_f6de_ddab1cb32b30["BaseAPIResponse"] 368da297_37b7_ab67_d177_ee29db8d5086 -->|defined in| 6ba97a89_3a56_ffff_f6de_ddab1cb32b30 dc4c979e_1f25_e27f_cc3e_f1bcdc3b27d0["parse()"] dc4c979e_1f25_e27f_cc3e_f1bcdc3b27d0 -->|calls| 368da297_37b7_ab67_d177_ee29db8d5086 cd4d4a4e_4ea8_4346_3324_a03f797485c7["parse()"] cd4d4a4e_4ea8_4346_3324_a03f797485c7 -->|calls| 368da297_37b7_ab67_d177_ee29db8d5086 d418996b_1a10_4fe8_0fe5_007be2e08653["is_stream_class_type()"] 368da297_37b7_ab67_d177_ee29db8d5086 -->|calls| d418996b_1a10_4fe8_0fe5_007be2e08653 941ce251_c4b2_51eb_76d8_a3a9127c15e5["extract_stream_chunk_type()"] 368da297_37b7_ab67_d177_ee29db8d5086 -->|calls| 941ce251_c4b2_51eb_76d8_a3a9127c15e5 68781e77_0c04_76c0_39da_f86210b5c63c["is_basemodel()"] 368da297_37b7_ab67_d177_ee29db8d5086 -->|calls| 68781e77_0c04_76c0_39da_f86210b5c63c 518c8bd6_474a_7933_381f_46a8c55e9f51["_process_response_data()"] 368da297_37b7_ab67_d177_ee29db8d5086 -->|calls| 518c8bd6_474a_7933_381f_46a8c55e9f51 484b3879_bf87_4601_d355_eb6e18237685["aiter_bytes()"] 368da297_37b7_ab67_d177_ee29db8d5086 -->|calls| 484b3879_bf87_4601_d355_eb6e18237685 afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7["get()"] 368da297_37b7_ab67_d177_ee29db8d5086 -->|calls| afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7 26e6c89b_c171_b129_b73c_7d8763351be3["json()"] 368da297_37b7_ab67_d177_ee29db8d5086 -->|calls| 26e6c89b_c171_b129_b73c_7d8763351be3 27750240_c560_f486_8702_e6672cb66e08["iter_bytes()"] 368da297_37b7_ab67_d177_ee29db8d5086 -->|calls| 27750240_c560_f486_8702_e6672cb66e08 style 368da297_37b7_ab67_d177_ee29db8d5086 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
src/anthropic/_response.py lines 129–295
def _parse(self, *, to: type[_T] | None = None) -> R | _T:
cast_to = to if to is not None else self._cast_to
# unwrap `TypeAlias('Name', T)` -> `T`
if is_type_alias_type(cast_to):
cast_to = cast_to.__value__ # type: ignore[unreachable]
# unwrap `Annotated[T, ...]` -> `T`
if cast_to and is_annotated_type(cast_to):
cast_to = extract_type_arg(cast_to, 0)
origin = get_origin(cast_to) or cast_to
if inspect.isclass(origin):
if issubclass(cast(Any, origin), JSONLDecoder):
return cast(
R,
cast("type[JSONLDecoder[Any]]", cast_to)(
raw_iterator=self.http_response.iter_bytes(chunk_size=64),
line_type=extract_type_arg(cast_to, 0),
http_response=self.http_response,
),
)
if issubclass(cast(Any, origin), AsyncJSONLDecoder):
return cast(
R,
cast("type[AsyncJSONLDecoder[Any]]", cast_to)(
raw_iterator=self.http_response.aiter_bytes(chunk_size=64),
line_type=extract_type_arg(cast_to, 0),
http_response=self.http_response,
),
)
if self._is_sse_stream:
if to:
if not is_stream_class_type(to):
raise TypeError(f"Expected custom parse type to be a subclass of {Stream} or {AsyncStream}")
return cast(
_T,
to(
cast_to=extract_stream_chunk_type(
to,
failure_message="Expected custom stream type to be passed with a type argument, e.g. Stream[ChunkType]",
),
response=self.http_response,
client=cast(Any, self._client),
),
)
if self._stream_cls:
return cast(
R,
self._stream_cls(
cast_to=extract_stream_chunk_type(self._stream_cls),
response=self.http_response,
client=cast(Any, self._client),
),
)
stream_cls = cast("type[Stream[Any]] | type[AsyncStream[Any]] | None", self._client._default_stream_cls)
if stream_cls is None:
raise MissingStreamClassError()
return cast(
R,
stream_cls(
cast_to=cast_to,
response=self.http_response,
client=cast(Any, self._client),
),
)
if cast_to is NoneType:
return cast(R, None)
response = self.http_response
if cast_to == str:
return cast(R, response.text)
Domain
Subdomains
Defined In
Calls
Source
Frequently Asked Questions
What does _parse() do?
_parse() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_response.py.
Where is _parse() defined?
_parse() is defined in src/anthropic/_response.py at line 129.
What does _parse() call?
_parse() calls 8 function(s): _process_response_data, aiter_bytes, extract_stream_chunk_type, get, is_basemodel, is_stream_class_type, iter_bytes, json.
What calls _parse()?
_parse() is called by 2 function(s): parse, parse.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free