liveBook · Manning

SpeechResponse

As explained in Spring AI in Action

SpeechResponse is a Spring AI response object containing synthesized audio bytes and related response information.

SpeechResponse packages synthesized audio and response information when SpeechModel processes a SpeechPrompt. Extract the generated bytes through response.getResult().getOutput(). section 8.1.2

Use SpeechResponse when the speech request needs configurable generation options:

  • Create a SpeechPrompt with an OpenAiAudioSpeechOptions object. section 8.1.2
  • Select the voice, audio model, and response format through the options builder. section 8.1.2
  • Extract the audio bytes and wrap them in a ByteArrayResource for an HTTP response. section 8.1.2

Returning generated speech

A board-game voice endpoint can transcribe an uploaded question, generate an answer, synthesize that answer, and return audio/mpeg bytes. section 8.1.2

public Resource textToSpeech(String text) {
  SpeechPrompt speechPrompt = new SpeechPrompt(text);
  SpeechResponse response = speechModel.call(speechPrompt);
  byte[] speechBytes = response.getResult().getOutput();
  return new ByteArrayResource(speechBytes);
}

Inspect the concrete extraction path from SpeechResponse to ByteArrayResource. section 8.1.2

How Spring AI in Action covers it

How deep it goes
Defines extraction through getResult().getOutput(), but does not detail other response information.
Examples
Uses a board-game answer returned as audio/mpeg and saved as answer.mp3.
What it assumes
Assumes SpeechModel.call() and byte-array handling, while contrasting direct String calls with prompt-based responses.
What you can do after
Apply it via Spring AI by extracting response bytes and wrapping them in a ByteArrayResource.
How it connects
Links SpeechPrompt, SpeechModel, OpenAiAudioSpeechOptions, and ByteArrayResource in configurable text-to-speech flows.

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.