Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for Python WebSocket >=14 while preserving backward compatibility #499

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

fviard
Copy link

@fviard fviard commented Jan 26, 2025

Proposed changes

Websockets did a breaking change with version 14:
The client async base implementation changed and the "extra_headers" parameter was renamed to "additional_headers".
(See issue #483)
A previous change solved the issue by pinning the websockets version to 12 or 13 (#487).

An alternative solution was proposed with the PR #486 but it was not satisfying as it was breaking the backward compatibility.

The purpose of the current PR is to support the new versions of python websockets by preserving support for the older 12 and 13 versions.

Fixes #483

Types of changes

What types of changes does your code introduce to the community Python SDK?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update or tests (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have lint'ed all of my code using repo standards
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

Summary by CodeRabbit

  • Dependencies

    • Updated websockets library version constraint to allow versions 12.0 and higher across multiple configuration files.
  • Compatibility

    • Enhanced handling of WebSocket connections to support different versions of the websockets library.
    • Improved exception handling for closed connections to ensure robustness in the WebSocket client implementation.

Copy link
Contributor

coderabbitai bot commented Jan 26, 2025

Walkthrough

The pull request addresses compatibility issues with the websockets library by modifying how connection headers are handled across different major versions. It introduces a new import structure for handling WebSocket connections, updates the connection logic in the AbstractAsyncWebSocketClient, and enhances exception handling in both async and sync WebSocket clients. Additionally, version constraints for the websockets dependency are broadened across multiple configuration files (pyproject.toml, requirements.txt, setup.py) to support versions 12.0 and above.

Changes

File Change Summary
deepgram/clients/common/v1/abstract_async_websocket.py Modified connection logic to handle different websockets library versions and updated _socket type.
deepgram/clients/common/v1/abstract_sync_websocket.py Updated exception handling for closed connections to use websockets.exceptions.ConnectionClosed.
pyproject.toml, requirements.txt, setup.py Updated websockets dependency version constraint from version-specific to >=12.0.

Assessment against linked issues

Objective Addressed Explanation
Fix WebSocket version 14.0 connection error [#483]
Ensure compatibility with WebSocket versions 12.0+

Possibly related PRs

Suggested labels

needs information

Suggested reviewers

  • jpvajda
  • SandraRodgers
  • naomi-lgbt
  • lukeocodes
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@jpvajda jpvajda mentioned this pull request Jan 27, 2025
8 tasks
@jpvajda jpvajda requested a review from jjmaldonis January 27, 2025 20:12
@jpvajda
Copy link
Contributor

jpvajda commented Jan 27, 2025

@jjmaldonis did you have any concerns on this approach?

@jpvajda jpvajda self-requested a review January 27, 2025 20:12
@jpvajda jpvajda modified the milestones: Python 3.8, Python Post-3.8 Jan 27, 2025
jjmaldonis
jjmaldonis previously approved these changes Jan 27, 2025
Copy link
Contributor

@jjmaldonis jjmaldonis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thank you @fviard!

@fviard
Copy link
Author

fviard commented Jan 27, 2025

Re-pushed to fix the linter/typing issues. :-)

@fviard fviard force-pushed the fix-websockets-14-retrocompat branch from 6ecbe4a to 43c0619 Compare January 28, 2025 00:37
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🔭 Outside diff range comments (1)
deepgram/clients/common/v1/abstract_sync_websocket.py (1)

Line range hint 1-1: Consider adding version compatibility handling to sync implementation.

The async implementation includes version-specific handling for the WebSocket library's breaking changes, but similar changes are missing in the sync implementation. Consider adding the same version compatibility approach to maintain consistency.

Apply these changes to the sync implementation:

 import websockets
+
+try:
+    # Websockets versions >= 13
+    from websockets.sync.client import connect, ClientConnection
+    WS_ADDITIONAL_HEADERS_KEY = "additional_headers"
+except ImportError:
+    # Backward compatibility with websockets versions 12
+    from websockets.sync.client import (
+        connect,
+        WebSocketClientProtocol as ClientConnection,
+    )
+    WS_ADDITIONAL_HEADERS_KEY = "extra_headers"

Then update the connection setup:

-    self._socket = connect(url_with_params, additional_headers=combined_headers)
+    ws_connect_kwargs: Dict = {
+        WS_ADDITIONAL_HEADERS_KEY: combined_headers
+    }
+    self._socket = connect(url_with_params, **ws_connect_kwargs)
🧹 Nitpick comments (1)
deepgram/clients/common/v1/abstract_async_websocket.py (1)

151-158: Clean implementation of version-agnostic connection setup!

The use of a dictionary with dynamic key names is an elegant solution. Consider adding a type annotation for ping_interval to improve type safety.

 ws_connect_kwargs: Dict = {
-    "ping_interval": PING_INTERVAL,
+    "ping_interval": PING_INTERVAL,  # type: float
     WS_ADDITIONAL_HEADERS_KEY: combined_headers,
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6ecbe4a and 43c0619.

📒 Files selected for processing (5)
  • deepgram/clients/common/v1/abstract_async_websocket.py (5 hunks)
  • deepgram/clients/common/v1/abstract_sync_websocket.py (1 hunks)
  • pyproject.toml (1 hunks)
  • requirements.txt (1 hunks)
  • setup.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • requirements.txt
  • setup.py
  • pyproject.toml
🔇 Additional comments (4)
deepgram/clients/common/v1/abstract_async_websocket.py (3)

13-26: Well-structured version compatibility handling!

The implementation elegantly handles the breaking change in the WebSocket library by using a try-except block to support both the new additional_headers (>=13) and legacy extra_headers (12) parameter names.


61-61: Type annotation correctly updated!

The socket type has been properly updated to use ClientConnection, which is aliased correctly for both new and old versions of the library.


190-190: Exception handling properly updated!

The exception handling has been correctly updated to use the websockets.exceptions namespace, maintaining consistency with the library's error handling patterns.

deepgram/clients/common/v1/abstract_sync_websocket.py (1)

187-187: Exception handling consistently updated across sync and async implementations!

The exception handling in the sync client has been properly aligned with the async client, using the correct websockets.exceptions namespace.

…tibility

Python websockets did a breaking change with version 14.
"extra_headers" was renamed to "additionnal_headers".
@fviard fviard force-pushed the fix-websockets-14-retrocompat branch from 43c0619 to 14ad591 Compare January 28, 2025 00:49
@fviard
Copy link
Author

fviard commented Jan 28, 2025

Sorry for the multiple pushs, I have reworked the fix to be more elegant.

It is now all good, including linter checks, and I have verified that it works correctly with all the websockets released versions (12, 13, 14) for sync and async clients.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
deepgram/clients/common/v1/abstract_async_websocket.py (2)

151-158: LGTM! Clean connection parameter handling with room for improvement.

The connection logic properly handles version-specific header keys while maintaining existing functionality.

Consider moving the magic number 20 to use the existing PING_INTERVAL constant:

             ws_connect_kwargs: Dict = {
-                "ping_interval": PING_INTERVAL,
+                "ping_interval": PING_INTERVAL,  # Using constant defined at line 43
                 WS_ADDITIONAL_HEADERS_KEY: combined_headers,
             }

190-190: LGTM! Exception handling properly updated.

The exception handling correctly uses the new import path for ConnectionClosed.

Consider adding specific handling for websockets.exceptions.InvalidStatusCode to provide more detailed error messages for HTTP-related connection failures:

         except websockets.exceptions.ConnectionClosed as e:
             self._logger.error(
                 "ConnectionClosed in AbstractAsyncWebSocketClient.start: %s", e
             )
             self._logger.debug("AbstractAsyncWebSocketClient.start LEAVE")
             if self._config.options.get("termination_exception_connect", False):
                 raise
             return False
+        except websockets.exceptions.InvalidStatusCode as e:
+            self._logger.error(
+                "HTTP error in AbstractAsyncWebSocketClient.start (status code %d): %s",
+                e.status_code,
+                e
+            )
+            self._logger.debug("AbstractAsyncWebSocketClient.start LEAVE")
+            if self._config.options.get("termination_exception_connect", False):
+                raise
+            return False
         except websockets.exceptions.WebSocketException as e:
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 43c0619 and 14ad591.

📒 Files selected for processing (5)
  • deepgram/clients/common/v1/abstract_async_websocket.py (5 hunks)
  • deepgram/clients/common/v1/abstract_sync_websocket.py (1 hunks)
  • pyproject.toml (1 hunks)
  • requirements.txt (1 hunks)
  • setup.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • requirements.txt
  • setup.py
  • pyproject.toml
  • deepgram/clients/common/v1/abstract_sync_websocket.py
🔇 Additional comments (2)
deepgram/clients/common/v1/abstract_async_websocket.py (2)

61-61: LGTM! Type annotation correctly updated.

The type annotation for _socket has been properly updated to use the version-agnostic ClientConnection type.


13-26: LGTM! Clean version-specific import handling.

The implementation elegantly handles backward compatibility with websockets v12 while supporting v13+ through version-specific imports and dynamic header key selection.

Let's verify the websockets version compatibility:

✅ Verification successful

✓ Version compatibility verified

The implementation correctly handles both websockets v12 (using legacy.client.WebSocketClientProtocol) and v13+ (using asyncio.client.ClientConnection). Import paths and type hints are accurate for both versions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify websockets package version compatibility
# Test: Check if the code works with both v12 and v13+ of websockets

# Check installed version
pip show websockets | grep "Version"

# Check imports in different versions
python3 -c "
import websockets
import sys

v = websockets.__version__
print(f'Testing with websockets {v}')

if int(v.split('.')[0]) >= 13:
    # Should succeed
    from websockets.asyncio.client import ClientConnection
    print('✓ Successfully imported ClientConnection from asyncio.client')
else:
    # Should succeed
    from websockets.legacy.client import WebSocketClientProtocol
    print('✓ Successfully imported WebSocketClientProtocol from legacy.client')
"

Length of output: 696


Script:

#!/bin/bash
# Test both websockets v12 and v13

echo "Testing websockets v12..."
pip install 'websockets>=12.0,<13.0'
python3 -c "
from websockets.legacy.client import WebSocketClientProtocol
print('✓ v12: Successfully imported WebSocketClientProtocol from legacy.client')
"

echo -e "\nTesting websockets v13..."
pip install 'websockets>=13.0'
python3 -c "
from websockets.asyncio.client import ClientConnection
print('✓ v13: Successfully imported ClientConnection from asyncio.client')
"

Length of output: 1638

@fviard
Copy link
Author

fviard commented Jan 30, 2025

Hi @jjmaldonis ,
Would it be possible for you to re-approve and eventually merge / release this change if you are happy about it?
Thanks

@jjmaldonis
Copy link
Contributor

I approved, and @jpvajda and team can merge + deploy depending on their timing.

@jpvajda jpvajda requested a review from naomi-lgbt January 30, 2025 17:34
@jpvajda
Copy link
Contributor

jpvajda commented Jan 30, 2025

I'm testing this change now with hopes to get it in our upcoming 3.9 release.

@jpvajda
Copy link
Contributor

jpvajda commented Jan 30, 2025

Test Results are promising:

@jjmaldonis / @naomi-lgbt let me know if this requires additional testing.

==================================================== short test summary info =====================================================
FAILED tests/daily_test/test_daily_async_listen_rest_url.py::test_daily_async_listen_rest_url[url0-options0-expected_output0] - AssertionError: Test ID: b00dc103a62ea2ccfc752ec0f646c7528ef5e729a9d7481d2a944253a9128ce2-29e7c8100617f70da4ae9da1921cb5071a0...
FAILED tests/daily_test/test_daily_async_listen_rest_url.py::test_daily_async_listen_rest_url[url1-options1-expected_output1] - AssertionError: Test ID: f3b6208a662156067a41bddd295a1a0a53ea34a268e27a8f1a9d7107aa99732f-29e7c8100617f70da4ae9da1921cb5071a0...
FAILED tests/daily_test/test_daily_async_listen_websocket.py::test_daily_async_listen_websocket[preamble-websocket.wav-options1-expected_output1] - AssertionError: Test ID: a6d1b12d5ce73a51a7b69ab156f0c98c72cdc1cfcf4a25f7b634c328cce4d760-42fc5ed98cabc1fa1a2f276301c27c46dd1...
FAILED tests/daily_test/test_daily_listen_rest_url.py::test_daily_listen_rest_url[url0-options0-expected_output0] - AssertionError: Test ID: b00dc103a62ea2ccfc752ec0f646c7528ef5e729a9d7481d2a944253a9128ce2-29e7c8100617f70da4ae9da1921cb5071a0...
FAILED tests/daily_test/test_daily_listen_rest_url.py::test_daily_listen_rest_url[url1-options1-expected_output1] - AssertionError: Test ID: f3b6208a662156067a41bddd295a1a0a53ea34a268e27a8f1a9d7107aa99732f-29e7c8100617f70da4ae9da1921cb5071a0...
FAILED tests/daily_test/test_daily_listen_websocket.py::test_daily_listen_websocket[preamble-websocket.wav-options1-expected_output1] - AssertionError: Test ID: a6d1b12d5ce73a51a7b69ab156f0c98c72cdc1cfcf4a25f7b634c328cce4d760-42fc5ed98cabc1fa1a2f276301c27c46dd1...
=============================== 6 failed, 10 passed, 19 deselected, 4 warnings in 73.09s (0:01:13) ===============================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deepgram SDK Fails with WebSocket Version 14.0 - Unexpected Keyword Argument extra_headers
3 participants