DeprecationWarning with websockets>=14: runtime import of WebSocketClientProtocol

Author: 23f3001135Created Jan 22, 2026Updated Apr 1, 2026

Hi

With python-binance==1.0.34 and websockets>=14, importing the websocket client triggers deprecation warnings:

--- Start --- .venv/lib/python3.14t/site-packages/binance/ws/websocket_api.py:4 /home/ubuntu/nnf_beta/.venv/lib/python3.14t/site-packages/binance/ws/websocket_api.py:4: DeprecationWarning: websockets.WebSocketClientProtocol is deprecated from websockets import WebSocketClientProtocol # type: ignore

.venv/lib/python3.14t/site-packages/websockets/legacy/init.py:6 /home/ubuntu/nnf_beta/.venv/lib/python3.14t/site-packages/websockets/legacy/init.py:6: DeprecationWarning: websockets.legacy is deprecated; see https://websockets.readthedocs.io/en/stable/howto/upgrade.html for upgrade instructions warnings.warn( # deprecated in 14.0 - 2024-11-09

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html --- End ---

This comes from a runtime import in: binance/ws/websocket_api.py

from websockets import WebSocketClientProtocol

According to the websockets 14+ upgrade notes, WebSocketClientProtocol and websockets.legacy are deprecated and scheduled for removal.

The warning is triggered on import, even when the class is only used for typing. This shows up clearly during pytest runs.

A short-term fix could be to guard the import behind typing.TYPE_CHECKING (or similar), which would remove the runtime warning without changing behavior.

Longer-term, a migration to the new websockets asyncio API may be needed, but I wanted to report this early since it will eventually become a hard failure.

Environment:

  • python-binance==1.0.34
  • websockets>=14
  • Python 3.12 / 3.13 / 3.14-dev (reproduces across versions)

Happy to open a PR for a minimal fix if this approach is acceptable.

Source: sammchardy/python-binance