feat(soniox): add real-time translation support and rewrite SpeechStream#5111
Open
MSameerAbbas wants to merge 1 commit intolivekit:mainfrom
Open
feat(soniox): add real-time translation support and rewrite SpeechStream#5111MSameerAbbas wants to merge 1 commit intolivekit:mainfrom
MSameerAbbas wants to merge 1 commit intolivekit:mainfrom
Conversation
Rewrite the Soniox STT plugin to support all WebSocket API features and fix structural issues in the streaming implementation. New features: - Real-time translation (one-way and two-way) via TranslationConfig - Configurable max_endpoint_delay_ms (500-3000ms) - Typed Literal autocomplete for models, languages, and translation type - Flush sentinel mapped to FINALIZE_MSG for clean session shutdown Structural fixes: - Remove dead reconnect machinery (_reconnect_event was never set) - Eliminate unnecessary intermediate audio queue (2 tasks -> 1) - Pass ws as parameter to subtasks instead of mutable self._ws - Single connection lifecycle in _run(); base class handles retries - Proper error semantics (5xx -> APIConnectionError, 4xx -> APIStatusError) - Raise on unexpected WS closure instead of silent hang - Handle _FlushSentinel (was silently dropped) - Remove unreachable except clause Translation design: - alternatives[0] = original text (always present) - alternatives[1] = translated text (when translation is enabled) - Fully backward-compatible: all consumers read alternatives[0] - Dict-keyed accumulators with no special cases Refs: livekit#4943
Contributor
Author
|
Hey @tinalenguyen, I saw this was assigned to you - hope it's helpful! Would love your review. |
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.
Summary
Adds real-time translation support to the Soniox STT plugin (#4943), along with a cleanup of
SpeechStreamto align with the patterns used by other plugins like Deepgram.New features
TranslationConfigdataclass. Translations surface asalternatives[1]onSpeechEvent, fully backward-compatible since all consumers only readalternatives[0].max_endpoint_delay_msparameter (500-3000ms) for tuning endpoint detection latency.models.pywithLiteraltype aliases (SonioxRTModels,SonioxLanguages) for IDE autocomplete -- follows the same pattern as the Google STT plugin._FlushSentinelis now mapped to Soniox's documented end-of-stream signal for clean session shutdown. Previously it was not handled.SpeechStream cleanup
While adding translation, I noticed a few things in
SpeechStreamthat could be simplified to match how other plugins (Deepgram, Google) structure their streaming:_run()that connects, runs tasks, and cleans up. The base class_main_task()already handles retry logic, so the plugin doesn't need its own retry loop.audio_queuebetween_prepare_audio_taskand_send_audio_taskwas consolidated into a single_send_taskthat reads_input_chdirectly.wsas parameter: Subtasks receive the WebSocket as a parameter rather than readingself._ws, similar to how the Deepgram plugin passes connection state.APIConnectionError(5xx) orAPIStatusError(4xx) so the base class can decide whether to retry. Unexpected WebSocket closure raises instead of silently returning.Translation design
When translation is enabled, Soniox returns tokens with a
translation_statusfield. The plugin routes tokens into dict-keyed accumulators:translation_statusin ("none","original", absent) ->final["original"]translation_status == "translation"->final["translation"]At endpoint:
alternatives[0]= original text,alternatives[1]= translation (if present). When translation is off, all tokens route to"original"and the event has a single alternative -- identical to the previous behavior. One code path handles both cases.What was NOT changed
_TokenAccumulatorclass -- already clean, kept as-is.STTclass -- kept as-is.STTOptionsdefaults preserved (model, sample_rate, num_channels, etc.).ContextObject,ContextGeneralItem,ContextTranslationTerm) -- unchanged.Files changed
livekit-plugins/livekit-plugins-soniox/livekit/plugins/soniox/stt.py--SpeechStreamrewrite, addedTranslationConfig+STTOptionsfieldslivekit-plugins/livekit-plugins-soniox/livekit/plugins/soniox/__init__.py-- exportTranslationConfig,SonioxLanguages,SonioxRTModelslivekit-plugins/livekit-plugins-soniox/livekit/plugins/soniox/models.py-- new file withLiteraltype aliasesTest plan
alternatives[0]andalternatives[1]max_endpoint_delay_ms-- verified API accepts the parameterRefs: #4943