public final class ChatMessage

  1. Object
  2. ChatMessage
A single turn in a chat conversation. Holds a Role, one or more MessageParts, and (for assistant turns) any ToolCalls the model produced. Construct via the static helpers (user(String), system(String), etc.) for the common case, or pass parts directly for multi-modal messages.

Constructors

public ChatMessage(Role role, List<MessagePart> parts)Creates a message with no assistant tool calls or provider name.
public ChatMessage(Role role, List<MessagePart> parts, List<ToolCall> toolCalls, String name, String toolCallId)Creates a fully specified normalized message.

Methods

public static ChatMessage system(String text)
public static ChatMessage user(String text)
public static ChatMessage assistant(String text)
public static ChatMessage userWithImage(String text, ImagePart image)Builds a USER message containing both a text and image part – the common multi-modal pattern.
public static ChatMessage toolResult(String toolCallId, String resultJson)Builds a TOOL message wrapping the result of a previous tool call.
public Role getRole()
public List<MessagePart> getParts()
public List<ToolCall> getToolCalls()
public String getName()
public String getToolCallId()
public String getText()Convenience: concatenates the text of every TextPart.

Inherited methods

Constructor details

ChatMessage

public ChatMessage(Role role, List<MessagePart> parts)
Creates a message with no assistant tool calls or provider name.

Parameters

role Role
speaker role; required
parts List<MessagePart>
ordered multimodal content; null becomes empty

ChatMessage

public ChatMessage(Role role, List<MessagePart> parts, List<ToolCall> toolCalls, String name, String toolCallId)
Creates a fully specified normalized message.

Parameters

role Role
speaker role; required
parts List<MessagePart>
ordered content parts; null becomes empty
toolCalls List<ToolCall>
assistant tool calls; null becomes empty
name String
optional provider-visible participant name
toolCallId String
tool call answered by a Role.TOOL message

Method details

system

public static ChatMessage system(String text)

Parameters

text String
system instruction text

Returns

a single-part system message

user

public static ChatMessage user(String text)

Parameters

text String
user input text

Returns

a single-part user message

assistant

public static ChatMessage assistant(String text)

Parameters

text String
assistant output text

Returns

a single-part assistant message

userWithImage

public static ChatMessage userWithImage(String text, ImagePart image)
Builds a USER message containing both a text and image part – the common multi-modal pattern.

Parameters

text String
optional text accompanying the image
image ImagePart
image content sent to the model

Returns

multimodal user message

toolResult

public static ChatMessage toolResult(String toolCallId, String resultJson)
Builds a TOOL message wrapping the result of a previous tool call.

Parameters

toolCallId String
provider id of the answered tool call
resultJson String
application result encoded as JSON

Returns

tool-role result message

getRole

public Role getRole()

Returns

speaker role

getParts

public List<MessagePart> getParts()

Returns

immutable content parts in message order

getToolCalls

public List<ToolCall> getToolCalls()

Returns

immutable assistant tool calls

getName

public String getName()

Returns

optional participant name, or null

getToolCallId

public String getToolCallId()

Returns

answered tool-call id for tool messages, or null

getText

public String getText()
Convenience: concatenates the text of every TextPart. Image and tool-result parts are skipped. Useful for ChatView rendering when you don’t care about multi-modal content.

Returns

text parts joined with newlines