Class AIGuard.Message

java.lang.Object
datadog.trace.api.aiguard.AIGuard.Message
Enclosing class:
AIGuard

public static class AIGuard.Message extends Object
Represents a message in an AI conversation. Messages can represent user prompts, assistant responses, system messages, or tool outputs.

Messages can contain either plain text content or structured content parts (text and images):

Example usage:


 // User prompt with text
 var userPrompt = AIGuard.Message.message("user", "What's the weather like?");

 // User prompt with text and image
 var multimodalPrompt = AIGuard.Message.message("user", List.of(
     AIGuard.ContentPart.text("Describe this image:"),
     AIGuard.ContentPart.imageUrl("https://example.com/image.jpg")
 ));

 // Assistant response with tool calls
 var assistantWithTools = AIGuard.Message.assistant(
     AIGuard.ToolCall.toolCall("call_123", "get_weather", "{\"location\": \"New York\"}")
 );

 // Tool response
 var toolResponse = AIGuard.Message.tool("call_123", "Sunny, 75°F");
 
  • Constructor Details

    • Message

      public Message(@Nonnull String role, @Nullable String content, @Nullable List<AIGuard.ToolCall> toolCalls, @Nullable String toolCallId)
      Creates a new message with the specified parameters.
      Parameters:
      role - the role of the message sender (e.g., "user", "assistant", "system", "tool")
      content - the text content of the message, or null for assistant messages with only tool calls
      toolCalls - list of tool calls associated with this message, or null if no tool calls
      toolCallId - the tool call ID this message is responding to, or null if not a tool response
    • Message

      public Message(@Nonnull String role, @Nonnull List<AIGuard.ContentPart> contentParts, @Nullable List<AIGuard.ToolCall> toolCalls, @Nullable String toolCallId)
      Creates a new message with content parts (text and/or images).
      Parameters:
      role - the role of the message sender
      contentParts - list of content parts
      toolCalls - list of tool calls, or null
      toolCallId - the tool call ID this message responds to, or null
      Throws:
      IllegalArgumentException - if contentParts contains null elements
  • Method Details

    • getRole

      public String getRole()
      Returns the role of the message sender.
      Returns:
      the role (e.g., "user", "assistant", "system", "tool")
    • getContent

      @Nullable public String getContent()
      Returns the text content of the message.
      Returns:
      the message content, or null if using content parts or for assistant messages with only tool calls
    • getContentParts

      @Nullable public List<AIGuard.ContentPart> getContentParts()
      Returns the content parts of the message.
      Returns:
      the content parts (text and images), or null if using plain text content
    • getToolCalls

      public List<AIGuard.ToolCall> getToolCalls()
      Returns the list of tool calls associated with this message.
      Returns:
      list of tool calls, or null if this message has no tool calls
    • getToolCallId

      public String getToolCallId()
      Returns the tool call ID that this message is responding to.
      Returns:
      the tool call ID, or null if this is not a tool response message
    • message

      @Nonnull public static AIGuard.Message message(@Nonnull String role, @Nonnull String content)
      Creates a message with specified role and text content.
      Parameters:
      role - the role of the message sender (e.g., "user", "system")
      content - the text content of the message
      Returns:
      a new Message instance
    • message

      @Nonnull public static AIGuard.Message message(@Nonnull String role, @Nonnull List<AIGuard.ContentPart> contentParts)
      Creates a message with specified role and content parts (text and/or images).
      Parameters:
      role - the role of the message sender (e.g., "user", "system")
      contentParts - list of content parts (text and/or images)
      Returns:
      a new Message instance
    • tool

      public static AIGuard.Message tool(String toolCallId, String content)
      Creates a tool response message.
      Parameters:
      toolCallId - the ID of the tool call this message is responding to
      content - the result or output from the tool execution
      Returns:
      a new Message instance with role "tool"
    • assistant

      @Nonnull public static AIGuard.Message assistant(@Nonnull AIGuard.ToolCall... toolCalls)
      Creates an assistant message with tool calls but no text content.
      Parameters:
      toolCalls - the tool calls the assistant wants to make
      Returns:
      a new Message instance with role "assistant" and no text content