Exception Hierarchy
All SpeechRecognition exceptions inherit from Python’s built-inException class:
UnknownValueError
Raised when the speech is unintelligible or when the recognition service cannot understand the audio. Inheritance:Exception
When Raised:
- The speech recognition API successfully processed the request but could not transcribe the audio
- The audio quality is too poor to recognize
- The speech is in a different language than expected
- The audio contains only silence or background noise
- Using
recognize_sphinx(),recognize_google(),recognize_wit(),recognize_bing(),recognize_azure(), or other recognition methods when the audio cannot be understood
Example
Handling Multiple Recognition Engines
RequestError
Raised when the speech recognition service request fails. Inheritance:Exception
When Raised:
- Network connection fails (no internet connection)
- API authentication fails (invalid API key)
- API rate limits are exceeded
- The recognition service is unavailable
- Invalid request parameters are provided
- Required dependencies are missing (e.g., PocketSphinx module)
- Network connectivity issues
- Invalid or expired API credentials
- Service outages
- Missing configuration files or language data (for offline engines like PocketSphinx)
Example
Handling Network Errors
WaitTimeoutError
Raised when listening times out while waiting for a phrase to start or for a hotword to be detected. Inheritance:Exception
When Raised:
- The
timeoutparameter inlisten()expires before speech begins - The
timeoutparameter insnowboy_wait_for_hot_word()expires before the hotword is detected - No audio input is detected within the specified timeout period
- User doesn’t speak within the timeout period when using
recognizer.listen(source, timeout=5) - Waiting for a wake word that is never spoken
Example
Background Listening with Timeout Handling
Phrase Time Limit
TranscriptionNotReady
Raised when an asynchronous transcription job is not yet complete. Inheritance:Exception
When Raised:
- Checking the status of an AWS Transcribe job that is still in progress
- Checking the status of an AssemblyAI transcription that hasn’t completed yet
- A transcription job was just submitted and is being processed
job_name: The identifier of the transcription job (if available)file_key: The file key associated with the transcription (if available)
- Polling for transcription results from asynchronous services like AWS Transcribe or AssemblyAI
- Implementing retry logic for long-running transcriptions
Example
AWS Transcribe Example
TranscriptionFailed
Raised when an asynchronous transcription job has failed. Inheritance:Exception
When Raised:
- An AWS Transcribe job completes with a “FAILED” status
- An AssemblyAI transcription completes with an “error” status
- The transcription service encountered an error processing the audio
job_name: The identifier of the failed transcription job (set toNoneafter cleanup)file_key: The file key associated with the transcription (set toNoneafter cleanup)
- Invalid audio format or corrupted audio file
- Transcription service internal errors
- Audio file is too long or too short
- Unsupported language or audio encoding
Example
Comprehensive Error Handling
Complete Exception Handling Pattern
Here’s a comprehensive example demonstrating proper exception handling for all SpeechRecognition exceptions:Best Practices
1. Always Handle Both UnknownValueError and RequestError
2. Use Appropriate Timeouts
3. Implement Retry Logic for Network Errors
4. Handle Asynchronous Transcriptions Properly
See Also
- Recognizer - The main speech recognition class
- AudioSource - Audio input sources
- AudioData - Recorded audio data representation