nutritious-oyster-4341
08/27/2025, 10:03 PMtall-airline-16756
08/27/2025, 10:03 PMnutritious-oyster-4341
08/27/2025, 10:03 PMdef stt_node(
self, audio: AsyncIterable[rtc.AudioFrame], model_settings: ModelSettings
) -> AsyncIterable[SpeechEvent | str]:
"""Wrap STT to only drive inactivity timers.
When handoff is active, disable STT entirely to avoid usage costs.
"""
if self._handoff_active:
return silent_gen()
async def _gen():
speaking = False
audio_buffer: list[rtc.AudioFrame] = []
async for frame in audio:
audio_buffer.append(frame)
logger.debug("Audio buffer: {}", len(audio_buffer))
if not speaking:
yield stt.SpeechEvent(type=stt.SpeechEventType.START_OF_SPEECH)
speaking = True
wav_bytes = rtc.combine_audio_frames(audio_buffer).to_wav_bytes()
audio_buffer.clear()
deepgram_client = DeepgramClient(api_key=echo_config.DEEPGRAM_API_KEY)
res = deepgram_client.listen.prerecorded.v("1").transcribe_file(
source={"buffer": wav_bytes, "mimetype": "audio/wav"},
options=PrerecordedOptions(
model="nova-3",
detect_entities=True,
smart_format=True,
punctuate=True,
),
)
logger.debug(
"Deepgram response: {}",
res.results.channels[0].alternatives[0].transcript,
)
yield stt.SpeechEvent(
type=stt.SpeechEventType.FINAL_TRANSCRIPT,
alternatives=[
stt.SpeechData(
language="en-US",
text=res.results.channels[0].alternatives[0].transcript,
)
],
)
yield stt.SpeechEvent(
type=stt.SpeechEventType.END_OF_SPEECH,
)
speaking = False
return _gen()tall-airline-16756
08/27/2025, 10:03 PMasyncio.wait_for()) so your loop doesn’t get stuck waiting endlessly.
• Resource Management: Be sure to close the audio stream properly by calling aclose() when you’re done. This triggers a StopAsyncIteration and lets the loop finish gracefully.
• Task Cleanup: If you have background tasks handling audio capture or processing, remember to cancel them explicitly when your app is shutting down.
Take a look at these points in your code and see if any of them might be causing the issue. And hey, if you want to chat more or need a hand digging deeper, just let me know—I’m here to help!
Sources: examples/video-stream/audio_wave.py | terminal-client/client.py | Job lifecycle | LiveKit Docs | Audio and video | LiveKit Docs | stream_audio.py