Repository navigation
fix(core): raise ApiError when a sync websocket handshake is rejected - #832
Open
mayankbohradev wants to merge 2 commits into
Open
mayankbohradev wants to merge 2 commits into
mayankbohradev wants to merge 2 commits into
Conversation
websockets raises InvalidStatusCode from the legacy asyncio client and InvalidStatus from websockets.sync.client. The compat shim bound only InvalidStatusCode, so the sync connect() methods raised a raw InvalidStatus for a rejected handshake (401, 429) while the async methods raised ApiError. Match both classes and read the status code from either layout. The generated except clauses stay unchanged. Add regression coverage that uses a real local server, and freeze the patched shim in .fernignore with an AGENTS.md entry.
mayankbohradev
requested review from
GregHolmes,
deepgram-kiley,
dg-coreylweathers and
jasondielman-dg
as code owners
November 18, 2026 07:56
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Sync WebSocket clients now raise
ApiErrorwhen the server rejects the handshake, the same as the async clients. Before this change they raised a rawwebsockets.exceptions.InvalidStatus.Why
except ApiErrorin sync code does not catch a rejected handshake (HTTP 401 for a bad key, 429 for a rate limit). This runs against the live API with an invalid key ondeepgram-sdk==7.19.0:Cause:
websocketshas two exception classes for a rejected handshake.InvalidStatusCode(exc.status_code).websockets.sync.clientraisesInvalidStatus(exc.response.status_code).core/websocket_compat.pyimportsInvalidStatusCodefirst and usesInvalidStatusonly onImportError. The legacy name still imports on every supportedwebsocketsversion, soInvalidWebSocketStatuswas always the legacy class. The asyncconnect()methods use the legacy client, so they matched. The syncconnect()methods usewebsockets.sync.client, so they never matched.The result is the same on
websockets12.0, 13.1, 16.1.1 and 17.4. It depends on sync or async, not on thewebsocketsversion.v5.0.0already caught onlyInvalidStatusCodearound the sync connect call, so this is not a regression.How
InvalidWebSocketStatusis now a tuple of both classes. The 20 generatedexcept InvalidWebSocketStatus as exccall sites (sync and async, inclient.pyandraw_client.pyfor Listen V1/V2, Speak V1/V2 and Agent V1) do not change.get_status_codereadsexc.status_codewhen it exists, elseexc.response.status_code.core/websocket_compat.pyis Fern output, so the patch is recorded as temporarily frozen in.fernignoreandAGENTS.md, with the unfreeze condition: remove it when the generated shim matches both classes.InvalidWebSocketStatus.__new__(InvalidWebSocketStatus). They now take the class from the tuple.Testing
tests/custom/test_websocket_handshake_rejection.py, registered in.fernignoreandAGENTS.md. It starts a real localwebsocketsserver that rejects the handshake with 401 and 429, and checks every sync and asyncconnect(), high-level andwith_raw_response. The existing mapping tests raise a hand-built exception from a patchedconnect, so they passed while the real sync client raised a different class.poetry run pytest -rP --cov=deepgram --cov-branch ., with the WireMock container: 1197 passed, 4 skipped, 1 xfailed; coverage 95.11%. The full suite also passes on Python 3.10, 3.11, 3.12 and 3.13.websockets12.0, 13.1, 16.1.1 and 17.4.mypy src/andmypy tests/typecheckpass on Python 3.10 and 3.13.ruff checkpasses on the changed files.poetry buildpasses. The built wheel, installed into clean environments on Python 3.10 to 3.14, raisesApiErrorfor a rejected handshake (401 and 429) on sync and async, high-level andwith_raw_response.ApiError(status_code=401)for both clients.Notes for reviewers
InvalidWebSocketStatuschanges from a class to a tuple of classes. It is re-exported fromdeepgram.core.exceptandisinstancework as before. Code that instantiates it or uses it as a type annotation would need a change; insrc/andexamples/it is used only in theexceptclauses.examples/17-transcription-live-reconnect.pycatches(ApiError, InvalidStatus)for this case. I kept the clause so the example still works with older SDK releases, and corrected its comment, which said the type depends on thewebsocketsversion.websockets14 and later, theInvalidStatusCodeimport emits aDeprecationWarning. That was already the case and this PR does not change it.