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

_build_headers() — anthropic-sdk-python Function Reference

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

Function python AnthropicClient SyncAPI calls 2 called by 1

Entity Profile

Dependency Diagram

graph TD
  56ae8c4c_37ad_68f4_21ce_35640dd2a264["_build_headers()"]
  842ce0be_ea56_b939_960f_13eb61230daf["BaseClient"]
  56ae8c4c_37ad_68f4_21ce_35640dd2a264 -->|defined in| 842ce0be_ea56_b939_960f_13eb61230daf
  c1255cc8_bc9d_85e2_d7e7_d99e63ebb389["_build_request()"]
  c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 -->|calls| 56ae8c4c_37ad_68f4_21ce_35640dd2a264
  c0dee7e1_89d5_7be9_c7ac_83843a32bf4d["_validate_headers()"]
  56ae8c4c_37ad_68f4_21ce_35640dd2a264 -->|calls| c0dee7e1_89d5_7be9_c7ac_83843a32bf4d
  2c40ea6c_a4cd_eacc_98e4_6c1614da6780["_merge_mappings()"]
  56ae8c4c_37ad_68f4_21ce_35640dd2a264 -->|calls| 2c40ea6c_a4cd_eacc_98e4_6c1614da6780
  style 56ae8c4c_37ad_68f4_21ce_35640dd2a264 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/anthropic/_base_client.py lines 440–472

    def _build_headers(self, options: FinalRequestOptions, *, retries_taken: int = 0) -> httpx.Headers:
        custom_headers = options.headers or {}
        headers_dict = _merge_mappings(
            {
                "x-stainless-timeout": str(options.timeout.read)
                if isinstance(options.timeout, Timeout)
                else str(options.timeout),
                **self.default_headers,
            },
            custom_headers,
        )
        self._validate_headers(headers_dict, custom_headers)

        # headers are case-insensitive while dictionaries are not.
        headers = httpx.Headers(headers_dict)

        idempotency_header = self._idempotency_header
        if idempotency_header and options.idempotency_key and idempotency_header not in headers:
            headers[idempotency_header] = options.idempotency_key

        # Don't set these headers if they were already set or removed by the caller. We check
        # `custom_headers`, which can contain `Omit()`, instead of `headers` to account for the removal case.
        lower_custom_headers = [header.lower() for header in custom_headers]
        if "x-stainless-retry-count" not in lower_custom_headers:
            headers["x-stainless-retry-count"] = str(retries_taken)
        if "x-stainless-read-timeout" not in lower_custom_headers:
            timeout = self.timeout if isinstance(options.timeout, NotGiven) else options.timeout
            if isinstance(timeout, Timeout):
                timeout = timeout.read
            if timeout is not None:
                headers["x-stainless-read-timeout"] = str(timeout)

        return headers

Subdomains

Called By

Frequently Asked Questions

What does _build_headers() do?
_build_headers() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_base_client.py.
Where is _build_headers() defined?
_build_headers() is defined in src/anthropic/_base_client.py at line 440.
What does _build_headers() call?
_build_headers() calls 2 function(s): _merge_mappings, _validate_headers.
What calls _build_headers()?
_build_headers() is called by 1 function(s): _build_request.

Analyze Your Own Codebase

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

Try Supermodel Free