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

_process_response() — anthropic-sdk-python Function Reference

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

Function python AnthropicClient SyncAPI calls 4 called by 2

Entity Profile

Dependency Diagram

graph TD
  68dab0d8_6666_69f9_2947_acd899897b96["_process_response()"]
  4b46ed95_ff71_ea5d_7534_6929dc929bdb["SyncAPIClient"]
  68dab0d8_6666_69f9_2947_acd899897b96 -->|defined in| 4b46ed95_ff71_ea5d_7534_6929dc929bdb
  2cb1e2bf_1a02_d5ea_42a9_c0def162363a["request()"]
  2cb1e2bf_1a02_d5ea_42a9_c0def162363a -->|calls| 68dab0d8_6666_69f9_2947_acd899897b96
  ae4395b9_e9e7_d7f2_e944_36fea3691100["_process_response()"]
  ae4395b9_e9e7_d7f2_e944_36fea3691100 -->|calls| 68dab0d8_6666_69f9_2947_acd899897b96
  ae4395b9_e9e7_d7f2_e944_36fea3691100["_process_response()"]
  68dab0d8_6666_69f9_2947_acd899897b96 -->|calls| ae4395b9_e9e7_d7f2_e944_36fea3691100
  02577dbd_844f_c3ef_09b1_c927dbc9a604["extract_response_type()"]
  68dab0d8_6666_69f9_2947_acd899897b96 -->|calls| 02577dbd_844f_c3ef_09b1_c927dbc9a604
  afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7["get()"]
  68dab0d8_6666_69f9_2947_acd899897b96 -->|calls| afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7
  dc4c979e_1f25_e27f_cc3e_f1bcdc3b27d0["parse()"]
  68dab0d8_6666_69f9_2947_acd899897b96 -->|calls| dc4c979e_1f25_e27f_cc3e_f1bcdc3b27d0
  style 68dab0d8_6666_69f9_2947_acd899897b96 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/anthropic/_base_client.py lines 1165–1231

    def _process_response(
        self,
        *,
        cast_to: Type[ResponseT],
        options: FinalRequestOptions,
        response: httpx.Response,
        stream: bool,
        stream_cls: type[Stream[Any]] | type[AsyncStream[Any]] | None,
        retries_taken: int = 0,
    ) -> ResponseT:
        if response.request.headers.get(RAW_RESPONSE_HEADER) == "true":
            return cast(
                ResponseT,
                LegacyAPIResponse(
                    raw=response,
                    client=self,
                    cast_to=cast_to,
                    stream=stream,
                    stream_cls=stream_cls,
                    options=options,
                    retries_taken=retries_taken,
                ),
            )

        origin = get_origin(cast_to) or cast_to

        if (
            inspect.isclass(origin)
            and issubclass(origin, BaseAPIResponse)
            # we only want to actually return the custom BaseAPIResponse class if we're
            # returning the raw response, or if we're not streaming SSE, as if we're streaming
            # SSE then `cast_to` doesn't actively reflect the type we need to parse into
            and (not stream or bool(response.request.headers.get(RAW_RESPONSE_HEADER)))
        ):
            if not issubclass(origin, APIResponse):
                raise TypeError(f"API Response types must subclass {APIResponse}; Received {origin}")

            response_cls = cast("type[BaseAPIResponse[Any]]", cast_to)
            return cast(
                ResponseT,
                response_cls(
                    raw=response,
                    client=self,
                    cast_to=extract_response_type(response_cls),
                    stream=stream,
                    stream_cls=stream_cls,
                    options=options,
                    retries_taken=retries_taken,
                ),
            )

        if cast_to == httpx.Response:
            return cast(ResponseT, response)

        api_response = APIResponse(
            raw=response,
            client=self,
            cast_to=cast("type[ResponseT]", cast_to),  # pyright: ignore[reportUnnecessaryCast]
            stream=stream,
            stream_cls=stream_cls,
            options=options,
            retries_taken=retries_taken,
        )
        if bool(response.request.headers.get(RAW_RESPONSE_HEADER)):
            return cast(ResponseT, api_response)

        return api_response.parse()

Subdomains

Frequently Asked Questions

What does _process_response() do?
_process_response() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_base_client.py.
Where is _process_response() defined?
_process_response() is defined in src/anthropic/_base_client.py at line 1165.
What does _process_response() call?
_process_response() calls 4 function(s): _process_response, extract_response_type, get, parse.
What calls _process_response()?
_process_response() is called by 2 function(s): _process_response, request.

Analyze Your Own Codebase

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

Try Supermodel Free