SpeechmaticsSTTService: improve exception handling and loggin

This commit is contained in:
Aleix Conchillo Flaqué
2025-08-21 18:41:03 -07:00
parent 3abaaf80e0
commit b61846534d
2 changed files with 31 additions and 17 deletions

View File

@@ -56,6 +56,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Added `SpeechmaticsSTTService` exception handling on connection and sending.
- Replaced `asyncio.wait_for()` for `wait_for2.wait_for()` for Python <
3.12. because of issues regarding task cancellation (i.e. cancellation is
never propagated).

View File

@@ -23,6 +23,7 @@ from pipecat.frames.frames import (
BotInterruptionFrame,
CancelFrame,
EndFrame,
ErrorFrame,
Frame,
InterimTranscriptionFrame,
StartFrame,
@@ -463,8 +464,14 @@ class SpeechmaticsSTTService(STTService):
async def run_stt(self, audio: bytes) -> AsyncGenerator[Frame, None]:
"""Adds audio to the audio buffer and yields None."""
await self._client.send_audio(audio)
yield None
try:
if self._client:
await self._client.send_audio(audio)
yield None
except Exception as e:
logger.error(f"Speechmatics error: {e}")
yield ErrorFrame(f"Speechmatics error: {e}", fatal=False)
await self._disconnect()
def update_params(
self,
@@ -520,7 +527,7 @@ class SpeechmaticsSTTService(STTService):
)
# Log the event
logger.debug("Connected to Speechmatics STT service")
logger.debug(f"{self} Connecting to Speechmatics STT service")
# Recognition started event
@self._client.on(ServerMessageType.RECOGNITION_STARTED)
@@ -562,31 +569,36 @@ class SpeechmaticsSTTService(STTService):
)
# Start session
await self._client.start_session(
transcription_config=self._transcription_config,
audio_format=AudioFormat(
encoding=self._params.audio_encoding,
sample_rate=self.sample_rate,
chunk_size=self._params.chunk_size,
),
)
try:
await self._client.start_session(
transcription_config=self._transcription_config,
audio_format=AudioFormat(
encoding=self._params.audio_encoding,
sample_rate=self.sample_rate,
chunk_size=self._params.chunk_size,
),
)
logger.debug(f"{self} Connected to Speechmatics STT service")
except Exception as e:
logger.error(f"{self} Error connecting to Speechmatics: {e}")
finally:
self._client = None
async def _disconnect(self) -> None:
"""Disconnect from the STT service."""
# Disconnect the client
logger.debug(f"{self} Disconnecting from Speechmatics STT service")
try:
if self._client:
await asyncio.wait_for(self._client.close(), timeout=1.0)
await asyncio.wait_for(self._client.close(), timeout=5.0)
logger.debug(f"{self} Disconnected from Speechmatics STT service")
except asyncio.TimeoutError:
logger.warning("Timeout while closing Speechmatics client connection")
logger.warning(f"{self} Timeout while closing Speechmatics client connection")
except Exception as e:
logger.error(f"Error closing Speechmatics client: {e}")
logger.error(f"{self} Error closing Speechmatics client: {e}")
finally:
self._client = None
# Log the event
logger.debug("Disconnected from Speechmatics STT service")
def _process_config(self) -> None:
"""Create a formatted STT transcription config.