Skip to content

fix(assemblyai): parse and handle WebSocket Error messages in STT service - #8761

Open
pylrn wants to merge 1 commit into
livekit:mainfrom
pylrn:fix/assemblyai-stt-error-message-handling
Open

pylrn wants to merge 1 commit into
livekit:mainfrom
pylrn:fix/assemblyai-stt-error-message-handling

Conversation

@pylrn

@pylrn pylrn commented Nov 24, 2026

Copy link
Copy Markdown

Summary

  • Parses and handles WebSocket Error messages from AssemblyAI in livekit.plugins.assemblyai.STT.
  • When an Error message is received from the AssemblyAI WebSocket (e.g., {"type": "Error", "error_code": 3005, "error": "Session duration exceeded"}), _process_stream_event now extracts the error_code, error, and error_type, logs the error with session context, and raises an APIStatusError carrying the specific status code, error message, and retryable status.
  • Classifies non-retryable errors (e.g. 1008/4001 authentication, 3010/3011/3012/3014 parameter/format invalidity) vs retryable server errors (e.g. 3005 session duration limit, 3009/4029 rate limits), honoring any explicit retryable boolean sent by the server.
  • Updates recv_task to re-raise APIError directly so that errors raised during event processing propagate out of recv_task rather than being caught by the generic except Exception: block.
  • Preserves _last_error so that if the server transmits an Error frame immediately prior to closing the socket, the specific APIStatusError is surfaced rather than a generic "AssemblyAI connection closed unexpectedly".

Verification

  • Added unit tests in tests/test_plugin_assemblyai_stt.py:
    • test_process_stream_event_error_raises_api_status_error: Verifies Error frame raises APIStatusError with correct code, message, request ID, and retryable flag.
    • test_process_stream_event_error_auth_non_retryable: Verifies error code 1008 (auth) is marked retryable=False.
    • test_process_stream_event_error_explicit_retryable: Verifies explicit retryable field from frame overrides defaults.
    • test_process_stream_event_error_without_error_code: Verifies graceful fallback when error_code is omitted (status_code=-1).
  • All 133 tests in tests/test_plugin_assemblyai_stt.py and 51 tests in tests/test_plugin_assemblyai_tts.py pass cleanly.
  • ruff check and ruff format --check report 0 issues.

@pylrn
pylrn requested a review from a team as a code owner November 24, 2026 15:18

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 2 potential issues.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment on lines +988 to +990
retryable = data.get("retryable")
if not isinstance(retryable, bool):
retryable = code not in _NON_RETRYABLE_STT_CODES

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔴 No retry after temporary rate limits

When AssemblyAI rejects a session start for exceeding its per-minute limit, _NON_RETRYABLE_STT_CODES treats it as a permanent failure. The provider uses 1008 for both that limit and credential failures. RecognizeStream stops immediately instead of using the configured retries.

Learn more

AssemblyAI’s streaming rate-limit documentation specifies a 1008 Error frame with the text Unauthorized Connection: Too many concurrent sessions when the account uses its session-start allowance for the minute. The code also covers permanent credential and billing failures. This frame has no retryable flag, so it goes through the same default as a bad key. RecognizeStream._main_task exits on the first failure rather than using its configured retries. Previously, the close code reached recv_task without an explicit retry flag, so APIStatusError treated it as retryable.

Example: A free account starts its sixth stream just before the minute resets. The provider rejects the start with 1008. The stream stops even though a configured retry after the reset could open it successfully.

Recommended fix: When no explicit boolean is present, distinguish the documented rate-limit text from other 1008 errors and keep authorization failures non-retryable. Allow retry after backoff into the next rate-limit window, subject to the configured retry policy. Test the documented frame alongside a bad-key frame.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +778 to +779
if self._last_error is not None:
raise self._last_error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Wrong error after reconnect

When a replacement socket closes unexpectedly without an Error frame, recv_task raises the previous socket’s error. RecognizeStream retries on the same stream, so callers get the old status and session instead of the new close details.

Learn more

A WebSocket Error frame becomes an exception in _process_stream_event. recv_task immediately propagates it, so that receive loop never reads the following close frame. When the error is retryable, RecognizeStream._main_task runs another attempt on the same object with the saved exception still set. An unexpected close without an Error frame on that new socket takes the saved exception instead of the current close code and reason.

Example: One socket sends a retryable 3005 server error. The replacement closes with 1001 and no Error frame. The caller sees the earlier 3005 error and its session ID rather than the replacement’s close details.

Recommended fix: Remove _last_error and the close-branch lookup. Keep immediate propagation of parsed Error frames and report the current socket’s close details when no Error frame arrives. Add a two-socket regression test with a retryable Error frame on the first socket and a bare unexpected close on the second.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

This branch has not been deployed

No deployments
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.

1 participant