Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/pipecat/services/gladia/stt.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def __init__(
self._reconnection_delay = reconnection_delay
self._reconnection_attempts = 0
self._session_url = None
self._session_id = None
self._connection_active = False

# Audio buffer management
Expand All @@ -278,6 +279,9 @@ def __init__(
self._connection_task = None
self._should_reconnect = True

def __str__(self):
return f"GladiaSTTService [{self._session_id}]"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all our services we log self.name to include the specific instance, so it would be better to keep the same pattern and just include the session id, like this:

Suggested change
return f"GladiaSTTService [{self._session_id}]"
return f"{self.name} [{self._session_id}]"


def can_generate_metrics(self) -> bool:
"""Check if the service can generate performance metrics.

Expand Down Expand Up @@ -412,14 +416,14 @@ async def run_stt(self, audio: bytes) -> AsyncGenerator[Frame, None]:
trim_size = len(self._audio_buffer) - self._max_buffer_size
self._audio_buffer = self._audio_buffer[trim_size:]
self._bytes_sent = max(0, self._bytes_sent - trim_size)
logger.warning(f"Audio buffer exceeded max size, trimmed {trim_size} bytes")
logger.warning(f"{self} Audio buffer exceeded max size, trimmed {trim_size} bytes")

# Send audio if connected
if self._connection_active and self._websocket and self._websocket.state is State.OPEN:
try:
await self._send_audio(audio)
except websockets.exceptions.ConnectionClosed as e:
logger.warning(f"Websocket closed while sending audio chunk: {e}")
logger.warning(f"{self} Websocket closed while sending audio chunk: {e}")
self._connection_active = False

yield None
Expand All @@ -433,8 +437,9 @@ async def _connection_handler(self):
settings = self._prepare_settings()
response = await self._setup_gladia(settings)
self._session_url = response["url"]
self._session_id = response["id"]
self._reconnection_attempts = 0
logger.info(f"Session URL : {self._session_url}")
logger.info(f"{self} Session URL : {self._session_url}")

# Connect with automatic reconnection
async with websocket_connect(self._session_url) as websocket:
Expand All @@ -454,7 +459,7 @@ async def _connection_handler(self):
await asyncio.gather(self._receive_task, self._keepalive_task)

except websockets.exceptions.ConnectionClosed as e:
logger.warning(f"WebSocket connection closed: {e}")
logger.warning(f"{self} WebSocket connection closed: {e}")
self._connection_active = False

# Clean up tasks
Expand Down Expand Up @@ -510,10 +515,10 @@ async def _setup_gladia(self, settings: Dict[str, Any]):
else:
error_text = await response.text()
logger.error(
f"Gladia error: {response.status}: {error_text or response.reason}"
f"{self} Gladia error: {response.status}: {error_text or response.reason}"
)
raise Exception(
f"Failed to initialize Gladia session: {response.status} - {error_text}"
f"{self} Failed to initialize Gladia session: {response.status} - {error_text}"
)

@traced_stt
Expand Down Expand Up @@ -553,10 +558,10 @@ async def _keepalive_task_handler(self):
empty_audio = b""
await self._send_audio(empty_audio)
else:
logger.debug("Websocket closed, stopping keepalive")
logger.debug(f"{self} Websocket closed, stopping keepalive")
break
except websockets.exceptions.ConnectionClosed:
logger.debug("Connection closed during keepalive")
logger.debug(f"{self} Connection closed during keepalive")
except Exception as e:
await self.push_error(error_msg=f"Unknown error occurred: {e}", exception=e)

Expand Down Expand Up @@ -630,7 +635,7 @@ async def _maybe_reconnect(self) -> bool:
self._reconnection_attempts += 1
if self._reconnection_attempts > self._max_reconnection_attempts:
await self.push_error(
error_msg=f"Max reconnection attempts ({self._max_reconnection_attempts}) reached",
error_msg=f"{self} Max reconnection attempts ({self._max_reconnection_attempts}) reached",
)
self._should_reconnect = False
return False
Expand Down