Event Reference & Usage
VoiceRun uses an event-driven architecture. Here's a comprehensive guide to all available events and how to use them effectively.
Event Lifecycle
Events flow through the system in the following sequence:
- Event handler is run once per "Input Event"
- Event handler can emit zero or more "Output Events"
- Each "Output Event" is processed in the same order as they are emitted
Input Events
StartEvent
Event emitted when a new voice agent session has started.
Data Fields
Example Usage
if isinstance(event, StartEvent):
# Handle start event
TextEvent
Event emitted when the user speaks or types text.
Data Fields
Example Usage
if isinstance(event, TextEvent):
text = event.data["text"] # What the user said
source = event.data["source"] # "speech" or "text"
TimeoutEvent
Event emitted when the user does not speak for 5 seconds.
Data Fields
Example Usage
if isinstance(event, TimeoutEvent):
# Handle timeout event
Output Events
AudioEvent
Plays audio to the user from a URL.
Data Fields
Example Usage
yield AudioEvent(path="https://example.com/audio.mp3")LogEvent
Logs a message to the system logs.
Data Fields
Example Usage
yield LogEvent(message="Processing user request")SilenceEvent
Plays silence for a specified duration in milliseconds.
Data Fields
Example Usage
yield SilenceEvent(duration=2000)StopEvent
Stops the current voice agent session. This will end the call.
Data Fields
Example Usage
yield StopEvent()TextToSpeechEvent
Converts text to speech using the specified voice and plays it to the user. Supports advanced features like voice styling, speed control, caching, and streaming for optimized performance.
Data Fields
Example Usage
# Basic usage with Prim Voice
yield TextToSpeechEvent(
text="Hello, I'm speaking to you!",
voice="lyric"
)
# OpenAI Voice with full feature support
yield TextToSpeechEvent(
text="Welcome to our service!",
voice="alloy",
instructions="enthusiastic and professional",
speed=1.25,
stream=True,
cache=True
)
# Non-interruptible announcement (user cannot interrupt)
yield TextToSpeechEvent(
text="IMPORTANT: This message cannot be interrupted.",
voice="nova",
interruptible=False,
instructions="serious and authoritative tone"
)
# High-performance streaming for real-time conversation
yield TextToSpeechEvent(
text="How can I help you today?",
voice="zephyr",
stream=True,
cache=False # Disable cache for dynamic content
)
# Azure Neural Voice with speed control
yield TextToSpeechEvent(
text="你好,我是小晓!",
voice="xiaoxiao",
speed=1.0,
stream=True
)
# Customer service scenario with caching for common responses
yield TextToSpeechEvent(
text="Thank you for calling. How may I assist you?",
voice="nova",
instructions="warm and welcoming tone",
speed=1.0,
cache=True, # Cache common greetings
interruptible=True
)
# Performance-optimized for repeated content
yield TextToSpeechEvent(
text="Please hold while I transfer your call.",
voice="alloy",
cache=True, # Cache hold messages
stream=False, # Pre-generate common messages
interruptible=False # Complete message before transfer
)TransferSessionEvent
Channel-agnostic session transfer. For phone channels: transfers the call to another phone number. For web/API channels: redirects the session to another agent or environment.
Data Fields
Example Usage
# Phone transfer (cold transfer)
yield TransferSessionEvent(phone_number="+15555555555")
# Phone transfer with context data
yield TransferSessionEvent(
phone_number="+15555551234",
data={"reason": "technical_support", "priority": "high", "customer_tier": "premium"}
)
# Web/API redirect to different agent
yield TransferSessionEvent(
agent_id="technical_support_agent",
environment="production"
)
# Web/API redirect with context preservation
yield TransferSessionEvent(
agent_id="billing_specialist",
environment="production",
data={"conversation_context": "payment_issue", "customer_id": "12345"}
)STTUpdateSettingsEvent
Dynamically updates Speech-to-Text (STT) configuration settings during a conversation. This allows agents to change language, models, transcription prompts, endpointing sensitivity, and audio processing without restarting the session.
Data Fields
Example Usage
# Switch to Spanish language
yield STTUpdateSettingsEvent(language="es")
# Switch STT provider with prewarming
yield STTUpdateSettingsEvent(
model="gpt-4o-transcribe",
prewarm_model="nova-3" # Prepare Deepgram in background
)
# Improve transcription accuracy with context
yield STTUpdateSettingsEvent(
prompt="This is a technical conversation. Please transcribe technical terms accurately."
)
# Optimize for phone call audio
yield STTUpdateSettingsEvent(
noise_reduction="telephony",
endpointing=2000 # 2 second silence for phone calls
)
# Comprehensive configuration update
yield STTUpdateSettingsEvent(
language="es",
model="nova-3",
prompt="Conversación técnica en español. Transcribe términos técnicos con precisión.",
endpointing=500,
noise_reduction="near_field",
prewarm_model="gpt-4o-transcribe"
)StartRecordingEvent
Starts recording the current call session. Useful for quality assurance, training, or compliance purposes. Only available for phone channels.
Data Fields
Example Usage
# Start recording without callback
yield StartRecordingEvent()
# Start recording with status webhook
yield StartRecordingEvent(
status_callback_url="https://your-app.com/webhooks/recording-status"
)StopRecordingEvent
Stops the current call recording session. The recording will be finalized and made available for download.
Data Fields
Example Usage
# Stop current recording
yield StopRecordingEvent()