Lesson 18 of 26 · see all lessons
  1. Start here: the exam, Python basics and responsible AI (skill area 1, 40-45%)

    1. 1. Start here: the AI-901 exam, the plan and what you already know
    2. 2. The Python and Azure basics AI-901 expects
    3. 3. Responsible AI: the six principles in practice
  2. AI models: how they work, choosing and deploying (skill area 1)

    1. 4. How generative AI models work
    2. 5. Choosing the right AI model for the job
    3. 6. Deploying a model: deployment options and configuration parameters
  3. AI workloads: text, speech, vision and extraction (skill area 1)

    1. 7. Common AI workloads: from chat and agents to vision and extraction
    2. 8. Text analysis: key phrases, entities, sentiment and summaries
    3. 9. Speech: recognition and synthesis
    4. 10. Computer vision and image generation
    5. 11. Extracting information from documents, images, audio and video
  4. Generative AI apps and agents in Foundry (skill area 2, 55-60%)

    1. 12. Writing system and user prompts
    2. 13. Deploying a model and chatting with it in the Foundry portal
    3. 14. Building a lightweight chat client with the Foundry SDK
    4. 15. Creating and testing a single agent in the Foundry portal
    5. 16. Building a lightweight client app for an agent
  5. Text, speech, vision and extraction apps in Foundry (skill area 2)

    1. 17. Building a text analysis app
    2. 18. Building speech apps: spoken prompts and Azure Speech
    3. 19. Building vision apps: images in prompts and generated images
    4. 20. Extracting information with Content Understanding: documents and forms
    5. 21. Content Understanding for images, audio and video, and a small extraction app
  6. Final review and four mock exams

    1. 22. Final review and exam day: every decision on one page
    2. 23. Mock exam 1
    3. 24. Mock exam 2
    4. 25. Mock exam 3
    5. 26. Mock exam 4
See the leaderboard

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.

[2]

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.

[58][79][115]

Rendering diagram…
Top: one audio-capable model hears and answers. Bottom: a pipeline of Azure Speech, a chat model and Azure Speech again.

[79][115]

Read the code: a spoken prompt to an audio-capable model

python
import base64, os
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
client = 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 name
modalities=["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.audio
print(reply.transcript)
open("answer.wav", "wb").write(base64.b64decode(reply.data))
Output: the text of the model's spoken answer, printed to the console, and the spoken answer saved as answer.wav.

[115]

Exam depth: know it cold.

  • Lines 5-10 create an AzureOpenAI client for your Foundry resource. Here it signs in with Microsoft Entra ID through DefaultAzureCredential. Audio chat needs an API version such as 2025-01-01-preview or 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 alloy voice, as WAV.
  • Lines 18-21 send one user message with two parts: a text instruction and an input_audio part holding the recording.
  • Lines 23-25 read the reply's transcript and decode its data into a WAV file.

Audio files can be up to 20 MB.

[115]

In the audio app, which content part carries the customer's recording to the model?

Pick one answer.

[115]

[115]

Read the code: an Azure Speech app

python
import os
import azure.cognitiveservices.speech as speechsdk
speech_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 microphone
audio_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 speaker
audio_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.")
Output: "You said: Which tours still have seats in June?" in the console, then the reply spoken on the speaker in the Ava voice.

[116][117]

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 region instead of an endpoint; 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. RecognizedSpeech means result.text holds the words.
  • Lines 17-20 create an AudioOutputConfig for the speaker and a SpeechSynthesizer, then speak_text_async(...).get() reads the reply aloud.

[116][117][118][119]

Azure Speech SDK objectsSpeechConfig: key, endpoint, language, voiceAudioConfig: microphone or fileAudioOutputConfig: speaker or fileSpeechRecognizer: recognize_once_asyncSpeechSynthesizer: speak_text_asyncresult.textSpoken audioOne config feeds bothdirectionsSpeech to textText to speech

[116][117]

Complete the code so the app reads the reply in a different voice. speech_config.____ = "en-US-Andrew:DragonHDLatestNeural"

Pick one answer.

[117][118]

[116][117][119][120]

SpeechRecognizer: I only listen. SpeechSynthesizer: I only talk. Every group project ever. Picture: Three-panel comic strip, bright flat colors.

Which speech approach fits?

Exam depth: know it cold.

NeedBest fitWhy
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.

[79][115][116][117]

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.

[79][116]

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.

[79]

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.

[116]

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.

[115]

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.

[79]

FlashcardsIndex cardsFlashcards ⚡

Card 1 of 16

[79][115][116][117]

Worth keeping

  1. A spoken prompt to one model: an audio-capable model with an input_audio content part.
  2. Azure Speech: SpeechConfig, then SpeechRecognizer to listen and SpeechSynthesizer to speak.
  3. The voice goes in speech_synthesis_voice_name; the input source goes in AudioConfig.
  4. 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.

[117]

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.

  1. [2]Microsoft Learn: Study guide for Exam AI-901: Microsoft Azure AI Fundamentals
  2. [58]Microsoft Learn: What is Azure Speech?
  3. [79]Microsoft Learn: Voice Live API overview
  4. [115]Microsoft Learn: Quickstart - Get started with Azure OpenAI audio generation
  5. [116]Microsoft Learn: Speech to text quickstart
  6. [117]Microsoft Learn: Text to speech quickstart
  7. [118]Microsoft Learn: azure.cognitiveservices.speech.SpeechConfig class
  8. [119]Microsoft Learn: azure.cognitiveservices.speech.SpeechRecognizer class
  9. [120]Microsoft Learn: azure.cognitiveservices.speech.SpeechSynthesizer class