Master voice-enabled AI agents with the OpenAI Agents SDK! This tutorial demonstrates how to build conversational voice agents using speech-to-text, text-to-speech, and intelligent agent workflows for natural voice interactions.
Sign in to save downloads to your library and vote.
Preview
๐๏ธ Tutorial 11: Voice Agents
Master voice-enabled AI agents with the OpenAI Agents SDK! This tutorial demonstrates how to build conversational voice agents using speech-to-text, text-to-speech, and intelligent agent workflows for natural voice interactions.
๐ฏ What You'll Learn
- Voice Pipeline Architecture: Complete speech โ text โ speech workflow
- Static Voice Processing: Turn-based voice interaction with recorded audio
- Streaming Voice Processing: Real-time voice conversation with live audio
- Multi-Language Support: Automatic language detection and agent handoffs
- Voice-Optimized Tools: Design tools specifically for voice interactions
- Audio Management: Recording, playback, and streaming audio utilities
๐ง Core Concept: Voice Agents
Voice agents combine the power of AI language models with speech processing to create natural conversational interfaces. Think of voice agents as AI assistants you can talk to naturally that:
- Listen to your speech and convert it to text
- Process your request with intelligent AI agents
- Use tools and make decisions like text-based agents
- Convert responses back to natural speech
- Handle multi-turn conversations seamlessly
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ VOICE AGENT SYSTEM โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ ๐ค USER SPEECH โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโ 1. SPEECH-TO-TEXT โ
โ โ AUDIO โ โฆ Convert speech to text โ
โ โ PIPELINE โ โฆ Handle multiple languages โ
โ โโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโ 2. AGENT PROCESSING โ
โ โ AGENT โ โฆ Multi-agent workflows โ
โ โ ECOSYSTEM โ โฆ Tool calling & handoffs โ
โ โโโโโโโโโโโโโโโ โฆ Context management โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโ 3. TEXT-TO-SPEECH โ
โ โ SPEECH โ โฆ Convert response to speech โ
โ โ SYNTHESIS โ โฆ Natural voice output โ
โ โโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ ๐ AI RESPONSE โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Tutorial Overview
This tutorial demonstrates three core voice interaction patterns:
1. Static Voice Processing (static/)
- Turn-based interaction: Record โ Process โ Respond
- Complete audio processing: Full utterance before processing
- Simpler implementation: Easier to understand and debug
- Best for: Voice commands, structured interactions
2. Streaming Voice Processing (streamed/)
- Real-time interaction: Continuous listening and responding
- Live audio streaming: Process audio as it arrives
- Activity detection: Automatic speech start/stop detection
- Best for: Natural conversations, voice assistants
3. Realtime Voice Processing (realtime/)
- Ultra-low latency: WebSocket-based persistent connections
- Interruption handling: Natural conversation interruptions
- Realtime API: OpenAI's newest voice technology
- Best for: Live conversations, minimal latency requirements
๐ Project Structure
11_voice/
โโโ README.md # This file - voice agents overview
โโโ static/ # Static voice processing example
โ โโโ agent.py # Complete static voice agent
โ โโโ util.py # Audio recording and playback utilities
โ โโโ requirements.txt # Dependencies for static example
โ โโโ env.example # Environment variables
โ โโโ README.md # Static voice documentation
โโโ streamed/ # Streaming voice processing example
โ โโโ agent.py # Real-time streaming voice agent
โ โโโ util.py # Streaming audio utilities
โ โโโ requirements.txt # Dependencies for streaming example
โ โโโ env.example # Environment variables
โ โโโ README.md # Streaming voice documentation
โโโ realtime/ # Realtime voice processing example
โ โโโ agent.py # Basic realtime voice agent
โ โโโ requirements.txt # Dependencies for realtime example
โ โโโ env.example # Environment variables
โ โโโ README.md # Realtime voice documentation
โโโ __init__.py # Module initialization
๐ฏ Learning Objectives
By the end of this tutorial, you'll understand:
- โ How to build complete voice interaction pipelines
- โ The difference between static and streaming voice processing
- โ How to implement multi-language voice agents with handoffs
- โ Best practices for voice-optimized agent design
- โ Real-time audio processing and streaming techniques
๐ Getting Started
Prerequisites
-
Install OpenAI Agents SDK with voice support:
pip install 'openai-agents[voice]' -
Install audio dependencies:
pip install sounddevice numpy soundfile librosa -
Set up environment variables:
cp static/env.example static/.env cp streamed/env.example streamed/.env # Edit .env files and add your OpenAI API key
Quick Start Options
Option 1: Static Voice (Recommended for beginners)
cd static/
python agent.py
Option 2: Streaming Voice (Advanced)
cd streamed/
python agent.py
Option 3: Realtime Voice (Ultra-low latency)
cd realtime/
python agent.py
๐งช Voice Agent Capabilities
Multi-Language Support
Both examples include:
- English Agent: Primary assistant with full tool access
- Spanish Agent: Specialized Spanish-speaking assistant
- French Agent: Specialized French-speaking assistant
- Automatic Language Detection: Seamless handoffs based on detected language
Voice-Optimized Tools
get_weather(city): Weather information with voice-friendly responsesget_time(): Current time with natural speech outputcalculate_tip(bill, percentage): Tip calculations for voice queriesset_reminder(message, minutes): Voice-activated reminders (streaming only)get_news_summary(): Voice-friendly news updates (streaming only)
Audio Processing Features
- High-Quality Recording: 24kHz audio capture
- Real-Time Playback: Low-latency audio output
- Activity Detection: Automatic speech boundary detection (streaming)
- Error Recovery: Robust audio pipeline error handling
๐ง Key Voice Agent Patterns
1. Basic Voice Pipeline
from agents.voice import VoicePipeline, SingleAgentVoiceWorkflow
pipeline = VoicePipeline(
workflow=SingleAgentVoiceWorkflow(agent)
)
2. Static Audio Processing
from agents.voice import AudioInput
audio_buffer = record_audio(duration=5.0)
audio_input = AudioInput(buffer=audio_buffer)
result = await pipeline.run(audio_input)
3. Streaming Audio Processing
from agents.voice import StreamedAudioInput
streamed_input = StreamedAudioInput()
result = await pipeline.run(streamed_input)
# Push audio chunks in real-time
streamed_input.push_audio(audio_chunk)
4. Multi-Language Agent Setup
spanish_agent = Agent(
name="Spanish",
handoff_description="A spanish speaking agent.",
instructions="Speak in Spanish only..."
)
main_agent = Agent(
name="Assistant",
handoffs=[spanish_agent, french_agent],
instructions="If user speaks Spanish, handoff to Spanish agent..."
)
๐ก Voice Agent Best Practices
Agent Design for Voice
- Concise Instructions: Voice interactions work best with brief instructions
- Conversational Responses: Design for natural speech patterns
- Clear Tool Descriptions: Voice-friendly tool naming and descriptions
- Language Handling: Implement clear language detection logic
Audio Quality
- Good Hardware: Use quality microphones and speakers
- Noise Reduction: Minimize background noise during recording
- Audio Levels: Ensure appropriate input/output volume levels
- Latency Optimization: Configure audio buffers for minimal delay
Error Handling
- Graceful Failures: Handle audio device failures gracefully
- Network Issues: Implement retry logic for API calls
- User Interruptions: Allow clean exit from voice sessions
- Resource Cleanup: Properly close audio streams and resources
๐งช Example Voice Interactions
English Conversations
- "Tell me a joke" โ Humorous response
- "What's the weather in London?" โ Weather tool call
- "What time is it?" โ Current time
- "Calculate a 18% tip on a $75 bill" โ Tip calculation
Language Switching
- "Hola, ยฟquรฉ tiempo hace en Madrid?" โ Spanish agent response
- "Bonjour, quelle heure est-il?" โ French agent response
- Seamless language detection and agent handoffs
Multi-Turn Conversations (Streaming)
- Natural back-and-forth dialogue
- Context preservation across turns
- Tool usage within conversations
๐ Static vs Streaming Comparison
| Feature | Static Voice | Streaming Voice |
|---|---|---|
| Processing | Turn-based | Real-time |
| Complexity | Simpler | More complex |
| Latency | Higher | Lower |
| Use Cases | Commands, queries | Conversations |
| Activity Detection | Manual | Automatic |
| Resource Usage | Lower | Higher |
| User Experience | Structured | Natural |
๐จ Requirements & Dependencies
Core Dependencies
openai-agents[voice]: Voice-enabled Agents SDKsounddevice: Real-time audio I/Onumpy: Audio data processingsoundfile: Audio file operations (optional)librosa: Audio resampling (optional)
System Requirements
- Python 3.8+: Required for async support
- Audio Hardware: Microphone and speakers/headphones
- Processing Power: Sufficient CPU for real-time audio processing
- Network: Stable internet for OpenAI API calls
๐ Related Documentation
- Voice Quickstart: Official voice agent setup guide
- Voice Pipelines: Advanced pipeline configuration
- Agent Fundamentals: Basic agent concepts
- Multi-Agent Systems: Agent handoffs and orchestration
๐จ Troubleshooting
Audio Issues
- No microphone input: Check audio device permissions and settings
- Poor audio quality: Verify microphone levels and background noise
- Playback problems: Test speaker/headphone configuration
- Latency issues: Optimize audio buffer sizes
Voice Pipeline Issues
- Transcription errors: Ensure clear speech and good audio quality
- Agent responses: Verify API keys and network connectivity
- Language detection: Test with clear language examples
- Handoff failures: Check agent instructions and handoff logic
Performance Issues
- High CPU usage: Monitor real-time processing load
- Memory leaks: Ensure proper cleanup of audio streams
- Network timeouts: Implement retry logic for API calls
- Resource conflicts: Check for audio device conflicts
๐ก Pro Tips
- Start with Static: Master static voice processing before attempting streaming
- Test Audio Setup: Verify hardware configuration before development
- Monitor Debug Output: Use callbacks to understand pipeline behavior
- Optimize for Voice: Design agents specifically for conversational interaction
- Handle Edge Cases: Plan for network issues, audio failures, and user interruptions
๐ Next Steps
After mastering voice agents:
- Production Deployment: Scale voice agents for real-world applications
- Custom Voice Models: Integrate specialized speech recognition/synthesis
- Multi-Modal Agents: Combine voice with vision and text capabilities
- Enterprise Voice Solutions: Build robust voice applications for business use
Ingestion metadata
- Source catalog
- awesome-llm-apps
- Repository
- Shubhamsaboo/awesome-llm-apps ยท main
- File path
- ai_agent_framework_crash_course/openai_sdk_crash_course/11_voice/README.md
- Last refreshed
- 7/24/2026, 3:00:13 AM (53m ago)
- Refresh schedule
- Daily ยท 03:00 UTC
- Dedupe status
- Unique ยท deduped by (source, url)