Skip to content

Commit 25dadca

Browse files
authored
Fix support for Python WebSocket >=14 while preserving backward compatibility (#499)
* Fix support for Python WebSocket >=14 while preserving backward compatibility Python websockets did a breaking change with version 14. "extra_headers" was renamed to "additionnal_headers". * Unpin websockets dependency to support at least versions 12, 13 and 14
1 parent 94baa9d commit 25dadca

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

deepgram/clients/common/v1/abstract_async_websocket.py

+24-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@
1010
from abc import ABC, abstractmethod
1111

1212
import websockets
13-
from websockets.client import WebSocketClientProtocol
13+
14+
try:
15+
# Websockets versions >= 13
16+
from websockets.asyncio.client import connect, ClientConnection
17+
18+
WS_ADDITIONAL_HEADERS_KEY = "additional_headers"
19+
except ImportError:
20+
# Backward compatibility with websockets versions 12
21+
from websockets.legacy.client import ( # type: ignore
22+
connect,
23+
WebSocketClientProtocol as ClientConnection,
24+
)
25+
26+
WS_ADDITIONAL_HEADERS_KEY = "extra_headers"
1427

1528
from ....audio import Speaker
1629
from ....utils import verboselogs
@@ -25,6 +38,7 @@
2538
)
2639
from .websocket_events import WebSocketEvents
2740

41+
2842
ONE_SECOND = 1
2943
HALF_SECOND = 0.5
3044
DEEPGRAM_INTERVAL = 5
@@ -44,7 +58,7 @@ class AbstractAsyncWebSocketClient(ABC): # pylint: disable=too-many-instance-at
4458
_endpoint: str
4559
_websocket_url: str
4660

47-
_socket: Optional[WebSocketClientProtocol] = None
61+
_socket: Optional[ClientConnection] = None
4862

4963
_listen_thread: Union[asyncio.Task, None]
5064
_delegate: Optional[Speaker] = None
@@ -134,10 +148,14 @@ async def start(
134148
url_with_params = append_query_params(self._websocket_url, combined_options)
135149

136150
try:
137-
self._socket = await websockets.connect(
151+
ws_connect_kwargs: Dict = {
152+
"ping_interval": PING_INTERVAL,
153+
WS_ADDITIONAL_HEADERS_KEY: combined_headers,
154+
}
155+
156+
self._socket = await connect(
138157
url_with_params,
139-
extra_headers=combined_headers,
140-
ping_interval=PING_INTERVAL,
158+
**ws_connect_kwargs,
141159
)
142160
self._exit_event.clear()
143161

@@ -169,7 +187,7 @@ async def start(
169187
self._logger.notice("start succeeded")
170188
self._logger.debug("AbstractAsyncWebSocketClient.start LEAVE")
171189
return True
172-
except websockets.ConnectionClosed as e:
190+
except websockets.exceptions.ConnectionClosed as e:
173191
self._logger.error(
174192
"ConnectionClosed in AbstractAsyncWebSocketClient.start: %s", e
175193
)

deepgram/clients/common/v1/abstract_sync_websocket.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def start(
184184
self._logger.notice("start succeeded")
185185
self._logger.debug("AbstractSyncWebSocketClient.start LEAVE")
186186
return True
187-
except websockets.ConnectionClosed as e:
187+
except websockets.exceptions.ConnectionClosed as e:
188188
self._logger.error(
189189
"ConnectionClosed in AbstractSyncWebSocketClient.start: %s", e
190190
)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ readme = "README.md"
3131
[tool.poetry.dependencies]
3232
python = "^3.10"
3333
httpx = "^0.25.2"
34-
websockets = "^12.0"
34+
websockets = ">=12.0"
3535
typing-extensions = "^4.9.0"
3636
dataclasses-json = "^0.6.3"
3737
aiohttp = "^3.9.1"

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# pip install -r requirements.txt
22

33
# standard libs
4-
websockets==12.*
4+
websockets>=12.0
55
httpx==0.*
66
dataclasses-json==0.*
77
dataclasses==0.*

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
packages=find_packages(exclude=["tests"]),
2929
install_requires=[
3030
"httpx>=0.25.2",
31-
"websockets>=12.0,<14.0",
31+
"websockets>=12.0",
3232
"dataclasses-json>=0.6.3",
3333
"typing_extensions>=4.9.0",
3434
"aiohttp>=3.9.1",

0 commit comments

Comments
 (0)