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

SyncAPIClient Class — anthropic-sdk-python Architecture

Architecture documentation for the SyncAPIClient class in _base_client.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  4b46ed95_ff71_ea5d_7534_6929dc929bdb["SyncAPIClient"]
  6ba97a89_3a56_ffff_f6de_ddab1cb32b30["BaseAPIResponse"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|extends| 6ba97a89_3a56_ffff_f6de_ddab1cb32b30
  c5dafd2e_e591_b4ff_d0c8_67a1204aef83["APIResponse"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|extends| c5dafd2e_e591_b4ff_d0c8_67a1204aef83
  31e60ad8_cac8_652d_176d_4f7cf7dda1ad["_base_client.py"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|defined in| 31e60ad8_cac8_652d_176d_4f7cf7dda1ad
  f44332d5_f28c_4563_7a62_731e0bbb1a07["__init__()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| f44332d5_f28c_4563_7a62_731e0bbb1a07
  ed713948_18fe_fd07_104a_917d41572166["is_closed()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| ed713948_18fe_fd07_104a_917d41572166
  68646c0e_7a37_2971_5977_d7bb4119303e["close()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| 68646c0e_7a37_2971_5977_d7bb4119303e
  ebe99a3d_dbe2_9ed2_91d2_fee6700ca656["__enter__()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| ebe99a3d_dbe2_9ed2_91d2_fee6700ca656
  1127f58a_e7fd_1e8a_59e4_cf1d6031ac4c["__exit__()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| 1127f58a_e7fd_1e8a_59e4_cf1d6031ac4c
  4aaf191e_6810_17fb_b7df_2b112a04a128["_prepare_options()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| 4aaf191e_6810_17fb_b7df_2b112a04a128
  9607d5e9_e7be_80cf_3ff2_7610857de35d["_prepare_request()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| 9607d5e9_e7be_80cf_3ff2_7610857de35d
  2cb1e2bf_1a02_d5ea_42a9_c0def162363a["request()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| 2cb1e2bf_1a02_d5ea_42a9_c0def162363a
  e28246be_5825_36c7_9258_c1395a05766e["_sleep_for_retry()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| e28246be_5825_36c7_9258_c1395a05766e
  68dab0d8_6666_69f9_2947_acd899897b96["_process_response()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| 68dab0d8_6666_69f9_2947_acd899897b96
  fd93a81d_e05c_b682_aed9_45b0ae941ae3["_request_api_list()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb -->|method| fd93a81d_e05c_b682_aed9_45b0ae941ae3

Relationship Graph

Source Code

src/anthropic/_base_client.py lines 910–1450

class SyncAPIClient(BaseClient[httpx.Client, Stream[Any]]):
    _client: httpx.Client
    _default_stream_cls: type[Stream[Any]] | None = None

    def __init__(
        self,
        *,
        version: str,
        base_url: str | URL,
        max_retries: int = DEFAULT_MAX_RETRIES,
        timeout: float | Timeout | None | NotGiven = not_given,
        http_client: httpx.Client | None = None,
        custom_headers: Mapping[str, str] | None = None,
        custom_query: Mapping[str, object] | None = None,
        _strict_response_validation: bool,
    ) -> None:
        if not is_given(timeout):
            # if the user passed in a custom http client with a non-default
            # timeout set then we use that timeout.
            #
            # note: there is an edge case here where the user passes in a client
            # where they've explicitly set the timeout to match the default timeout
            # as this check is structural, meaning that we'll think they didn't
            # pass in a timeout and will ignore it
            if http_client and http_client.timeout != HTTPX_DEFAULT_TIMEOUT:
                timeout = http_client.timeout
            else:
                timeout = DEFAULT_TIMEOUT

        if http_client is not None and not isinstance(http_client, httpx.Client):  # pyright: ignore[reportUnnecessaryIsInstance]
            raise TypeError(
                f"Invalid `http_client` argument; Expected an instance of `httpx.Client` but got {type(http_client)}"
            )

        super().__init__(
            version=version,
            # cast to a valid type because mypy doesn't understand our type narrowing
            timeout=cast(Timeout, timeout),
            base_url=base_url,
            max_retries=max_retries,
            custom_query=custom_query,
            custom_headers=custom_headers,
            _strict_response_validation=_strict_response_validation,
        )
        self._client = http_client or SyncHttpxClientWrapper(
            base_url=base_url,
            # cast to a valid type because mypy doesn't understand our type narrowing
            timeout=cast(Timeout, timeout),
        )

    def is_closed(self) -> bool:
        return self._client.is_closed

    def close(self) -> None:
        """Close the underlying HTTPX client.

        The client will *not* be usable after this.
        """
        # If an error is thrown while constructing a client, self._client
        # may not be present
        if hasattr(self, "_client"):
            self._client.close()

    def __enter__(self: _T) -> _T:
        return self

    def __exit__(
        self,
        exc_type: type[BaseException] | None,
        exc: BaseException | None,
        exc_tb: TracebackType | None,
    ) -> None:
        self.close()

    def _prepare_options(
        self,
        options: FinalRequestOptions,  # noqa: ARG002
    ) -> FinalRequestOptions:
        """Hook for mutating the given options"""
        return options

Frequently Asked Questions

What is the SyncAPIClient class?
SyncAPIClient is a class in the anthropic-sdk-python codebase, defined in src/anthropic/_base_client.py.
Where is SyncAPIClient defined?
SyncAPIClient is defined in src/anthropic/_base_client.py at line 910.
What does SyncAPIClient extend?
SyncAPIClient extends BaseAPIResponse, APIResponse.

Analyze Your Own Codebase

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

Try Supermodel Free