Class Media

java.lang.Object
org.springframework.ai.content.Media

public class Media extends Object
The Media class represents the data and metadata of a media attachment in a message. It consists of a MIME type, raw data, and optional metadata such as id and name.

Media objects can be used in the UserMessage class to attach various types of content like images, documents, or videos. When interacting with AI models, the id and name fields help track and reference specific media objects.

The id field is typically assigned by AI models when they reference previously provided media.

The name field can be used to provide a descriptive identifier to the model, though care should be taken to avoid prompt injection vulnerabilities. For amazon AWS the name must only contain:

  • Alphanumeric characters
  • Whitespace characters (no more than one in a row)
  • Hyphens
  • Parentheses
  • Square brackets
Note, this class does not directly enforce that restriction.

If no name is provided, one will be automatically generated using the pattern: {mimeType.subtype}-{UUID}

This class includes a Media.Format inner class that provides commonly used MIME types as constants, organized by content category (documents, videos, images). These formats can be used when constructing Media objects to ensure correct MIME type specification.

This class is used as a parameter in the constructor of the UserMessage class.

Since:
1.0.0
Author:
Christian Tzolov, Mark Pollack, Thomas Vitale
  • Constructor Details

    • Media

      public Media(org.springframework.util.MimeType mimeType, URI uri)
      Create a new Media instance.
      Parameters:
      mimeType - the media MIME type
      uri - the URI for the media data
    • Media

      public Media(org.springframework.util.MimeType mimeType, org.springframework.core.io.Resource resource)
      Create a new Media instance.
      Parameters:
      mimeType - the media MIME type
      resource - the media resource
  • Method Details

    • builder

      public static Media.Builder builder()
      Creates a new Media builder.
      Returns:
      a new Media builder instance
    • getMimeType

      public org.springframework.util.MimeType getMimeType()
      Get the media MIME type
      Returns:
      the media MIME type
    • getData

      public Object getData()
      Get the media data object
      Returns:
      a java.net.URI.toString() or a byte[]
    • getDataAsByteArray

      public byte[] getDataAsByteArray()
      Get the media data as a byte array
      Returns:
      the media data as a byte array
    • getId

      @Nullable public String getId()
      Get the media id
      Returns:
      the media id
    • getName

      public String getName()