Text Chunking for TTS

Basic techniques for breaking text into chunks to reduce latency in Text-to-Speech applications.

Why Text Chunking Matters

Text chunking significantly reduces perceived latency in TTS applications by allowing audio playback to begin sooner. This is especially important for conversational AI and voice agents where responsiveness is critical.

Using Flux TTS (/v2/speak)? This guidance applies to Aura (/v1/speak). On Flux TTS, the server places flush boundaries internally — stream text in as it’s produced and don’t chunk client-side. See Getting Started with Flux TTS.

Instead of waiting for the entire audio to be generated, chunking lets you:

  • Begin audio playback much faster
  • Create more responsive voice experiences
  • Maintain natural-sounding speech

Basic Sentence Chunking

The simplest and most effective approach is to split text at sentence boundaries. This preserves natural speech patterns while enabling faster time-to-first-byte:

# For more Python SDK migration guides, visit:
# https://github.com/deepgram/deepgram-python-sdk/tree/main/docs
import re
def chunk_by_sentence(text):
# Split text at sentence boundaries (periods, question marks, exclamation points)
# while preserving the punctuation
sentences = re.split(r'(?<=[.!?])\s+', text)
# Remove any empty chunks
return [sentence for sentence in sentences if sentence]
# Example usage
text = "Hello, welcome to Deepgram. This is an example of text chunking. How does it sound?"
chunks = chunk_by_sentence(text)
for i, chunk in enumerate(chunks):
print(f"Chunk {i+1}: {chunk}")
# Output:
# Chunk 1: Hello, welcome to Deepgram.
# Chunk 2: This is an example of text chunking.
# Chunk 3: How does it sound?

Processing Streaming Text with REST TTS

When working with streaming text from an LLM, collect tokens until you have complete sentences. This example sends each sentence to REST TTS and saves its completed MP3 response.

# For more Python SDK migration guides, visit:
# https://github.com/deepgram/deepgram-python-sdk/tree/main/docs
import re
import asyncio
from deepgram import AsyncDeepgramClient
class SimpleTextChunker:
def __init__(self, deepgram_client):
self.queue = [] # Queue to store incoming paragraph chunks
self.deepgram_client = deepgram_client
self.chunk_number = 0
self.pending_text = ""
async def synthesize_and_save(self, text):
audio_response = self.deepgram_client.speak.v1.audio.generate(
text=text,
model="aura-2-thalia-en"
)
audio_data = b"".join([chunk async for chunk in audio_response])
self.chunk_number += 1
filename = f"chunk_{self.chunk_number}.mp3"
with open(filename, "wb") as audio_file:
audio_file.write(audio_data)
print(f"Audio saved to {filename}")
async def process_text_stream(self, paragraph):
"""Process an array of paragraph chunks, each containing 1-2 sentences"""
# Queue paragraph as it arrives (simulating fast reception)
self.queue.append(paragraph)
print(f"Received and queued paragraph: {paragraph}")
# You could preprocess paragraphs here and split them by more than just sentence boundaries
# Process the queue
while self.queue:
# Get the next paragraph from the queue
paragraph = self.queue.pop(0)
self.pending_text += paragraph
matches = list(re.finditer(r'[^.!?]+[.!?]', self.pending_text))
if not matches:
continue
self.pending_text = self.pending_text[matches[-1].end():].lstrip()
# Process each sentence
for match in matches:
sentence = match.group().strip()
# Send the sentence to TTS
print(f"Sending sentence to TTS: {sentence}")
await self.synthesize_and_save(sentence)
async def flush(self):
if self.pending_text.strip():
await self.synthesize_and_save(self.pending_text.strip())
self.pending_text = ""
# Example usage with an array of paragraph chunks
async def main():
# This simulates text coming in as paragraph chunks from an LLM
paragraph_chunks = [
"Hello",
" world. Deepgram's TTS API offers low latency.",
"It works great for voice agents.",
"This approach simulates receiving chunks as paragraphs. Each paragraph may contain one or two sentences.",
"Try it today! You'll be impressed with the results."
]
# Set up TTS client
deepgram = AsyncDeepgramClient()
chunker = SimpleTextChunker(deepgram)
# Process each paragraph sequentially
for paragraph in paragraph_chunks:
await chunker.process_text_stream(paragraph)
await chunker.flush()
# Run the example
if __name__ == "__main__":
asyncio.run(main())

For low-latency playback over a persistent connection, see Real-Time TTS with WebSockets.

Processing Chunked Text

After creating chunks, you have two main options for processing them:

Sequential Processing

Process each chunk in sequence, prioritizing the first chunk:

# For more Python SDK migration guides, visit:
# https://github.com/deepgram/deepgram-python-sdk/tree/main/docs
async def process_chunks_sequential(chunks, tts_function):
results = []
for i, chunk in enumerate(chunks):
# You might prioritize the first chunk for faster response
result = await tts_function(chunk)
results.append(result)
return results

Setting Chunk Size

For most applications, sentences work well as chunks. If you need finer control:

  • Voice assistants: Aim for 50-100 character chunks
  • Call center bots: Use complete sentences (most natural)
  • Long-form content: Larger chunks (200-400 characters) preserve intonation

Other Chunking Strategies

If you need more advanced chunking methods, search for these techniques:

  • Clause-based chunking: Splits long sentences at commas and semicolons
  • NLP-based chunking: Uses natural language processing to find semantic boundaries
  • Adaptive chunking: Adjusts chunk size based on content complexity
  • First-chunk optimization: Specially optimizes the first chunk for minimal latency
  • SSML chunking: Handles Speech Synthesis Markup Language tags when chunking

For WebSocket implementation details to stream the chunked audio, see our guide on Real-Time TTS with WebSockets.