deepgram / deepgram-python-sdk

Official Python SDK for Deepgram's automated speech recognition APIs.
https://developers.deepgram.com
MIT License
179 stars 48 forks source link

Error upon socket connection closure #183

Open roerohan opened 7 months ago

roerohan commented 7 months ago

Description

I'm using v2.11.0 of your SDK and I often run into this error in live transcriptions.

future: <Task finished name='Task-311517' coro=<LiveTranscription._start() done, defined at /usr/local/lib/python3.11/dist-packages/deepgram/transcription.py:178> exception=ConnectionClosedError(Close(code=1011, reason='Deepgram did not provide a response message within the timeout window. See https://dpgr.am/net0000'), Close(code=1011, reason='Deepgram did not provide a response message within the timeout window. See https://dpgr.am/net0000'), True)>

Traceback (most recent call last):
 File "/usr/local/lib/python3.11/dist-packages/deepgram/transcription.py", line 222, in _start
   await self._socket.send(body)
 File "/usr/local/lib/python3.11/dist-packages/websockets/legacy/protocol.py", line 635, in send
   await self.ensure_open()
 File "/usr/local/lib/python3.11/dist-packages/websockets/legacy/protocol.py", line 939, in ensure_open
   raise self.connection_closed_exc()

After this, none of the send() method calls on an instance of the LiveTranscription object send any data over the socket.

Debugging

It seems like the issue is here: https://github.com/deepgram/deepgram-python-sdk/blob/2135dc5b352f1d7832975b8c726392f3a2b67496/deepgram/transcription.py#L198

It appears that the code only checks if the socket is closed when there is a message timeout while waiting in the queue. However, if I insert a lot of messages in the queue (by calling the send method) when self._socket.closed is False, and the socket closes later, the code will try to send the message over a closed socket at some point, resulting in the above error.

Since there is no try except block, the error also causes the while loop to exit, and therefore I'm not able to send any more messages using that object.

dvonthenen commented 7 months ago

Hi @roerohan The Live client is in the process of getting a major overhaul. Will ping the community to take a look when the PR becomes available.

roerohan commented 7 months ago

Alright!

I also wanted to bring to your notice that there's no way to handle this error currently since the async task created in the following code snippet isn't returned outside the SDK, and there's no way to catch the exception since I don't have access to this task

https://github.com/deepgram/deepgram-python-sdk/blob/2135dc5b352f1d7832975b8c726392f3a2b67496/deepgram/transcription.py#L175

For now, the only way I found to fix it was a hacky workaround, by using the _socket object directly from the LiveTranscription class.

dvonthenen commented 7 months ago

A new release was just posted which should address this problem, please take a look: https://github.com/deepgram/deepgram-python-sdk/releases/tag/v3.0.0-alpha.1

dvonthenen commented 7 months ago

Latest is alpha.4 with another release due out today

dvonthenen commented 7 months ago

@roerohan alpha.5 is out and that commit is most likely going to be the beta.1 release. Have your y been able to try with this latest update?

roerohan commented 7 months ago

Hi @dvonthenen I'm currently using a workaround where I'm sending the socket messages directly using the _socket object. I'll try out the newer version as soon as I get some time.

dvonthenen commented 6 months ago

cool. If you run into any problems, please let me know.

dvonthenen commented 4 months ago

Re-opened to take a look at when I have time. I am unfamiliar with v2 version, so this might take some time.

dvonthenen commented 3 months ago

@SandraRodgers said she would take a look at this issue and the other one since she has more knowledge about the v2 client.

jz155 commented 2 months ago

i have an issue not sure if it is a similar one: Could not open socket: type object 'LiveTranscriptionEvents' has no attribute 'Unhandled'