text_to_speech.py
Run the Example
1
Set up your virtual environment
2
Install dependencies
3
Export your Google API key
4
Run the example
Save the code above as
text_to_speech.py, then run:Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Generate speech with a Gemini TTS model and write the audio to a WAV file.
"""
Google Text To Speech
=====================
Cookbook example for `google/gemini/text_to_speech.py`.
"""
from agno.agent import Agent
from agno.models.google import Gemini
from agno.utils.audio import write_wav_audio_to_file
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
model=Gemini(
id="gemini-2.5-flash-preview-tts",
response_modalities=["AUDIO"],
speech_config={
"voice_config": {"prebuilt_voice_config": {"voice_name": "Kore"}}
},
)
)
run_output = agent.run("Say cheerfully: Have a wonderful day!")
if run_output.response_audio is not None:
audio_data = run_output.response_audio.content
output_file = "tmp/cheerful_greeting.wav"
write_wav_audio_to_file(output_file, audio_data)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
pass
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activate
uv venv --python 3.12
.venv\Scripts\activate
Install dependencies
uv pip install -U agno google-genai
Export your Google API key
export GOOGLE_API_KEY="your_google_api_key_here"
$Env:GOOGLE_API_KEY="your_google_api_key_here"
Run the example
text_to_speech.py, then run:python text_to_speech.py
Was this page helpful?