Skip to content

fix(streaming): keep reading after a server message that fails to parse - #303

Open
mayankbohradev wants to merge 1 commit into
AssemblyAI:masterfrom
mayankbohradev:fix/streaming-reader-survives-bad-message
Open

mayankbohradev wants to merge 1 commit into
AssemblyAI:masterfrom
mayankbohradev:fix/streaming-reader-survives-bad-message

Conversation

@mayankbohradev

Copy link
Copy Markdown
Contributor

What

Both streaming clients now skip a server message they cannot read, and keep reading. Before, one such message ended the read thread or task with no Error event.

Why

_read_message (sync) and _read_loop (async) parsed each message with no guard. A pydantic ValidationError (for example one null in a Turn), a JSON value that is not an object, or a non-UTF-8 binary frame raised out of the loop. After that:

  • no later Turn or Termination was delivered;
  • no Error event fired and _stop_event stayed unset;
  • the write side kept sending audio.

Bad JSON and unknown event types were already skipped with a warning. This makes the other unreadable cases behave the same way.

How

  • One shared _read_event in _base.py decodes and parses a frame for both clients. No frame content makes it raise.
  • A frame that is not JSON, is not an object, has an unsupported type, or fails to parse is skipped with one warning.
  • An Error or Termination message that fails validation is not skipped. It is rebuilt from the fields that can be read, so the session still ends and on_error still fires.
  • The parse warning names the message type and the failing fields only. pydantic v2 puts the rejected input (for a Turn, the transcript) in the exception text, so that text is not logged.

Testing

  • 23 new tests. All fail on master and pass with the change.
  • Streaming tests pass on Python 3.9, 3.11, 3.12 and 3.13, on the pydantic 1.10 floor and on the websockets 11 floor.
  • Full unit suite: 1056 passed. test_a_forked_child_does_not_share_the_parents_tts_connections fails on macOS on master too.
  • ruff 0.3.5 and mypy 1.5.1 pass as CI runs them.

Notes for reviewers

  • The "Failed to decode message" and "Unsupported event type" warnings now come from the assemblyai.streaming.v3._base logger, because the code moved there.
  • A rebuilt Error event carries code=None when the code cannot be read. A rebuilt Termination carries None for a duration that cannot be read.
  • I could not make the live API send a malformed message. The tests drive the read loops with a fake socket.
  • Version bumped to 1.10.13.

A message the client could not read raised out of the read loop. That
ended the read thread or task with no Error event, and the write side
kept sending audio.

Both loops now call one shared reader that skips such a message with a
warning. An Error or Termination message that fails validation still
ends the session.

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