Skip to content

Commit dec377c

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

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
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-30
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: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@
3939
UsageMode,
4040
)
4141

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

4456
# If the logging level is set to DEBUG then websockets logs very verbosely,
@@ -613,14 +625,18 @@ async def run(
613625
parsed_url._replace(path=url_path, query=updated_query)
614626
)
615627

628+
ws_kwargs = {
629+
WS_HEADERS_KEY: extra_headers,
630+
"ssl": self.connection_settings.ssl_context,
631+
"ping_timeout": self.connection_settings.ping_timeout_seconds,
632+
# Don't limit the max. size of incoming messages
633+
"max_size": None,
634+
}
635+
616636
try:
617-
async with websockets.connect( # pylint: disable=no-member
637+
async with connect(
618638
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,
639+
**ws_kwargs,
624640
) as self.websocket:
625641
await self._communicate(stream, audio_settings)
626642
finally:

tests/test_client.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import pytest
1111

1212
from pytest_httpx import HTTPXMock
13-
import websockets
1413
from speechmatics import client
1514
from speechmatics.batch_client import BatchClient
1615
from speechmatics.exceptions import ForceEndSession
@@ -25,6 +24,17 @@
2524

2625
from tests.utils import path_to_test_resource, default_ws_client_setup
2726

27+
try:
28+
# Try to import from new websockets >= 13
29+
from websockets.asyncio.client import connect
30+
31+
WS_HEADERS_KEY = "additional_headers"
32+
except ImportError:
33+
# Fall back to legacy websockets
34+
from websockets.legacy.client import connect
35+
36+
WS_HEADERS_KEY = "extra_headers"
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)