liveBook · Manning

Media()

As explained in Spring AI in Action

media() is a Spring AI user-message specification method that attaches media content and its MIME type to a prompt.

media() attaches non-text content to a Spring AI user message. Supply a Media object or provide a MIME type with a Resource, while text() supplies the accompanying instruction. section 8.1.3 section 8.2

Audio question

Create Media.builder().data(question.questionAudio()).mimeType(MimeTypeUtils.parseMimeType("audio/mp3")).build(), then attach it with .text("Answer the question from the given audio file.").media(questionAudio).

Listing 8.5 Adding an image to the user message as media
@Override
public Answer askQuestion(Question question,
                          Resource image,             #1
                          String imageContentType,    #1
                          String conversationId) { #1
  var gameNameMatch = String.format(
      "gameTitle == '%s'",
      normalizeGameTitle(question.gameTitle()));

  var mediaType =
      MimeTypeUtils.parseMimeType(imageContentType);  #2

  return chatClient.prompt()
      .user(userSpec -> userSpec
          .text(question.question())
          .media(mediaType, image))  #3
      .system(systemSpec -> systemSpec
          .text(promptTemplate)
          .param("gameTitle", question.gameTitle()))
      .advisors(advisorSpec -> advisorSpec
          .param(FILTER_EXPRESSION, gameNameMatch)
          .param(CONVERSATION_ID, conversationId))
      .call()
      .entity(Answer.class);
}

Inspect the complete image-prompt implementation, including MIME-type parsing, media attachment, advisors, and response handling. section 8.2

How Spring AI in Action covers it

How deep it goes
Explains overloads, MIME-type handling, upload resources, controller integration, and working request examples without detailing transport internals.
Examples
Demonstrates spoken game questions, burger-game images, multipart endpoints, and HTTP requests returning audio or JSON answers.
What it assumes
Assumes familiarity with Spring services, ChatClient prompts, Resource, MultipartFile, and MIME types.
What you can do after
Build it from scratch by adding audio or images to Spring AI user messages and routing uploaded media through a controller.
How it connects
Connects user messages with MIME types, resources, multipart uploads, ChatClient, advisors, and multimodal language models.

Related concepts

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.