Text and Speech Solutions in Foundry · Topic 2 of 2

Spoken Prompts and Azure Speech Applications

AI-901 speech apps: sending audio to a multimodal model with input_audio, reading the transcript, and the Azure Speech SDK for recognition and synthesis in Python.

  • 5 min
In a nutshell
  • Audio goes to a multimodal chat model as an input_audio content part.
  • The reply's audio object has transcript, data, id and expires_at. Use transcript for captions.
  • The Speech SDK uses SpeechConfig, speech_synthesis_voice_name, speech_recognition_language, SpeechSynthesizer and SpeechRecognizer.

What it is

There are two ways to build an application that listens and speaks. A multimodal model that accepts audio can take a recorded question and answer directly. The Azure Speech service, used through the Azure Speech SDK, converts speech to text and text to speech, and you connect it to any language model.

Which one you choose depends on whether you want one model to handle the whole exchange or separate, specialised steps.

Spoken questionrecorded audioMultimodal modelunderstands the audioReplytext, or spoken audio

How it works

Spoken prompts with a multimodal model

A model deployed with audio support accepts audio in a chat completions request as an input_audio content part. The part holds the encoded audio and its format, next to any text instruction.

import base64

with open("question.wav", "rb") as f:
    audio_b64 = base64.b64encode(f.read()).decode("utf-8")

messages = [
    {"role": "system", "content": "You are a helpful voice assistant. Keep answers short."},
    {"role": "user", "content": [
        {"type": "input_audio", "input_audio": {"data": audio_b64, "format": "wav"}},
    ]},
]

The roles are the same as for text. The system message carries standing instructions, the user message carries the recorded question, the assistant message carries earlier replies and a tool message carries function output.

To get a spoken answer, the request asks for audio output with a chosen voice. The reply then contains an audio object.

Field of the audio objectWhat it holds
transcriptThe text of the spoken reply, which suits on screen captions
dataThe audio itself, encoded
idAn identifier for the reply
expires_atWhen the stored audio lapses

For continuous, low latency conversation, the Realtime API keeps a session open. Session properties include instructions, which are standing guidance, voice, which picks the voice for replies, input_audio_format, such as pcm16, which says how the audio you send is encoded, and temperature, which controls randomness.

The Azure Speech SDK

The Python package is azure-cognitiveservices-speech. You create a SpeechConfig with the key and endpoint of your Speech resource, read from environment variables.

Text to speech

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_synthesis_voice_name = "en-US-Ava:DragonHDLatestNeural"

audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)

result = synthesizer.speak_text_async("Your order has shipped.").get()
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
    print("Done")

The property speech_synthesis_voice_name chooses the neural voice that speaks.

Speech to text

speech_config.speech_recognition_language = "en-US"
audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

result = recognizer.recognize_once()
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
    print(result.text)
elif result.reason == speechsdk.ResultReason.NoMatch:
    print("No speech could be recognised")

The property speech_recognition_language sets the language of the audio you expect.

Result reasonMeaning
RecognizedSpeechSpeech was recognised and text holds it
NoMatchNo speech was found in the audio
SynthesizingAudioCompletedSpeech was synthesised
CanceledThe operation was cancelled, and cancellation details give the error

For finer control of speech output, send SSML, with elements such as voice, prosody for pitch and rate, and break for pauses.

Choosing an approach

The needApproach
One model that listens and answersA multimodal model with input_audio
Natural two way voice conversation with low delayThe Realtime API
Captions on live audio within secondsAzure Speech real time recognition
Thousands of stored recordingsBatch transcription
Control of pronunciation, pitch and pausesAzure Speech with SSML

Errors you may meet

A 400 error can mean an unsupported audio format or a malformed body. A 401 means the key or credential is missing or wrong. A 404 means a deployment name that does not exist. A 429 means requests above the rate limit.

A worked scenario

A kiosk lets visitors ask questions aloud. The first version records each question to a WAV file and sends it as an input_audio part to a multimodal model, with a system message that says to answer briefly. The reply's transcript field is shown as captions on the screen. A second feature reads out the opening hours using the Speech SDK with a chosen neural voice, set through speech_synthesis_voice_name. Keys are read from environment variables, and when the kiosk misunderstands a brand name, the team adds it to a phrase list.

What to remember for the exam

  • Audio goes to a multimodal chat model as an input_audio content part.
  • The reply's audio object has transcript, data, id and expires_at. Use transcript for captions.
  • The Speech SDK uses SpeechConfig, speech_synthesis_voice_name, speech_recognition_language, SpeechSynthesizer and SpeechRecognizer.
  • Real time transcription suits live captions, and batch transcription suits stored files.
  • Keys come from environment variables or Key Vault.
Exam tip

To pick the voice use speech_synthesis_voice_name. To set the language you expect to hear, use speech_recognition_language.

How the exam asks about this

  • "How is recorded speech supplied to a multimodal model in a chat completions request?" An input_audio content part.
  • "Which field of the audio object gives text for on screen captions?" transcript.
  • "Which SpeechConfig property chooses the neural voice?" speech_synthesis_voice_name.
  • "Which Realtime session property says how the audio you send is encoded?" input_audio_format.

Common mistakes

Sending audio as an image or file path. Use an input_audio part with encoded data.

Confusing the two language properties. One is for synthesis voice, and the other for recognition language.

Using batch transcription for live captions. It is not real time.

Hard coding the Speech key. Use environment variables or Key Vault.

Check yourself

  • Which content part carries recorded speech to a multimodal chat model?

    An input_audio part with the encoded audio and its format.

  • Which property chooses the voice in the Azure Speech SDK?

    speech_synthesis_voice_name.

  • Which field of a spoken reply gives text for captions?

    The transcript field of the audio object.

Topic 2 of 2 in Text and Speech Solutions in Foundry

Test yourself on Text and Speech Solutions in Foundry

The full Microsoft AI-901 question bank has 511 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Microsoft. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.