Support Voice Notes
Instant Voice Cloning is in beta. This cookbook uses it for the spoken reply; inbound transcription still uses Saaras.
Overview
This cookbook builds a WhatsApp support bot whose spoken replies sound like a specific agent, not a stock speaker. It clones that agent once from a short reference clip, then on every inbound message it transcribes with Saaras, drafts a reply with a Sarvam chat model, and speaks the reply with Instant Voice Cloning.
The WhatsApp Bot guide is the same channel with a Bulbul speaker. Use this page when the reply has to be a named person’s voice.
What you’ll build:
- A saved cloned voice from a 10-15 second support-agent clip
- A Flask webhook that receives WhatsApp text and voice notes via Twilio
- Automatic transcription of inbound voice notes
- A short support reply from a Sarvam chat model
- A spoken reply in the cloned agent voice, attached as WhatsApp audio
Business Value
- Keep one familiar agent voice on a channel customers already have open
- Reply in Hindi, Tamil, or any supported language from a single English (or other) reference clip
- Avoid hiring per-language voice talent for routine status and FAQ replies
Customer Support
Collections & Reminders
Field Networks
A known agent’s voice confirms an order or walks through a refund, instead of a generic TTS speaker.
How It Works
Clone the agent once
Send a clean reference clip to voice_cloning.create_voice. Keep the returned voice_id — every later reply uses it.
Receive the message
Twilio POSTs every inbound WhatsApp message, text or voice note, to your Flask webhook.
Voice cloning requires consent. Only clone a voice you have the right to use. See Commercial Licensing.
1. Prerequisites
- Python 3.9 or higher
sarvamai≥0.1.37- A Sarvam API key from Key Management
- A 10-15 second, single-speaker clip of the support agent, saved as
reference.wav. See Prepare Reference Audio. - A Twilio account with the WhatsApp Sandbox (or a WhatsApp-enabled Sender)
- ngrok to expose the local server
This cookbook uses the free Twilio Sandbox for WhatsApp for testing. Production needs a registered WhatsApp Sender. See Twilio’s WhatsApp docs. For Sandbox join-code and webhook setup, follow WhatsApp Bot — the Twilio side is the same.
2. Install the SDK and Dependencies
3. Set Environment Variables
Load keys from the environment. If VOICE_ID is unset, the script clones reference.wav once at startup and prints the new id — save that value so later runs skip create.
4. The Full Bot Script
Copy this into support_voice_notes.py. It creates (or reuses) the agent voice, then handles every inbound WhatsApp message.
How it works
resolve_voice_idclones the agent fromreference.wavonce, unlessVOICE_IDis already set. Create is not on the per-message path.- Inbound voice notes download from Twilio and go to Saaras with
language_code="unknown", so a Hindi note and a Tamil note work without a code change. sarvam-105bdrafts a short reply in the customer’s language.voice_cloning.convertspeaks that reply with the saved agent voice. Synthesis has its owntry/except, so a cloning failure still leaves the customer with the text reply.language_codeis omitted on convert so the service detects the language from the reply. Pass the code when pronunciation has to be exact — see Supported languages.
WhatsApp only renders a native voice-note bubble for OGG/Opus. A .wav reply still plays; it shows as a regular audio attachment.
This script trusts every request to /whatsapp. Before production, validate X-Twilio-Signature with twilio.request_validator.RequestValidator.
5. Run It
Put reference.wav next to the script, point the Twilio Sandbox webhook at https://<ngrok-host>/whatsapp, then:
Send a text or a voice note to the Sandbox number. You should get a text reply and an audio attachment in the cloned agent’s voice.
6. Tips & Best Practices
- Reference clip: 10-15 seconds, one speaker, no music. The clone copies pace and emotion, not only timbre. See Prepare Reference Audio.
- Create once: Reuse
VOICE_ID. Re-uploading the clip on every process start repeats transcription and can hit the free-plan voice cap. - Reply length: Keep the system prompt explicit. WhatsApp bodies are capped at 1600 characters, and short replies clone more cleanly.
- One-shot vs stream: WhatsApp needs a finished file to attach, so this guide uses
convert. For a live voice agent, use HTTP streaming or WebSocket. - Sandbox opt-in: Recipients must send the Sandbox join code first. See WhatsApp Bot.
7. Error Handling
Retry 429, 500, 502, 503, and 504 with exponential backoff. Do not retry 400, 401, 402, 403, 404, or 422. Log request_id from failed Sarvam responses.
You may also see:
- 400 on clone — both
voice_idandref_audiowere sent, the clip is too long, or the reply is romanized Indic / Urdu. Keep native script in the chat prompt. - 402 — the subscription cannot generate this request.
- 403 — invalid API key.
- Twilio 63015 — the sender has not joined the Sandbox.
For the full table, see Errors & Troubleshooting and the Instant Voice Cloning FAQ.
8. Additional Resources
- API overview: Instant Voice Cloning
- Same channel, stock voice: WhatsApp Bot
- How-tos: Prepare Reference Audio · Clone Across Languages · HTTP Streaming
- Related APIs: Speech-to-Text · Chat Completion
- Twilio: Sandbox for WhatsApp
- Community: Join the Discord Community
9. Final Notes
- Keep API keys in environment variables.
- Only clone a voice you have the right to use.
- Create the agent voice once, then reuse
voice_idon every reply.
Keep Building! 🚀