jolly-eve-3916
07/30/2025, 5:38 AMtall-airline-16756
07/30/2025, 5:39 AMtall-airline-16756
07/30/2025, 5:40 AMsentence_tokenizer parameter like the deprecated VoicePipelineAgent. However, you can achieve similar functionality by customizing the tts_node to buffer text into complete sentences before sending it to TTS.
2. Customizing Text Chunking:
You can override the tts_node in your AgentSession to implement custom text buffering and sentence chunking. Below is an example:
from livekit.agents.voice import Agent
from livekit.agents import tokenize
class SentenceBufferedAgent(Agent):
async def tts_node(self, text: AsyncIterable[str], model_settings: ModelSettings):
async def process_with_sentence_buffering():
buffer = ""
sentence_tokenizer = tokenize.blingfire.SentenceTokenizer(
min_sentence_len=15,
retain_format=False
)
async for chunk in text:
buffer += chunk
# Tokenize accumulated text
sentences = sentence_tokenizer.tokenize(buffer)
# Yield complete sentences, keep incomplete text in buffer
if len(sentences) > 1:
for sentence in sentences[:-1]:
yield sentence + " "
buffer = sentences[-1] # Keep last incomplete sentence
# Yield any remaining text
if buffer.strip():
yield buffer
return Agent.default.tts_node(self, process_with_sentence_buffering(), model_settings)
This approach ensures that text is buffered into complete sentences before being sent to TTS, reducing unnatural pauses and improving speech naturalness.
3. Available Sentence Tokenizers:
You can use various sentence tokenizers, such as:
• Blingfire Tokenizer: Provides options for minimum sentence length, stream context length, and format retention.
• NLTK Tokenizer: Uses the punkt tokenizer with configurable parameters.
4. Migration to VoicePipelineAgent:
Since VoicePipelineAgent is deprecated, it is recommended to adapt your implementation to the new AgentSession architecture using the customization options mentioned above.
By implementing these changes, you can achieve natural-sounding TTS output with improved sentence handling.
Sources: Agents v0.x migration guide | LiveKit Docs | AgentTranscriptionOptions | LiveKit Agents | livekit-agents/livekit/agents/tokenize/blingfire.py | Voice pipeline nodes | LiveKit Docs | pipeline-tts/short_replies_only.py