Home / Class/ LegacyAPIResponse Class — anthropic-sdk-python Architecture

LegacyAPIResponse Class — anthropic-sdk-python Architecture

Architecture documentation for the LegacyAPIResponse class in _legacy_response.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e["LegacyAPIResponse"]
  17ce5647_6f06_0676_a4a5_e378a3f57cb1["BaseModel"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|extends| 17ce5647_6f06_0676_a4a5_e378a3f57cb1
  db3528f4_d56c_fc6e_955a_06508f166b4e["JSONLDecoder"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|extends| db3528f4_d56c_fc6e_955a_06508f166b4e
  ae5d86ac_c756_fcd6_d559_608e6815f980["AsyncJSONLDecoder"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|extends| ae5d86ac_c756_fcd6_d559_608e6815f980
  707108e1_1591_e5c4_7fa0_ca26831eab86["HttpxBinaryResponseContent"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|extends| 707108e1_1591_e5c4_7fa0_ca26831eab86
  f3a9224d_bdab_aa4b_9f5e_877302fb10bc["_legacy_response.py"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|defined in| f3a9224d_bdab_aa4b_9f5e_877302fb10bc
  a8f28ffe_b74e_e220_a9a6_631ed281e783["__init__()"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|method| a8f28ffe_b74e_e220_a9a6_631ed281e783
  28d80341_f2f4_817e_1f96_b5e255bc31bd["request_id()"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|method| 28d80341_f2f4_817e_1f96_b5e255bc31bd
  aad466e0_4b2d_aeb2_205c_df6bbaeef008["parse()"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|method| aad466e0_4b2d_aeb2_205c_df6bbaeef008
  6fd99434_5f90_bc07_dee7_6f6037871d84["headers()"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|method| 6fd99434_5f90_bc07_dee7_6f6037871d84
  18e08a4f_cf8f_3db6_09c7_b4bee4aa3c2a["http_request()"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|method| 18e08a4f_cf8f_3db6_09c7_b4bee4aa3c2a
  2426f101_82a1_4add_0116_fa496f52131d["status_code()"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|method| 2426f101_82a1_4add_0116_fa496f52131d
  d7ce8439_eb6e_04cd_d4de_cd2ba2a282be["url()"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|method| d7ce8439_eb6e_04cd_d4de_cd2ba2a282be
  2b8c6ddd_c46b_dd75_cade_197a156f282e["method()"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|method| 2b8c6ddd_c46b_dd75_cade_197a156f282e
  aee0f27b_7a02_5c43_a369_3308bce3187f["content()"]
  0eaa35b2_72d4_9c3a_4c4e_21c6bfcb945e -->|method| aee0f27b_7a02_5c43_a369_3308bce3187f

Relationship Graph

Source Code

src/anthropic/_legacy_response.py lines 47–365

class LegacyAPIResponse(Generic[R]):
    """This is a legacy class as it will be replaced by `APIResponse`
    and `AsyncAPIResponse` in the `_response.py` file in the next major
    release.

    For the sync client this will mostly be the same with the exception
    of `content` & `text` will be methods instead of properties. In the
    async client, all methods will be async.

    A migration script will be provided & the migration in general should
    be smooth.
    """

    _cast_to: type[R]
    _client: BaseClient[Any, Any]
    _parsed_by_type: dict[type[Any], Any]
    _stream: bool
    _stream_cls: type[Stream[Any]] | type[AsyncStream[Any]] | None
    _options: FinalRequestOptions

    http_response: httpx.Response

    retries_taken: int
    """The number of retries made. If no retries happened this will be `0`"""

    def __init__(
        self,
        *,
        raw: httpx.Response,
        cast_to: type[R],
        client: BaseClient[Any, Any],
        stream: bool,
        stream_cls: type[Stream[Any]] | type[AsyncStream[Any]] | None,
        options: FinalRequestOptions,
        retries_taken: int = 0,
    ) -> None:
        self._cast_to = cast_to
        self._client = client
        self._parsed_by_type = {}
        self._stream = stream
        self._stream_cls = stream_cls
        self._options = options
        self.http_response = raw
        self.retries_taken = retries_taken

    @property
    def request_id(self) -> str | None:
        return self.http_response.headers.get("request-id")  # type: ignore[no-any-return]

    @overload
    def parse(self, *, to: type[_T]) -> _T: ...

    @overload
    def parse(self) -> R: ...

    def parse(self, *, to: type[_T] | None = None) -> R | _T:
        """Returns the rich python representation of this response's data.

        NOTE: For the async client: this will become a coroutine in the next major version.

        For lower-level control, see `.read()`, `.json()`, `.iter_bytes()`.

        You can customise the type that the response is parsed into through
        the `to` argument, e.g.

        ```py
        from anthropic import BaseModel


        class MyModel(BaseModel):
            foo: str


        obj = response.parse(to=MyModel)
        print(obj.foo)
        ```

        We support parsing:
          - `BaseModel`
          - `dict`
          - `list`

Frequently Asked Questions

What is the LegacyAPIResponse class?
LegacyAPIResponse is a class in the anthropic-sdk-python codebase, defined in src/anthropic/_legacy_response.py.
Where is LegacyAPIResponse defined?
LegacyAPIResponse is defined in src/anthropic/_legacy_response.py at line 47.
What does LegacyAPIResponse extend?
LegacyAPIResponse extends BaseModel, JSONLDecoder, AsyncJSONLDecoder, HttpxBinaryResponseContent.

Analyze Your Own Codebase

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

Try Supermodel Free