public final class ChatMessage
- Object
- 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
Methods
Inherited methods
Constructor details
ChatMessage
public ChatMessage(Role role, List<MessagePart> parts)Creates a message with no assistant tool calls or provider name.
Parameters
roleRole- speaker role; required
partsList<MessagePart>- ordered multimodal content;
nullbecomes empty
ChatMessage
public ChatMessage(Role role, List<MessagePart> parts, List<ToolCall> toolCalls, String name, String toolCallId)Creates a fully specified normalized message.
Parameters
roleRole- speaker role; required
partsList<MessagePart>- ordered content parts;
nullbecomes empty toolCallsList<ToolCall>- assistant tool calls;
nullbecomes empty nameString- optional provider-visible participant name
toolCallIdString- tool call answered by a
Role.TOOLmessage
Method details
system
public static ChatMessage system(String text)Parameters
textString- system instruction text
Returns
a single-part system message
user
public static ChatMessage user(String text)Parameters
textString- user input text
Returns
a single-part user message
assistant
public static ChatMessage assistant(String text)Parameters
textString- 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
textString- optional text accompanying the image
imageImagePart- 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
toolCallIdString- provider id of the answered tool call
resultJsonString- 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
nullgetToolCallId
public String getToolCallId()Returns
answered tool-call id for tool messages, or
nullgetText
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