Support for retry when 503 error to TTS API.

This commit is contained in:
Sam Sykes
2025-11-11 13:49:14 +00:00
parent 54e8d29615
commit adf5198423
2 changed files with 68 additions and 36 deletions

View File

@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.0.94] - 2025-11-10 ## [0.0.94] - 2025-11-10
### Changed
- Added support for retrying `SpeechmaticsTTSService` when it returns a 503
error. Default values in `InputParams`.
### Deprecated ### Deprecated
- The `KrispFilter` is deprecated and will be removed in a future version. Use - The `KrispFilter` is deprecated and will be removed in a future version. Use

View File

@@ -6,6 +6,7 @@
"""Speechmatics TTS service integration.""" """Speechmatics TTS service integration."""
import asyncio
from typing import AsyncGenerator, Optional from typing import AsyncGenerator, Optional
from urllib.parse import urlencode from urllib.parse import urlencode
@@ -45,7 +46,8 @@ class SpeechmaticsTTSService(TTSService):
class InputParams(BaseModel): class InputParams(BaseModel):
"""Optional input parameters for Speechmatics TTS configuration.""" """Optional input parameters for Speechmatics TTS configuration."""
pass retry_interval_s: float = 0.02
retry_timeout_s: float = 1.0
def __init__( def __init__(
self, self,
@@ -125,9 +127,29 @@ class SpeechmaticsTTSService(TTSService):
try: try:
await self.start_ttfb_metrics() await self.start_ttfb_metrics()
# Retry loop for 503 responses
start_time = asyncio.get_event_loop().time()
while True:
async with self._session.post(url, json=payload, headers=headers) as response: async with self._session.post(url, json=payload, headers=headers) as response:
if response.status == 503:
elapsed_time = asyncio.get_event_loop().time() - start_time
if elapsed_time >= self._params.retry_timeout_s:
error_message = (
f"{self} HTTP 503 (timeout after {self._params.retry_timeout_s}s)"
)
logger.error(error_message)
yield ErrorFrame(error=error_message)
return
logger.debug(
f"{self} Received 503, retrying in {self._params.retry_interval_s}s..."
)
await asyncio.sleep(self._params.retry_interval_s)
continue
if response.status != 200: if response.status != 200:
error_message = f"Speechmatics TTS error: HTTP {response.status}" error_message = f"{self} HTTP {response.status}"
logger.error(error_message) logger.error(error_message)
yield ErrorFrame(error=error_message) yield ErrorFrame(error=error_message)
return return
@@ -155,7 +177,9 @@ class SpeechmaticsTTSService(TTSService):
complete_bytes = complete_samples * 2 complete_bytes = complete_samples * 2
audio_data = buffer[:complete_bytes] audio_data = buffer[:complete_bytes]
buffer = buffer[complete_bytes:] # Keep remaining bytes for next iteration buffer = buffer[
complete_bytes:
] # Keep remaining bytes for next iteration
yield TTSAudioRawFrame( yield TTSAudioRawFrame(
audio=audio_data, audio=audio_data,
@@ -163,8 +187,11 @@ class SpeechmaticsTTSService(TTSService):
num_channels=1, num_channels=1,
) )
# Successfully processed the response, break out of retry loop
break
except Exception as e: except Exception as e:
logger.exception(f"Error generating TTS: {e}") logger.exception(f"{self}: Error generating TTS: {e}")
yield ErrorFrame(error=f"Speechmatics TTS error: {str(e)}") yield ErrorFrame(error=f"Speechmatics TTS error: {str(e)}")
finally: finally:
yield TTSStoppedFrame() yield TTSStoppedFrame()