diff --git a/src/anthropic/_base_client.py b/src/anthropic/_base_client.py index f2eb5de2..1a8f8938 100644 --- a/src/anthropic/_base_client.py +++ b/src/anthropic/_base_client.py @@ -456,11 +456,17 @@ def _make_status_error( def _build_headers(self, options: FinalRequestOptions, *, retries_taken: int = 0) -> httpx.Headers: custom_headers = options.headers or {} + req_timeout = self.timeout if isinstance(options.timeout, NotGiven) else options.timeout + if isinstance(req_timeout, Timeout): + stainless_timeout = str(req_timeout.read) + elif req_timeout is not None: + stainless_timeout = str(req_timeout) + else: + default_timeout = self.timeout + stainless_timeout = str(default_timeout.read if isinstance(default_timeout, Timeout) else default_timeout) headers_dict = _merge_mappings( { - "x-stainless-timeout": str(options.timeout.read) - if isinstance(options.timeout, Timeout) - else str(options.timeout), + "x-stainless-timeout": stainless_timeout, **self.default_headers, }, custom_headers, diff --git a/tests/test_client.py b/tests/test_client.py index d8814b75..94a61571 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -416,6 +416,13 @@ def test_default_headers_option(self) -> None: test_client.close() test_client2.close() + def test_default_timeout_header_is_not_not_given(self) -> None: + client = Anthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True) + request = client._build_request(FinalRequestOptions(method="get", url="/foo")) + assert request.headers.get("x-stainless-timeout") != "NOT_GIVEN" + assert request.headers.get("x-stainless-timeout") == request.headers.get("x-stainless-read-timeout") + client.close() + def test_validate_headers(self) -> None: client = Anthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True) request = client._build_request(FinalRequestOptions(method="get", url="/foo"))