Skip to content

fix(stt): support external AssemblyAI endpointing - #3066

Open
rosetta-livekit-bot[bot] wants to merge 1 commit into
mainfrom
port/assemblyai-external-endpointing
Open

rosetta-livekit-bot[bot] wants to merge 1 commit into
mainfrom
port/assemblyai-external-endpointing

Conversation

@rosetta-livekit-bot

@rosetta-livekit-bot rosetta-livekit-bot Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Ports livekit/agents#8610 to support AssemblyAI turn finalization through LiveKit Inference while the audio stream remains open. Adds external VAD or framework-driven endpointing, ordered audio/finalize writes, 50 ms padding for short chunks, corrected timestamps, and stream-level manual-flush propagation.

Source: livekit/agents#8610

Source diff coverage
  • livekit-agents/livekit/agents/inference/stt.py -> adapted to agents/src/inference/stt.ts; camelCase public options and the existing JS WebSocket/handoff writer are used for external endpointing, VAD handling, padding, timestamp correction, and frozen stream capabilities. The generated public API update is recorded in agents/etc/agents.api.md.
  • livekit-agents/livekit/agents/stt/fallback_adapter.py -> adapted to agents/src/stt/fallback_adapter.ts; the fallback stream delegates manual flush to its live child stream.
  • livekit-agents/livekit/agents/stt/stt.py -> adapted to agents/src/stt/stt.ts; adds the TypeScript SpeechStream.manualFlush contract.
  • livekit-agents/livekit/agents/voice/audio_recognition.py -> adapted to agents/src/voice/audio_recognition.ts; the pipeline reads manual-flush behavior from the live stream, with the existing registration capability fallback retained for structural custom-node streams.
  • tests/test_inference_stt.py -> adapted to agents/src/inference/stt.test.ts; ports the source constructor/VAD, live-stream capability, ordered finalize, short-audio padding, and timestamp regressions into the Vitest gateway harness.
  • tests/test_stt_fallback.py -> adapted to agents/src/stt/fallback_adapter.test.ts; ports the live-child manual-flush regression.
  • Not applicable: none. Every source file has a target counterpart and is covered.

Target-only release metadata: .changeset/assemblyai-external-endpointing.md adds a minor bump for the new public API.

Validation

  • pnpm test agents/src/inference/stt.test.ts agents/src/stt/fallback_adapter.test.ts agents/src/voice/audio_recognition_manual_flush.test.ts (163 passed, 1 skipped)
  • pnpm build:agents
  • pnpm --filter @livekit/agents lint
  • pnpm --filter @livekit/agents typecheck
  • pnpm --filter @livekit/agents api:check
  • Full pnpm test agents and serial pnpm test agents --maxWorkers=1 were run. Both reached 5,111 passing tests and exposed unrelated timing flakes in inference/tts, tcp_session_transport, and once telemetry/loop_monitor; all failing files pass together when rerun in isolation (112 passed, 1 skipped).

Runtime cue-cli validation could not run because cue-cli and LiveKit credentials are unavailable in this workspace. The ported gateway and voice manual-flush tests validate the affected framework behavior locally.


Ported from livekit/agents#8610

Original PR description

Summary

AssemblyAI can hold a turn open for a client endpoint, but Inference STT previously sent session.finalize only when input ended. With external_endpointing=True, AgentSession turn detection or a direct flush() can finalize each turn while the audio stream stays open. A supplied VAD can drive finalization instead.

The audio writer sends pending audio before finalize and pads a short flushed chunk to AssemblyAI’s 50 ms minimum. Select the provider’s turn detection separately, for example extra_kwargs={"mode": "manual"} on U3.7 Realtime Agent.

Validation

  • make check passed.
  • The unit and audio EOT run passed 6,494 tests. All nine room tests that needed a local LiveKit Server passed on rerun.
  • The patched SDK sent three per-turn finalizes on one socket through a local gateway for both U3.6 and U3.7 using a controlled upstream.
  • Live AssemblyAI benchmark through the SDK and local gateway completed all 60 sessions. The SDK sent 180 per-turn finalizes in audio order. Benchmark results on agent-gateway #2055.

Related gateway investigation and benchmark: livekit/agent-gateway#2055.

