Enable Real-Time Speech Playback with Streaming TTS
Eliminate playback delay by enabling streaming for speech generation.
Enable streaming in the Gemini API using 'streamGenerateContent' to provide immediate audio feedback in voice-based applications.
The Scenario
You are designing a voice-enabled virtual assistant that needs to respond to user questions with near-zero latency.
Before & after
Waiting for an entire audio file to generate before playback often creates a latency gap of 10-15 seconds, frustrating users.
Switch to gemini-3.1-flash-tts-preview and enable 'stream: true' in your API request to begin playing audio within 1-2 seconds of the request.
The Prompt
Update my current Gemini API implementation to use 'gemini-3.1-flash-tts-preview' with streaming enabled. Provide the Python code to handle the 'streamGenerateContent' output.
Streaming support for speech generation via streamGenerateContent ensures that the text-to-speech process feels natural and immediate, which is critical for voice assistants and interactive agents.
Source
Release notes | Gemini API | Google AI for Developers"Streaming via streamGenerateContent (and stream: true in the Interactions API) is now supported for the gemini-3.1-flash-tts-preview model."
