From 228cc9d540ba463dbb934de092efa7f73f25508e Mon Sep 17 00:00:00 2001 From: Felix Lu <58391009+lumpinif@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:46:18 -0800 Subject: [PATCH] fix: a per-call header replaces the SDK's header whatever its case RequestOptions additional_headers were merged into the SDK's headers by exact name, so {"authorization": ...} was sent next to the SDK's Authorization as a second header, and the same for User-Agent. A name now replaces the SDK's header of that name in any casing, as an exactly matching name already did. --- src/fishaudio/core/client_wrapper.py | 14 ++++++++---- tests/unit/test_core.py | 32 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/fishaudio/core/client_wrapper.py b/src/fishaudio/core/client_wrapper.py index be74c73..51eda15 100644 --- a/src/fishaudio/core/client_wrapper.py +++ b/src/fishaudio/core/client_wrapper.py @@ -67,12 +67,18 @@ def __init__( def get_headers( self, additional_headers: Optional[dict[str, str]] = None ) -> dict[str, str]: - """Build headers including authentication and user agent.""" + """Build headers including authentication and user agent. + + A name in ``additional_headers`` replaces the SDK's header of that + name whatever its case, so the two are never sent side by side. + """ headers = { "Authorization": f"Bearer {self.api_key}", "User-Agent": f"fish-audio/python/{__version__}", } if additional_headers: + replaced = {name.lower() for name in additional_headers} + headers = {k: v for k, v in headers.items() if k.lower() not in replaced} headers.update(additional_headers) return headers @@ -81,9 +87,9 @@ def _prepare_request_kwargs( ) -> None: """Prepare request kwargs by merging headers, timeout, and query params.""" # Merge headers - headers = self.get_headers() - if request_options and request_options.additional_headers: - headers.update(request_options.additional_headers) + headers = self.get_headers( + request_options.additional_headers if request_options else None + ) # Headers set by the calling method win, whatever the case of the name: # {"Model": ...} from request options must not travel next to {"model": ...}. explicit = kwargs.get("headers", {}) diff --git a/tests/unit/test_core.py b/tests/unit/test_core.py index ce38383..eeb51e8 100644 --- a/tests/unit/test_core.py +++ b/tests/unit/test_core.py @@ -148,6 +148,38 @@ def test_additional_header_is_kept_without_an_explicit_one(self, mock_api_key): assert kwargs["headers"]["Model"] == "transcribe-1-pro" assert kwargs["headers"]["Authorization"] == f"Bearer {mock_api_key}" + @pytest.mark.parametrize( + "name", ["authorization", "AUTHORIZATION", "Authorization"] + ) + def test_additional_header_replaces_the_sdk_header_of_other_case( + self, mock_api_key, name + ): + # Sent through httpx, which would send two spellings as two headers. + seen = [] + + def handler(request: httpx.Request) -> httpx.Response: + seen.append(request.headers.get_list("authorization")) + return httpx.Response(200, json={}) + + http = httpx.Client( + base_url="https://api.test", transport=httpx.MockTransport(handler) + ) + wrapper = ClientWrapper(api_key=mock_api_key, httpx_client=http) + wrapper.request( + "GET", + "/x", + request_options=RequestOptions(additional_headers={name: "Bearer other"}), + ) + wrapper.request("GET", "/x") + assert seen == [["Bearer other"], [f"Bearer {mock_api_key}"]] + + def test_additional_user_agent_replaces_the_sdk_one(self, mock_api_key): + wrapper = ClientWrapper(api_key=mock_api_key) + headers = wrapper.get_headers({"user-agent": "mine"}) + assert {k: v for k, v in headers.items() if k.lower() == "user-agent"} == { + "user-agent": "mine" + } + class TestAsyncClientWrapper: """Test async AsyncClientWrapper."""