A real-time voice interaction example using the OpenAI Agents SDK with continuous audio streaming. This demonstrates advanced voice pipeline capabilities with live speech detection, real-time processing, and turn-based conversation management.
Sign in to save downloads to your library and vote.
Preview
π Streaming Voice Agent
A real-time voice interaction example using the OpenAI Agents SDK with continuous audio streaming. This demonstrates advanced voice pipeline capabilities with live speech detection, real-time processing, and turn-based conversation management.
π― What This Demonstrates
- Real-Time Audio Processing: Continuous audio input and output streaming
- Activity Detection: Automatic detection of speech start/stop
- Turn Management: Intelligent conversation turn handling
- Live Agent Processing: Real-time agent responses during conversation
- Interruption Handling: Lifecycle events for managing conversation flow
- Streaming Callbacks: Real-time monitoring and debugging
π§ Core Concept: Streaming Voice Pipeline
The streaming voice pipeline processes audio continuously in real-time. Think of it as a live conversation assistant that:
- Continuously listens for audio input
- Automatically detects when you start and stop speaking
- Processes speech in real-time with AI agents
- Streams responses back as they're generated
- Manages conversation turns automatically
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STREAMING VOICE WORKFLOW β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β π€ CONTINUOUS AUDIO INPUT β
β β β
β βΌ β
β βββββββββββββββ 1. REAL-TIME CAPTURE β
β β STREAMING β β¦ Continuous microphone input β
β β AUDIO β β¦ Chunk-based processing β
β β RECORDER β β¦ Activity detection β
β βββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββ 2. LIVE TRANSCRIPTION β
β β STREAMING β β¦ Real-time speech-to-text β
β βTRANSCRIPTIONβ β¦ Turn boundary detection β
β βββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββ 3. CONCURRENT PROCESSING β
β β PARALLEL β β¦ Agent workflow execution β
β β AGENT EXEC β β¦ Tool calls & handoffs β
β βββββββββββββββ β¦ Multiple turns in session β
β β β
β βΌ β
β βββββββββββββββ 4. STREAMING RESPONSE β
β β LIVE TTS β β¦ Real-time text-to-speech β
β β PLAYBACK β β¦ Chunked audio output β
β βββββββββββββββ β¦ Immediate response playback β
β β β
β βΌ β
β π CONTINUOUS AUDIO OUTPUT β
β β
β βΊ LOOP FOR MULTIPLE TURNS β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Quick Start
-
Install voice dependencies:
pip install 'openai-agents[voice]' pip install sounddevice numpy soundfile librosa -
Set up environment:
cp env.example .env # Edit .env and add your OpenAI API key -
Run the streaming voice agent:
python agent.py -
Start talking: The agent will automatically detect when you speak and respond in real-time!
π§ͺ What This Example Includes
Real-Time Audio Management
- StreamedAudioRecorder: Continuous microphone input with threading
- AudioPlayer: Real-time audio playback with stream management
- Activity Detection: Automatic speech start/stop detection
- Turn-Based Processing: Intelligent conversation management
Advanced Agent Capabilities
- Multi-Language Support: English, Spanish, and French agents
- Enhanced Tools: Weather, time, reminders, and news
- Real-Time Handoffs: Language detection during streaming
- Session Management: Multi-turn conversation tracking
Streaming Tools
get_weather(city): Real-time weather informationget_time(): Current time with live updatesset_reminder(message, minutes): Demo reminder functionalityget_news_summary(): Mock news updates
Advanced Monitoring
- StreamingWorkflowCallbacks: Real-time event monitoring
- VoiceSessionManager: Session lifecycle management
- Turn Tracking: Conversation analytics and statistics
- Lifecycle Events: Turn start/end event handling
π― Example Interactions
Natural Conversation Flow
- Start speaking β Agent automatically detects speech
- Pause β Agent processes and responds immediately
- Continue talking β New turn begins automatically
- Multiple turns in single session
Real-Time Tool Usage
- "What's the weather in New York?" β Immediate weather response
- "What time is it?" β Live time information
- "Set a reminder to call Sarah in 15 minutes" β Reminder confirmation
- "Give me a news summary" β Current news update
Live Language Switching
- Speak in English β English agent responds
- Switch to "ΒΏQuΓ© tiempo hace en Madrid?" β Spanish agent takes over
- Switch to "Quelle heure est-il?" β French agent responds
- Seamless language detection and handoffs
π§ Key Implementation Patterns
1. Streaming Pipeline Setup
pipeline = VoicePipeline(
workflow=SingleAgentVoiceWorkflow(agent, callbacks=StreamingWorkflowCallbacks())
)
2. Continuous Audio Input
with StreamedAudioRecorder() as recorder:
streamed_input = StreamedAudioInput()
while session_active:
if recorder.has_audio():
audio_chunk = recorder.get_audio_chunk()
streamed_input.push_audio(audio_chunk)
3. Real-Time Audio Output
with AudioPlayer() as player:
async for event in result.stream():
if event.type == "voice_stream_event_audio":
player.add_audio(event.data)
elif event.type == "voice_stream_event_lifecycle":
handle_turn_events(event)
4. Session Management
class VoiceSessionManager:
async def start_session(self):
# Concurrent input/output processing
input_task = asyncio.create_task(self._process_audio_input())
output_task = asyncio.create_task(self._process_audio_output())
await asyncio.gather(input_task, output_task)
π‘ Streaming Voice Best Practices
- Activity Detection: Let the pipeline handle speech detection automatically
- Turn Management: Use lifecycle events to manage conversation flow
- Concurrent Processing: Handle input and output streams simultaneously
- Buffer Management: Add silence buffers between turns for natural flow
- Error Recovery: Implement robust error handling for streaming failures
- Resource Management: Properly clean up audio streams and resources
π Performance Characteristics
Streaming Pipeline Benefits
- Real-Time Interaction: Immediate response to user speech
- Natural Conversation: Continuous, flowing dialogue
- Activity Detection: Automatic turn boundary detection
- Concurrent Processing: Parallel input/output handling
- Scalable: Handles multiple turns efficiently
Technical Advantages
- Low Latency: Minimal delay between speech and response
- Adaptive: Handles variable speech patterns
- Robust: Automatic error recovery and continuation
- Efficient: Chunk-based processing for optimal performance
π Streaming Features
Automatic Turn Detection
- Speech Activity Detection: Automatically detects when user starts speaking
- Silence Detection: Identifies when user finishes speaking
- Turn Boundaries: Intelligent conversation turn management
- Continuous Listening: Always ready for next input
Real-Time Processing
- Live Transcription: Speech-to-text as you speak
- Streaming Agent Response: AI processing during speech
- Immediate Audio Output: Text-to-speech as response generates
- Parallel Operations: Multiple processes running simultaneously
Lifecycle Management
- Turn Events:
turn_startedandturn_endednotifications - Session Tracking: Multi-turn conversation analytics
- State Management: Proper resource allocation and cleanup
- Interruption Handling: Graceful handling of user interruptions
π¨ Requirements & Dependencies
Core Dependencies
openai-agents[voice]: OpenAI Agents SDK with voice supportsounddevice: Real-time audio I/Onumpy: Audio data processingthreading: Concurrent audio processingasyncio: Asynchronous pipeline management
System Requirements
- Real-Time Audio: Low-latency audio hardware
- Microphone: Good quality microphone for speech detection
- Processing Power: Sufficient CPU for real-time processing
- Network: Stable connection for streaming API calls
π Related Examples
- Static Voice: Turn-based voice interaction
- Voice Pipeline Documentation: Official pipeline docs
- Streaming Events: Voice event handling
π οΈ Advanced Customization
Custom Activity Detection
- Implement custom speech detection algorithms
- Add voice activity thresholds
- Configure silence detection parameters
Enhanced Session Management
- Add conversation memory across sessions
- Implement user authentication
- Add conversation logging and analytics
Real-Time Features
- Add live transcription display
- Implement real-time sentiment analysis
- Add voice emotion detection
π¨ Streaming Considerations
Interruption Handling
The SDK currently doesn't support built-in interruptions. Use lifecycle events to:
- Mute microphone during AI responses (
turn_started) - Unmute microphone after responses (
turn_ended) - Handle user interruptions gracefully
Performance Optimization
- Buffer Sizes: Optimize audio chunk sizes for latency vs. quality
- Concurrent Limits: Balance processing threads for performance
- Memory Management: Clean up audio buffers regularly
- Network Optimization: Handle API call failures gracefully
π‘ Pro Tips
- Start Simple: Begin with basic streaming, add features gradually
- Monitor Lifecycle Events: Use callbacks to understand turn flow
- Test Audio Hardware: Ensure low-latency audio setup
- Handle Edge Cases: Plan for network issues and audio failures
- Optimize for Conversation: Design agents for natural dialogue flow
π Next Steps
After mastering streaming voice agents:
- Production Deployment: Scale streaming voice for real applications
- Custom Voice Models: Integrate specialized speech models
- Multi-Modal Agents: Combine voice with vision and text
- Enterprise Voice Solutions: Build robust voice applications
π― Troubleshooting
Common Issues
- Audio Latency: Check audio hardware and buffer settings
- Speech Detection: Adjust microphone levels and sensitivity
- Turn Management: Monitor lifecycle events for debugging
- Resource Usage: Monitor CPU and memory during streaming
- Network Issues: Implement retry logic for API failures
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/streamed/README.md
- Last refreshed
- 7/24/2026, 3:00:13 AM (52m ago)
- Refresh schedule
- Daily Β· 03:00 UTC
- Dedupe status
- Unique Β· deduped by (source, url)