Home / Function/ _parse() — anthropic-sdk-python Function Reference

_parse() — anthropic-sdk-python Function Reference

Architecture documentation for the _parse() function in _legacy_response.py from the anthropic-sdk-python codebase.

Function python AnthropicClient AsyncAPI calls 8 called by 1

Entity Profile

Dependency Diagram

graph TD
  58686c44_d8b1_cf13_381b_6cff1300f441["_parse()"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e["LegacyAPIResponse"]
  58686c44_d8b1_cf13_381b_6cff1300f441 -->|defined in| 0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e
  aad466e0_4b2d_aeb2_205c_df6bbaeef008["parse()"]
  aad466e0_4b2d_aeb2_205c_df6bbaeef008 -->|calls| 58686c44_d8b1_cf13_381b_6cff1300f441
  45fe0d69_db3d_a57c_6c23_db57ac73d759["iter_bytes()"]
  58686c44_d8b1_cf13_381b_6cff1300f441 -->|calls| 45fe0d69_db3d_a57c_6c23_db57ac73d759
  7e30c80b_9b40_d863_b9d5_a0e681fe7e3a["aiter_bytes()"]
  58686c44_d8b1_cf13_381b_6cff1300f441 -->|calls| 7e30c80b_9b40_d863_b9d5_a0e681fe7e3a
  d418996b_1a10_4fe8_0fe5_007be2e08653["is_stream_class_type()"]
  58686c44_d8b1_cf13_381b_6cff1300f441 -->|calls| d418996b_1a10_4fe8_0fe5_007be2e08653
  941ce251_c4b2_51eb_76d8_a3a9127c15e5["extract_stream_chunk_type()"]
  58686c44_d8b1_cf13_381b_6cff1300f441 -->|calls| 941ce251_c4b2_51eb_76d8_a3a9127c15e5
  68781e77_0c04_76c0_39da_f86210b5c63c["is_basemodel()"]
  58686c44_d8b1_cf13_381b_6cff1300f441 -->|calls| 68781e77_0c04_76c0_39da_f86210b5c63c
  6f77bac1_8784_c84a_9d98_b667e36cf350["json()"]
  58686c44_d8b1_cf13_381b_6cff1300f441 -->|calls| 6f77bac1_8784_c84a_9d98_b667e36cf350
  518c8bd6_474a_7933_381f_46a8c55e9f51["_process_response_data()"]
  58686c44_d8b1_cf13_381b_6cff1300f441 -->|calls| 518c8bd6_474a_7933_381f_46a8c55e9f51
  afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7["get()"]
  58686c44_d8b1_cf13_381b_6cff1300f441 -->|calls| afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7
  style 58686c44_d8b1_cf13_381b_6cff1300f441 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/anthropic/_legacy_response.py lines 199–361

    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._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)

Subdomains

Called By

Frequently Asked Questions

What does _parse() do?
_parse() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_legacy_response.py.
Where is _parse() defined?
_parse() is defined in src/anthropic/_legacy_response.py at line 199.
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 1 function(s): parse.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free