@staticmethod async def tts_node( ...
# ask-ai
a
@staticmethod async def tts_node( agent: Agent, text: AsyncIterable[str], model_settings: ModelSettings ) -> AsyncGenerator[rtc.AudioFrame, None]: """Default implementation for `Agent.tts_node`""" activity = agent._get_activity_or_raise() assert activity.tts is not None, "tts_node called but no TTS node is available" wrapped_tts = activity.tts if not activity.tts.capabilities.streaming: wrapped_tts = tts.StreamAdapter( tts=wrapped_tts, sentence_tokenizer=tokenize.basic.SentenceTokenizer(min_sentence_len=5, stream_context_len=5) ) conn_options = activity.session.conn_options.tts_conn_options async with wrapped_tts.stream(conn_options=conn_options) as stream: async def _forward_input() -> None: async for chunk in text: logger.info(f"This is chunk {chunk}") stream.push_text(chunk) stream.end_input() forward_task = asyncio.create_task(_forward_input()) try: async for ev in stream: yield ev.frame finally: await utils.aio.cancel_and_wait(forward_task) In here I have already received the partial chunk to be sent. But its not sending, does it wait for the next left chunk to be sent.
t
Thanks for reaching out, Aditya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
When you call
stream.push_text(chunk)
within
async for chunk in text
, the chunk is sent downstream for processing immediately rather than being buffered or delayed for future input. The processing flow is as follows: • The text chunk is immediately placed in an input channel as soon as
push_text
is called. • Tokenization and other processing steps occur as input is received, typically in real-time. • The chunk is streamed to the TTS service as soon as it's available, resulting in low-latency streaming. The
flush()
method is only used for explicitly marking segment boundaries, not for forcing delayed input to be sent. Your partial chunk should be processed and sent as soon as it is received by
push_text
. If you are observing a delay, the cause is likely in downstream tokenization or in the TTS implementation itself, but it is not a result of upstream buffering in
tts_node
or
push_text
itself. Sources: livekit-agents/livekit/agents/voice/generation.py | pipeline-tts/tts_node.py