From 75d95ec68e9435f4b83fb57501bc43bb87feb450 Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Tue, 3 Nov 2026 13:19:55 +0000 Subject: [PATCH] fix(client): copy custom headers before adding the session id --- src/deepgram/client.py | 20 +++++++++---------- .../test_client_construction_and_helpers.py | 13 ++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/deepgram/client.py b/src/deepgram/client.py index 56e98d11..e1deb7ad 100644 --- a/src/deepgram/client.py +++ b/src/deepgram/client.py @@ -99,11 +99,11 @@ def __init__(self, *args, **kwargs) -> None: # Use provided session_id or generate one final_session_id = session_id if session_id is not None else str(uuid.uuid4()) - # Ensure headers object exists for pass-through custom headers - headers: Optional[Dict[str, str]] = kwargs.get("headers") - if headers is None: - headers = {} - kwargs["headers"] = headers + # Copy pass-through custom headers so the session header below does not + # mutate the caller's dict (a dict shared by several clients would + # otherwise make every client send the last client's session id). + headers: Dict[str, str] = dict(kwargs.get("headers") or {}) + kwargs["headers"] = headers # Ensure every request has a session identifier header headers["x-deepgram-session-id"] = final_session_id @@ -183,11 +183,11 @@ def __init__(self, *args, **kwargs) -> None: # Use provided session_id or generate one final_session_id = session_id if session_id is not None else str(uuid.uuid4()) - # Ensure headers object exists for pass-through custom headers - headers: Optional[Dict[str, str]] = kwargs.get("headers") - if headers is None: - headers = {} - kwargs["headers"] = headers + # Copy pass-through custom headers so the session header below does not + # mutate the caller's dict (a dict shared by several clients would + # otherwise make every client send the last client's session id). + headers: Dict[str, str] = dict(kwargs.get("headers") or {}) + kwargs["headers"] = headers # Ensure every request has a session identifier header headers["x-deepgram-session-id"] = final_session_id diff --git a/tests/custom/test_client_construction_and_helpers.py b/tests/custom/test_client_construction_and_helpers.py index eb72c9d3..4a124488 100644 --- a/tests/custom/test_client_construction_and_helpers.py +++ b/tests/custom/test_client_construction_and_helpers.py @@ -62,6 +62,19 @@ def test_client_generates_session_id_and_opts_out_of_redaction(client_cls: typin assert client.session_id # auto-generated UUID +@pytest.mark.parametrize("client_cls", [DeepgramClient, AsyncDeepgramClient]) +def test_clients_sharing_a_headers_dict_keep_their_own_session_id(client_cls: typing.Any) -> None: + shared_headers = {"X-Custom": "value"} + first = client_cls(environment=_environment(), api_key="k", headers=shared_headers) + second = client_cls(environment=_environment(), api_key="k", headers=shared_headers) + + for client in (first, second): + sent = client._client_wrapper.get_headers() + assert sent["x-deepgram-session-id"] == client.session_id + assert sent["X-Custom"] == "value" + assert shared_headers == {"X-Custom": "value"} + + @pytest.mark.parametrize("client_cls", [DeepgramClient, AsyncDeepgramClient]) def test_client_with_transport_factory_disables_reconnect(client_cls: typing.Any) -> None: from deepgram.transport import restore_transport