Lesson 18 of 26 · see all lessons
Start here: the exam, Python basics and responsible AI (skill area 1, 40-45%)
AI models: how they work, choosing and deploying (skill area 1)
AI workloads: text, speech, vision and extraction (skill area 1)
Generative AI apps and agents in Foundry (skill area 2, 55-60%)
Text, speech, vision and extraction apps in Foundry (skill area 2)
- 17. Building a text analysis app
- 18. Building speech apps: spoken prompts and Azure Speech
- 19. Building vision apps: images in prompts and generated images
- 20. Extracting information with Content Understanding: documents and forms
- 21. Content Understanding for images, audio and video, and a small extraction app
Final review and four mock exams
18 min
Building speech apps: spoken prompts and Azure Speech
Respond to spoken prompts with an audio-capable model and read a small app built with the Azure Speech SDK.
By the end of this lesson you can
- Read a Python call that sends a spoken prompt to an audio-capable model
- Read an Azure Speech SDK app that turns speech into text and text into speech
- Choose between one audio model, a speech-to-chat-to-speech pipeline and Voice Live
From earlier lessons · 1 of 2
Your Azure Language app calls client.analyze_sentiment(reviews). Where do you read how sure the service is that a review is negative?
Pick one answer.
From earlier lessons · 2 of 2
Fernhill Travel wants a voice that reads booking confirmations aloud. Which capability is that?
Pick one answer.
A customer calls the Fernhill Travel helpline and asks, out loud, which tours still have seats in June. Another customer, who is blind, wants every answer spoken back. The team asks you for a small app that listens and talks. There are two ways to build it, and the exam tests both.
Two ways to handle speech
Exam depth: know it cold.
An audio-capable multimodal model takes sound as input and can answer in text, audio or both. One call does the listening, the thinking and the talking. Examples in Foundry are gpt-audio and gpt-audio-mini.
Azure Speech is the Foundry Tool for speech. Its speech to text turns your voice into text. Its text to speech reads text aloud in a chosen voice. Put a chat model in the middle, and you get a pipeline: speech to text, then a chat model, then text to speech.
The pipeline gives you more control. The single model is less code.
Read the code: a spoken prompt to an audio-capable model
import base64, osfrom openai import AzureOpenAIfrom azure.identity import DefaultAzureCredential, get_bearer_token_providerclient = AzureOpenAI(azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://ai.azure.com/.default"),azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],api_version="2025-01-01-preview",)with open("question.wav", "rb") as f:audio_b64 = base64.b64encode(f.read()).decode("utf-8")completion = client.chat.completions.create(model="gpt-audio-mini", # your deployment namemodalities=["text", "audio"],audio={"voice": "alloy", "format": "wav"},messages=[{"role": "user", "content": [{"type": "text", "text": "Answer the travel question in this recording."},{"type": "input_audio", "input_audio": {"data": audio_b64, "format": "wav"}},]}],)reply = completion.choices[0].message.audioprint(reply.transcript)open("answer.wav", "wb").write(base64.b64decode(reply.data))
Exam depth: know it cold.
- Lines 5-10 create an
AzureOpenAIclient for your Foundry resource. Here it signs in with Microsoft Entra ID throughDefaultAzureCredential. Audio chat needs an API version such as2025-01-01-previewor later. - Lines 11-12 read the WAV file and encode it as base64, text that can travel inside JSON.
- Line 15 passes your deployment name as
model. - Lines 16-17 ask for text and audio back, in the
alloyvoice, as WAV. - Lines 18-21 send one user message with two parts: a text instruction and an
input_audiopart holding the recording. - Lines 23-25 read the reply's
transcriptand decode itsdatainto a WAV file.
Audio files can be up to 20 MB.
In the audio app, which content part carries the customer's recording to the model?
Pick one answer.
Read the code: an Azure Speech app
import osimport azure.cognitiveservices.speech as speechsdkspeech_config = speechsdk.SpeechConfig(subscription=os.environ.get("SPEECH_KEY"), endpoint=os.environ.get("ENDPOINT"))speech_config.speech_recognition_language = "en-US"speech_config.speech_synthesis_voice_name = "en-US-Ava:DragonHDLatestNeural"# Speech to text: listen for one utterance on the default microphoneaudio_in = speechsdk.audio.AudioConfig(use_default_microphone=True)recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_in)result = recognizer.recognize_once_async().get()if result.reason == speechsdk.ResultReason.RecognizedSpeech:print("You said:", result.text)# Text to speech: read a reply on the default speakeraudio_out = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_out)reply = f"You asked: {result.text}. Checking June tours now."synthesizer.speak_text_async(reply).get()elif result.reason == speechsdk.ResultReason.NoMatch:print("No speech could be recognized.")
Exam depth: know it cold.
- Line 2 imports the Speech SDK, installed with
pip install azure-cognitiveservices-speech. - Lines 4-5 create a SpeechConfig from the Speech resource key and endpoint. Older samples pass a
regioninstead of anendpoint; both work. - Line 6 sets the language to listen for. Line 7 sets the voice name to speak with.
- Lines 10-12 create an AudioConfig for the microphone and a SpeechRecognizer.
recognize_once_async().get()listens for one utterance, up to about 30 seconds or until silence. - Line 14 checks
result.reason.RecognizedSpeechmeansresult.textholds the words. - Lines 17-20 create an AudioOutputConfig for the speaker and a SpeechSynthesizer, then
speak_text_async(...).get()reads the reply aloud.
Complete the code so the app reads the reply in a different voice. speech_config.____ = "en-US-Andrew:DragonHDLatestNeural"
Pick one answer.
Which speech approach fits?
Exam depth: know it cold.
| Need | Best fit | Why |
|---|---|---|
| Ask a question out loud, get an answer, least code. | Audio-capable model. | One call hears and answers. |
| A transcript you store, search or check. | Azure Speech, speech to text. | It returns the exact words as text. |
| Read fixed text aloud in a chosen voice. | Azure Speech, text to speech. | Many prebuilt voices, no model needed. |
| Control each step, such as checking the text before answering. | Speech to text, chat model, text to speech. | You see and change the text in between. |
| Natural back-and-forth voice agent with low latency. | Voice Live API. | One managed service with interruption handling. |
Decision rule: a spoken prompt to one model means an audio-capable model. Exact words or a set voice means Azure Speech.
Fernhill Travel must keep a written record of every helpline question, word for word, before any answer is generated. Staff will review the text later. What should the app use first?
Pick one answer.
Voice Live API
Exam depth: recognize it.
The Voice Live API is a managed Azure Speech service for real-time voice agents. It joins speech recognition, a generative model and text to speech behind one interface. You don't deploy a model; you pick one, such as a GPT or Phi model.
It adds noise suppression, echo cancellation, interruption detection and end-of-turn detection. Apps connect over WebSockets or WebRTC. Pick it for a natural, low-latency voice conversation, such as a contact-centre bot.
Exam-style practice
Fernhill Travel has recorded voicemails saved as WAV files. Your Speech SDK app currently listens to the microphone. You need it to transcribe a file instead. Which line should you change?
Pick one answer.
For each statement, select Yes if it is true. Statement: in the audio-model app, the audio={"voice": "alloy", "format": "wav"} setting tells the model the format of the customer's recording.
Pick one answer.
A tour company plans a phone agent that customers can interrupt mid-sentence. The solution must handle background noise and keep replies fast, with the least engineering. What should you use?
Pick one answer.
Worth keeping
- A spoken prompt to one model: an audio-capable model with an input_audio content part.
- Azure Speech: SpeechConfig, then SpeechRecognizer to listen and SpeechSynthesizer to speak.
- The voice goes in speech_synthesis_voice_name; the input source goes in AudioConfig.
- Need exact words or a set voice: Azure Speech. Real-time voice agent: Voice Live.
On the job
For a first version, the pipeline is easier to debug: you can log the text at each step and see where it went wrong. Keep the reply short, because long answers sound slow when spoken. For accessibility, such as Fernhill's spoken itineraries, text to speech alone is often enough. No chat model is needed to read a document aloud.
The helpline can listen and talk. Then a guide sends a photo of a broken coach seat. She wants the app to say what's wrong. Next lesson, your app reads pictures and even draws new ones.
Leaderboard
Loading…
50 XP for each lesson you finish, 10 for each right answer. Names show only for learners who turn on Show me on leaderboards.
Embed this practice on your site
Free for classrooms, blogs and study groups. Paste this into any page that accepts HTML.
Report a copyright problem with this lesson
Sources for this lesson (9)
Each fact in the lesson is marked with the number of the page it comes from.
- [2]Microsoft Learn: Study guide for Exam AI-901: Microsoft Azure AI Fundamentals
- [58]Microsoft Learn: What is Azure Speech?
- [79]Microsoft Learn: Voice Live API overview
- [115]Microsoft Learn: Quickstart - Get started with Azure OpenAI audio generation
- [116]Microsoft Learn: Speech to text quickstart
- [117]Microsoft Learn: Text to speech quickstart
- [118]Microsoft Learn: azure.cognitiveservices.speech.SpeechConfig class
- [119]Microsoft Learn: azure.cognitiveservices.speech.SpeechRecognizer class
- [120]Microsoft Learn: azure.cognitiveservices.speech.SpeechSynthesizer class