@rosetta-livekit-bot
rosetta-livekit-bot Bot requested a review from a team as a code owner November 24, 2026 21:05
@changeset-bot

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0863e02

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 42 packages
Name Type
@livekit/agents Minor
@livekit/agents-plugin-anam Minor
@livekit/agents-plugin-anthropic Minor
@livekit/agents-plugin-assemblyai Minor
@livekit/agents-plugin-azure Minor
@livekit/agents-plugin-baseten Minor
@livekit/agents-plugin-bey Minor
@livekit/agents-plugin-cartesia Minor
@livekit/agents-plugin-cerebras Minor
@livekit/agents-plugin-deepgram Minor
@livekit/agents-plugin-did Minor
@livekit/agents-plugin-elevenlabs Minor
@livekit/agents-plugin-fishaudio Minor
@livekit/agents-plugin-google Minor
@livekit/agents-plugin-hume Minor
@livekit/agents-plugin-inworld Minor
@livekit/agents-plugin-krisp Minor
@livekit/agents-plugin-lemonslice Minor
@livekit/agents-plugin-liveavatar Minor
@livekit/agents-plugin-livekit Minor
@livekit/agents-plugin-meta Minor
@livekit/agents-plugin-minimax Minor
@livekit/agents-plugin-mistral Minor
@livekit/agents-plugin-mistralai Minor
@livekit/agents-plugin-neuphonic Minor
@livekit/agents-plugin-nvidia Minor
@livekit/agents-plugin-openai Minor
@livekit/agents-plugin-perplexity Minor
@livekit/agents-plugin-phonic Minor
@livekit/agents-plugin-protoface Minor
@livekit/agents-plugin-resemble Minor
@livekit/agents-plugin-rime Minor
@livekit/agents-plugin-runway Minor
@livekit/agents-plugin-sarvam Minor
@livekit/agents-plugin-silero Minor
@livekit/agents-plugin-soniox Minor
@livekit/agents-plugin-speechify Minor
@livekit/agents-plugin-synthesia Minor
@livekit/agents-plugin-tavus Minor
@livekit/agents-plugins-test Minor
@livekit/agents-plugin-trugen Minor
@livekit/agents-plugin-xai Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@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.

3 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

}

vadStream?.endInput();
for (const frame of audioStream.flush()) sendAudioFrame(frame);

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.

🔴 Partial audio aborts final transcription

With external VAD enabled, send closes the detector before sending any audio left in the 50 ms buffer. The send callback pushes that audio into the closed detector and throws. AssemblyAI never receives the finalization request.

Learn more

The stream buffers incoming audio into 50 ms chunks. With external AssemblyAI endpointing, prepareAudioFrame feeds each chunk to the voice activity detector only when the writer sends it. The final partial chunk takes that same path after VADStream.endInput has marked the detector input closed. VADStream.pushFrame then throws synchronously, so the writer never sends the final request.

Example: Push three 20 ms frames into an AssemblyAI stream configured with an external VAD, then call endInput(). The writer sends 50 ms normally. Sending the padded 10 ms remainder calls the closed VAD, so the final transcript is not requested.

Recommended fix: Keep the detector input open until all remaining audio callbacks have run. Flush the tail before ending the detector on the immediate-write path, and defer detector EOF until buffered writes replay on the handoff path.

Devin Review


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

vad: VAD | undefined,
externalEndpointing = false,
): VAD | undefined {
if (externalEndpointing && isAssemblyAIModel(model)) return vad;

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.

🔴 External VAD blocks stream completion

After endInput() on an external-VAD stream, run waits for processVAD before closing the detector. Silero leaves its event output open at input EOF. Transcript consumers never receive EOF, even after the gateway closes.

Learn more

This option lets AssemblyAI streams use a local voice activity detector such as Silero. VADStream.endInput closes only the detector input, and InferenceVADStream exits its inference loop without closing the event output when that input ends. processVAD remains blocked reading those events until canceled or until the output closes. finishFinalization cleans up the WebSocket and server-event channel, but run waits for the detector task before it cancels and closes the detector. The base stream closes its transcript queue only once that attempt returns.

Example: Use the bundled Silero VAD with external AssemblyAI endpointing, send an exact multiple of 50 ms, and call endInput(). A for await consumer receives the final transcript but waits forever for the stream to finish unless it explicitly closes it.

Recommended fix: Give the detector reader its own cancellation signal and cancel it when terminal finalization begins, before joining the tasks. Keep transcript reception independent so trailing provider events can drain, and test that a VAD whose output stays open at input EOF does not prevent transcript EOF.

Devin Review


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

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.

0 participants