Skip to content

Commit 2ce9909

Browse files
committed
Patch WebsocketClient to work with any websockets version >= 10.0
1 parent 6b7acc8 commit 2ce9909

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [4.0.8] - 2025-06-23
9+
10+
- Patch WebsocketClient to work with any websockets version >= 10.0
11+
812
## [4.0.7] - 2025-06-19
913

1014
- Support requesting a temporary token (JWT) with region and client ref

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.7
1+
4.0.8

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
websockets>=14.0
1+
websockets>=10.0
22
httpx[http2]~=0.23
33
polling2~=0.5
44
toml~=0.10.2

speechmatics/client.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@
3939
UsageMode,
4040
)
4141

42+
try:
43+
# Try to import from new websockets >=13.0
44+
from websockets.asyncio.client import connect
45+
WS_HEADERS_KEY = "additional_headers"
46+
except ImportError:
47+
# Fall back to legacy websockets
48+
from websockets.legacy.client import connect
49+
WS_HEADERS_KEY = "extra_headers"
50+
51+
52+
4253
LOGGER = logging.getLogger(__name__)
4354

4455
# If the logging level is set to DEBUG then websockets logs very verbosely,
@@ -613,14 +624,18 @@ async def run(
613624
parsed_url._replace(path=url_path, query=updated_query)
614625
)
615626

627+
ws_kwargs = {
628+
WS_HEADERS_KEY: extra_headers,
629+
"ssl": self.connection_settings.ssl_context,
630+
"ping_timeout": self.connection_settings.ping_timeout_seconds,
631+
# Don't limit the max. size of incoming messages
632+
"max_size": None,
633+
}
634+
616635
try:
617-
async with websockets.connect( # pylint: disable=no-member
636+
async with connect(
618637
updated_url,
619-
ssl=self.connection_settings.ssl_context,
620-
ping_timeout=self.connection_settings.ping_timeout_seconds,
621-
# Don't limit the max. size of incoming messages
622-
max_size=None,
623-
additional_headers=extra_headers,
638+
**ws_kwargs,
624639
) as self.websocket:
625640
await self._communicate(stream, audio_settings)
626641
finally:

tests/test_client.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525

2626
from tests.utils import path_to_test_resource, default_ws_client_setup
2727

28+
try:
29+
# Try to import from new websockets >= 13
30+
from websockets.asyncio.client import connect
31+
WS_HEADERS_KEY = "additional_headers"
32+
except ImportError:
33+
# Fall back to legacy websockets
34+
from websockets.legacy.client import connect
35+
WS_HEADERS_KEY = "extra_headers"
36+
37+
2838

2939
def test_json_utf8():
3040
@client.json_utf8
@@ -229,13 +239,16 @@ async def test_send_message(mock_server, message_type: str, message_data: Any):
229239
"""
230240
ws_client, _, _ = default_ws_client_setup(mock_server.url)
231241
ws_client.session_running = True
232-
233-
async with websockets.connect(
242+
ws_kwargs = {
243+
"ssl": ws_client.connection_settings.ssl_context,
244+
"ping_timeout": ws_client.connection_settings.ping_timeout_seconds,
245+
"max_size": None,
246+
WS_HEADERS_KEY: None,
247+
}
248+
249+
async with connect(
234250
mock_server.url,
235-
ssl=ws_client.connection_settings.ssl_context,
236-
ping_timeout=ws_client.connection_settings.ping_timeout_seconds,
237-
max_size=None,
238-
additional_headers=None,
251+
**ws_kwargs,
239252
) as ws_client.websocket:
240253
await ws_client.send_message(message_type, message_data)
241254
assert message_type in [
@@ -387,7 +400,7 @@ async def mock_connect(*_, **__):
387400

388401
mock_logger_error_method = MagicMock()
389402

390-
with patch("websockets.connect", mock_connect):
403+
with patch("speechmatics.client.connect", mock_connect):
391404
with patch.object(client.LOGGER, "error", mock_logger_error_method):
392405
try:
393406
ws_client.run_synchronously(
@@ -411,7 +424,7 @@ def call_exit(*args, **kwargs):
411424
mock_server.url
412425
)
413426
stream = MagicMock()
414-
with patch("websockets.connect", connect_mock):
427+
with patch("speechmatics.client.connect", connect_mock):
415428
try:
416429
ws_client.run_synchronously(
417430
transcription_config=transcription_config,
@@ -422,7 +435,7 @@ def call_exit(*args, **kwargs):
422435
except Exception:
423436
assert len(connect_mock.mock_calls) == 1
424437
assert (
425-
connect_mock.mock_calls[0][2]["additional_headers"] == extra_headers
438+
connect_mock.mock_calls[0][2][WS_HEADERS_KEY] == extra_headers
426439
), f"Extra headers don't appear in the call list = {connect_mock.mock_calls}"
427440

428441

0 commit comments

Comments
 (0)