How to call Azure speech to text service in a streaming way

The following code shows how to call the microsoft Azure text to speech service in an continuous or streaming way.
The audio input is from default microphone.

import azure.cognitiveservices.speech as speechsdk

def from_mic_continuous():
speech_config = speechsdk.SpeechConfig(subscription="subscription_key", region="azure service region")
audio_config =
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

def recognized_handler(evt):
# This handler is for final results
if evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
print(f"Final result: {evt.result.text}")

def recognizing_handler(evt):
# This handler is for partial results
if evt.result.reason == speechsdk.ResultReason.RecognizingSpeech:
print(f"Partial result: {evt.result.text}")


# Start continuous speech recognition
while True:
# Keep the main thread alive while the background thread processes the speech recognition
except KeyboardInterrupt:
# Stop recognition when Ctrl+C is pressed


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !