TwilioRishab KumarTutorial
How to Build a Gemini Live Voice Agent with Twilio Agent Connect and Python
Summary
Phone audio differs from local microphone audio, requiring a translation layer between Twilio and Gemini Live. This tutorial builds a Python appointment concierge that connects Twilio Programmable Voice to the Gemini Live API through a custom Twilio Agent Connect provider. The provider converts 8 kHz μ-law phone audio to 16 kHz PCM for Gemini and 24 kHz Gemini output back to 8 kHz μ-law for Twilio. The agent handles interruptions, calls Python tools for appointment availability and booking, streams a live transcript, and supports outbound calls. The project uses twilio-agent-connect[server] 2.4, the google-genai Python SDK, FastAPI, and the gemini-3.8-live model as of October 2026.
Context
Phone audio differs from local microphone audio; Twilio Media Streams sends base64-encoded 8 kHz μ-law, while Gemini Live expects 16 kHz PCM input and outputs 24 kHz PCM. A custom provider bridges these formats and manages per-call state.
Approach / What changed
Build a Python appointment concierge that connects Twilio Programmable Voice to the Gemini Live API through a custom Twilio Agent Connect provider. The provider translates 8 kHz μ-law phone audio to 16 kHz PCM for Gemini and 24 kHz Gemini output back to 8 kHz μ-law for Twilio, handles interruptions, executes Python tools, streams a transcript, and supports outbound calls.
Takeaways
Twilio Media Streams sends phone audio as base64-encoded 8 kHz μ-law; Gemini Live expects 16 kHz PCM input and outputs 24 kHz PCM, requiring a custom audio bridge.
The custom GeminiLiveProvider extends TAC’s MediaStreamsProviderConfig and creates one Gemini Live session and one audio bridge per call to prevent state leakage between callers.
The same provider supports outbound calls by reusing the audio bridge and tools; pass an E.164-formatted destination with the –to flag to initiate an outbound conversation.
FAQ
Why does the phone ring but I hear silence?
Confirm the webhook uses POST, the public hostname in .env matches the active tunnel, and welcome_instruction is configured. Open /health through the public URL to confirm the tunnel can reach the app.
Why does the WebSocket connect and immediately close?
Inspect the server log for a media-format validation error. Twilio Media Streams should report audio/x-mulaw, an 8,000 Hz sample rate, and one channel.
Why does audio sound choppy or too fast?
Make sure μ-law bytes are decoded before resampling, keep the sample-rate converter state between chunks, and send complete 20-millisecond output frames. Treating μ-law as PCM produces noise rather than speech.
Source and freshness
- Source
- twilio.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.