Merge pull request #1191 from pipecat-ai/mb/azure-tts-error-handling

Improve AzureTTSService error handling
This commit is contained in:
Mark Backman
2025-02-10 18:01:39 -05:00
committed by GitHub
2 changed files with 32 additions and 21 deletions

View File

@@ -20,6 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- Improved error handling in `AzureTTSService` to properly detect and log
synthesis cancellation errors.
- Enhanced `WhisperSTTService` with full language support and improved model - Enhanced `WhisperSTTService` with full language support and improved model
documentation. documentation.

View File

@@ -576,15 +576,19 @@ class AzureTTSService(AzureBaseTTSService):
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]: async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
logger.debug(f"Generating TTS: [{text}]") logger.debug(f"Generating TTS: [{text}]")
try:
if self._speech_synthesizer is None:
error_msg = "Speech synthesizer not initialized."
logger.error(error_msg)
yield ErrorFrame(error_msg)
return
try: try:
await self.start_ttfb_metrics() await self.start_ttfb_metrics()
yield TTSStartedFrame() yield TTSStartedFrame()
ssml = self._construct_ssml(text) ssml = self._construct_ssml(text)
# Start synthesis
self._speech_synthesizer.speak_ssml_async(ssml) self._speech_synthesizer.speak_ssml_async(ssml)
await self.start_tts_usage_metrics(text) await self.start_tts_usage_metrics(text)
# Stream audio chunks as they arrive # Stream audio chunks as they arrive
@@ -594,7 +598,6 @@ class AzureTTSService(AzureBaseTTSService):
break break
await self.stop_ttfb_metrics() await self.stop_ttfb_metrics()
yield TTSAudioRawFrame( yield TTSAudioRawFrame(
audio=chunk, audio=chunk,
sample_rate=self.sample_rate, sample_rate=self.sample_rate,
@@ -604,8 +607,13 @@ class AzureTTSService(AzureBaseTTSService):
yield TTSStoppedFrame() yield TTSStoppedFrame()
except Exception as e: except Exception as e:
logger.error(f"{self} error generating TTS: {e}") logger.error(f"{self} error during synthesis: {e}")
yield ErrorFrame(f"{self} error: {str(e)}") yield TTSStoppedFrame()
# Could add reconnection logic here if needed
return
except Exception as e:
logger.error(f"{self} exception: {e}")
class AzureHttpTTSService(AzureBaseTTSService): class AzureHttpTTSService(AzureBaseTTSService):