Record Class Message

java.lang.Object
java.lang.Record
org.a2aproject.sdk.spec.Message
Record Components:
role - the role of the message sender (user or agent)
parts - the content parts of the message (text, file, or data)
messageId - the unique identifier for this message
contextId - the conversation context identifier
taskId - the task identifier this message is associated with
referenceTaskIds - list of reference task identifiers
metadata - additional metadata for the message
extensions - list of protocol extensions used in this message
All Implemented Interfaces:
Event, EventKind, StreamingEventKind

public record Message(Message.Role role, List<Part<?>> parts, String messageId, @Nullable String contextId, @Nullable String taskId, @Nullable List<String> referenceTaskIds, @Nullable Map<String,Object> metadata, @Nullable List<String> extensions) extends Record implements EventKind, StreamingEventKind
Represents a single message in the conversation between a user and an agent in the A2A Protocol.

A Message encapsulates communication content exchanged during agent interactions. It contains the message role (user or agent), content parts (text, files, or data), and contextual metadata for message threading and correlation.

Messages are fundamental to the A2A Protocol's conversational model, enabling rich multi-modal communication between users and agents. Each message has a unique identifier and can reference related tasks and contexts.

Messages implement both EventKind and StreamingEventKind, meaning they can be sent as standalone events or as part of a streaming response sequence.

This class is mutable (allows setting taskId and contextId) to support post-construction correlation with tasks and conversation contexts. Use the Message.Builder for construction.

See Also:
  • Field Details

    • STREAMING_EVENT_ID

      public static final String STREAMING_EVENT_ID
      The identifier when used in streaming responses
      See Also:
  • Constructor Details

    • Message

      public Message(Message.Role role, List<Part<?>> parts, String messageId, @Nullable String contextId, @Nullable String taskId, @Nullable List<String> referenceTaskIds, @Nullable Map<String,Object> metadata, @Nullable List<String> extensions)
      Compact constructor with validation and defensive copying.
      Parameters:
      role - the role of the message sender (user or agent)
      parts - the content parts of the message (text, file, or data)
      messageId - the unique identifier for this message
      contextId - the conversation context identifier
      taskId - the task identifier this message is associated with
      referenceTaskIds - list of reference task identifiers
      metadata - additional metadata for the message
      extensions - list of protocol extensions used in this message
      Throws:
      IllegalArgumentException - if role, parts, or messageId is null, or if parts is empty
  • Method Details

    • kind

      public String kind()
      Description copied from interface: EventKind
      Returns the type identifier for this event.

      This method provides programmatic type discrimination for routing, logging, and debugging. NOTE: This value is NOT serialized to JSON in protocol v1.0+.

      Specified by:
      kind in interface EventKind
      Specified by:
      kind in interface StreamingEventKind
      Returns:
      the event kind string (e.g., "task", "message")
    • builder

      public static Message.Builder builder()
      Creates a new Builder for constructing Message instances.
      Returns:
      a Message.builder instance
    • builder

      public static Message.Builder builder(Message message)
      Creates a new Builder initialized with values from an existing Message.
      Parameters:
      message - the Message to copy values from
      Returns:
      a new Builder instance initialized with the message's values
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • role

      public Message.Role role()
      Returns the value of the role record component.
      Returns:
      the value of the role record component
    • parts

      public List<Part<?>> parts()
      Returns the value of the parts record component.
      Returns:
      the value of the parts record component
    • messageId

      public String messageId()
      Returns the value of the messageId record component.
      Returns:
      the value of the messageId record component
    • contextId

      public @Nullable String contextId()
      Returns the value of the contextId record component.
      Returns:
      the value of the contextId record component
    • taskId

      public @Nullable String taskId()
      Returns the value of the taskId record component.
      Returns:
      the value of the taskId record component
    • referenceTaskIds

      public @Nullable List<String> referenceTaskIds()
      Returns the value of the referenceTaskIds record component.
      Returns:
      the value of the referenceTaskIds record component
    • metadata

      public @Nullable Map<String,Object> metadata()
      Returns the value of the metadata record component.
      Returns:
      the value of the metadata record component
    • extensions

      public @Nullable List<String> extensions()
      Returns the value of the extensions record component.
      Returns:
      the value of the extensions record component