Spring / Spring AI interview questions
What is the Spring AI AudioModel and how does it support speech synthesis?
Spring AI includes an AudioModel (specifically SpeechModel) abstraction for text-to-speech (TTS) generation. This covers converting text responses to spoken audio — useful for voice assistants, accessibility features, and audio content pipelines. Currently, the primary provider with TTS support in Spring AI is OpenAI, which offers the tts-1 and tts-1-hd models with multiple voices (alloy, echo, fable, onyx, nova, shimmer).
@Service public class SpeechService { private final SpeechModel speechModel; public SpeechService(SpeechModel speechModel) { this.speechModel = speechModel; } public byte[] synthesise(String text) { SpeechResponse response = speechModel.call( new SpeechPrompt(text, OpenAiAudioSpeechOptions.builder() .withVoice(OpenAiAudioApi.SpeechRequest.Voice.NOVA) .withResponseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3) .withSpeed(1.0f) .build()) ); return response.getResult().getOutput(); // returns byte[] audio data } }
The SpeechResponse carries the audio as a byte[] which you can write to a file, stream as an HTTP response, or forward to a message broker. The response format can be MP3, OPUS, AAC, FLAC, or WAV depending on the provider's supported formats.
Speech-to-text (transcription) is a separate capability covered by the AudioTranscriptionModel abstraction, also backed by OpenAI Whisper in Spring AI's current implementation.
More Related questions...