Class Media.Builder

java.lang.Object
org.springframework.ai.content.Media.Builder
Enclosing class:
Media

public static final class Media.Builder extends Object
Builder class for Media.
  • Method Details

    • mimeType

      public Media.Builder mimeType(org.springframework.util.MimeType mimeType)
      Sets the MIME type for the media object.
      Parameters:
      mimeType - the media MIME type, must not be null
      Returns:
      the builder instance
      Throws:
      IllegalArgumentException - if mimeType is null
    • data

      public Media.Builder data(org.springframework.core.io.Resource resource)
      Sets the media data from a Resource.
      Parameters:
      resource - the media resource, must not be null
      Returns:
      the builder instance
      Throws:
      IllegalArgumentException - if resource is null or if reading the resource content fails
    • data

      public Media.Builder data(byte[] data)
      Sets the media data from a byte array.
      Parameters:
      data - the raw media bytes, must not be null
      Returns:
      the builder instance
      Throws:
      IllegalArgumentException - if data is null
    • data

      public Media.Builder data(String data)
      Sets the media data from a String. The value may be a URL string (http/https/s3), or a base64-encoded representation of the media content.
      Parameters:
      data - the media data string, must not be null
      Returns:
      the builder instance
      Throws:
      IllegalArgumentException - if data is null
    • data

      public Media.Builder data(URI uri)
      Sets the media data from a URI.
      Parameters:
      uri - the media URI, must not be null
      Returns:
      the builder instance
      Throws:
      IllegalArgumentException - if URI is null
    • data

      public Media.Builder data(URL url)
      Sets the media data from a URL. The URL object is stored as-is, preserving its protocol for downstream security validation (e.g. blocking non-http/https schemes or internal addresses).
      Parameters:
      url - the media URL, must not be null
      Returns:
      the builder instance
      Throws:
      IllegalArgumentException - if URL is null
    • id

      public Media.Builder id(String id)
      Sets the ID for the media object. The ID is typically assigned by AI models when they return a reference to previously provided media content.
      Parameters:
      id - the media identifier
      Returns:
      the builder instance
    • name

      public Media.Builder name(String name)
      Sets the name for the media object.

      Important security note: This field is vulnerable to prompt injections, as the model might inadvertently interpret it as instructions. It is recommended to specify neutral names.

      The name must only contain:

      • Alphanumeric characters
      • Whitespace characters (no more than one in a row)
      • Hyphens
      • Parentheses
      • Square brackets
      Parameters:
      name - the media name
      Returns:
      the builder instance
    • build

      public Media build()
      Builds a new Media instance with the configured properties.
      Returns:
      a new Media instance
      Throws:
      IllegalArgumentException - if mimeType or data are null