deepgram / deepgram-python-sdk

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

keepalive is not working as expected with asyncwebsocket #478

Closed atulyaatul1999 closed 1 week ago

atulyaatul1999 commented 2 weeks ago

I’m using the keepalive option with asyncwebsocket to prevent timeout errors. However, even with keepalive enabled, I'm still encountering: ConnectionClosed in AbstractAsyncWebSocketClient._listening with code 1011: Deepgram did not receive audio data or a text message within the timeout window. See https://dpgr.am/net0001.

Code that I am using:

dg_config = DeepgramClientOptions(options={"keepalive": "true"}) client: DeepgramClient = DeepgramClient( EnvSettings.DEEPGRAM_API_KEY, config=dg_config )

dg_connection = client.listen.asyncwebsocket.v("1")

davidvonthenen commented 2 weeks ago

Hi @atulyaatul1999

What version of the SDK are you using? Can you post your code here to verify?

This sounds like a thread being blocked and typically when that happens, it's because there is a missing await for a function call somewhere in the application code.

davidvonthenen commented 1 week ago

Verified keepalive is being sent (see log below) using example example/speech-to-text/websocket/async_microphone:

AsyncListenWebSocketClient._emit ENTER
callback handlers for: Results
after running thread: MainThread
after running thread: asyncio_0
after running thread: Thread-1 (_start_asyncio_loop)
number of active threads: 3
waiting for tasks to finish...
after running thread: MainThread
after running thread: asyncio_0
after running thread: Thread-1 (_start_asyncio_loop)
number of active threads: 3
AsyncListenWebSocketClient._emit LEAVE
_process_text Succeeded
AsyncListenWebSocketClient._process_text LEAVE
_listening Succeeded
AbstractAsyncWebSocketClient._listening LEAVE
Sending KeepAlive...
keep_alive succeeded
Text data received
AsyncListenWebSocketClient._process_text ENTER
Text data received
response_type: Results, data: {'type': 'Results', 'channel_index': [0, 1], 'duration': 3.0812502, 'start': 2.04, 'is_final': False, 'speech_final': False, 'channel': {'alternatives': [{'transcript': '', 'confidence': 0.0, 'words': []}]}, 'metadata': {'request_id': '9c174e47-2038-4fd0-8d6a-be8a86f4966c', 'model_info': {'name': '2-general-nova', 'version': '2024-01-18.26916', 'arch': 'nova-2'}, 'model_uuid': 'c0d1a568-ce81-4fea-97e7-bd45cb1fdf3c'}, 'from_finalize': False}
LiveResultResponse: {
    "channel": {
        "alternatives": [
            {
                "transcript": "",
                "confidence": 0.0,
                "words": []
            }
        ]
    },
    "metadata": {
        "model_info": {
            "name": "2-general-nova",
            "version": "2024-01-18.26916",
            "arch": "nova-2"
        },
        "request_id": "9c174e47-2038-4fd0-8d6a-be8a86f4966c",
        "model_uuid": "c0d1a568-ce81-4fea-97e7-bd45cb1fdf3c"
    },
    "type": "Results",
    "channel_index": [
        0,
        1
    ],
    "duration": 3.0812502,
    "start": 2.04,
    "is_final": false,
    "from_finalize": false,
    "speech_final": false
}

Verified keepalive holds the connection open using this example tests/edge_cases/keepalive/async for over a minute without sending audio:

(deepgram-python-sdk) vonthd@vonthds-mbp-2:async$ python main.py 
Version.v ENTER
version: 1
AsyncListenWebSocketClient.start ENTER
options: {
    "model": "nova-2"
}
addons: None
headers: None
members: None
kwargs: {}
ListenWebSocketOptions switching class -> dict
AbstractAsyncWebSocketClient.start ENTER
addons: None
headers: None
kwargs: {}
combined_options: {'model': 'nova-2'}
combined_headers: {'Accept': 'application/json', 'Authorization': 'Token 5a30f564b6f892c89c6d9c7a3249398293891dc7', 'User-Agent': '@deepgram/sdk/0.0.0 python/10.14'}
after running thread: MainThread
after running thread: asyncio_0
number of active threads: 2
create _listening thread
after running thread: MainThread
after running thread: asyncio_0
number of active threads: 2
AsyncListenWebSocketClient._emit ENTER
callback handlers for: Open
after running thread: MainThread
after running thread: asyncio_0
number of active threads: 2
after running thread: MainThread
after running thread: asyncio_0
number of active threads: 2
AsyncListenWebSocketClient._emit LEAVE
start succeeded
AbstractAsyncWebSocketClient.start LEAVE
after running thread: MainThread
after running thread: asyncio_0
number of active threads: 2
keepalive is enabled
autoflush is disabled
after running thread: MainThread
after running thread: asyncio_0
number of active threads: 2
start succeeded
AsyncListenWebSocketClient.start LEAVE
AbstractAsyncWebSocketClient._listening ENTER
AsyncListenWebSocketClient._keep_alive ENTER
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded
Sending KeepAlive...
keep_alive succeeded

Unless you have more info, this appears to be an error somewhere in your code. If you want to chat on discord and share your code, I can probably pinpoint where the error is.

davidvonthenen commented 1 week ago

If this is still an issue, please reopen with debug logs attached.