Class Media

java.lang.Object
org.springframework.ai.model.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
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Builder class for Media.
    static class 
    Common media formats.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Media(org.springframework.util.MimeType mimeType, URL url)
    Create a new Media instance.
    Media(org.springframework.util.MimeType mimeType, org.springframework.core.io.Resource resource)
    Create a new Media instance.
    Media(org.springframework.util.MimeType mimeType, org.springframework.core.io.Resource resource, String id)
    Deprecated.
    Use Media.Builder instead.
  • Method Summary

    Modifier and Type
    Method
    Description
    static final Media.Builder
    Creates a new Media builder.
    Get the media data object
    byte[]
    Get the media data as a byte array
    Get the media id
    org.springframework.util.MimeType
    Get the media MIME type
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Media

      public Media(org.springframework.util.MimeType mimeType, URL url)
      Create a new Media instance.
      Parameters:
      mimeType - the media MIME type
      url - the URL 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
    • Media

      @Deprecated(since="1.0.0.M5") public Media(org.springframework.util.MimeType mimeType, org.springframework.core.io.Resource resource, String id)
      Deprecated.
      Use Media.Builder instead.
      Create a new Media instance.
      Parameters:
      mimeType - the media MIME type
      resource - the media resource
      id - the media id
  • Method Details

    • builder

      public static final 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.URL.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

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

      public String getName()