Finalize
Learn how to send Deepgram a Finalize message, which flushes the websocket stream’s audio by forcing the server to process all unprocessed audio data immediately and return the results.
In real-time audio processing, there are scenarios where you may need to force the server to process (or flush) all unprocessed audio data immediately. Deepgram supports a Finalize
message to handle such situations, ensuring that interim results are treated as final.
What is the Finalize
Message?
The Finalize
message is a JSON command that you send to the Deepgram server, instructing it to process and finalize all remaining audio data immediately. This is particularly useful in scenarios where an utterance has ended, or when transitioning to a keep-alive period to ensure that no previous transcripts reappear unexpectedly.
Sending Finalize
To send the Finalize
message, you need to send the following JSON message to the server:
You can optionally specify a channel
field to finalize a specific channel. If the channel
field is omitted, all channels in the audio will be finalized. Note that channel indexing starts at 0, so to finalize only the first channel you need to send:
Finalize
Confirmation
Upon receiving the Finalize message, the server will process all remaining audio data and return the final results. You may receive a response with the from_finalize
attribute set to true
, indicating that the finalization process is complete. This response typically occurs when there is a noticeable amount of audio buffered in the server.
If you specified a channel
to be finalized, use the response’s channel_index
field to check which channel was finalized.
In most cases, you will receive this response, but it is not guaranteed if there is no significant amount of audio data to process.
Language-Specific Implementations
Following are code examples to help you get started.
Sending a Finalize
message in JSON Format
These snippets demonstrate how to construct a JSON message containing the “Finalize” type and send it over the WebSocket connection in each respective language.
Streaming Examples
Here are more complete examples that make a streaming request and use Finalize. Try running these examples to see how Finalize can be sent to Deepgram, forcing the API to process all unprocessed audio data and immediately return the results.
Conclusion
Using the Finalize
message with Deepgram’s API allows for precise control over the finalization of audio processing. This feature is essential for scenarios requiring immediate processing of the remaining audio data, ensuring accurate and timely results.