Class ToolResponseMessage

java.lang.Object
org.springframework.ai.chat.messages.AbstractMessage
org.springframework.ai.chat.messages.ToolResponseMessage
All Implemented Interfaces:
Message, Content

public class ToolResponseMessage extends AbstractMessage
The ToolResponseMessage class represents a message with a function content in a chat application.

The message content is an ordered list of MessageParts, one ToolResultPart per tool result, so a result can carry more than a string, for example media or an error flag. getResponses() is a view over the parts that keeps only the text of each result. Messages built through the legacy constructor or the ToolResponseMessage.Builder.responses(List) setter get one text-only result part per response, so existing code keeps working unchanged.

Since:
1.0.0
Author:
Christian Tzolov, Eric Bottard
  • Field Details

  • Constructor Details

    • ToolResponseMessage

      protected ToolResponseMessage(List<ToolResponseMessage.ToolResponse> responses, Map<String,Object> metadata)
    • ToolResponseMessage

      protected ToolResponseMessage(Map<String,Object> metadata, List<MessagePart> parts)
      Creates a message from an ordered list of parts. The metadata comes first because the legacy constructor already takes a list and a map in the other order and the two would otherwise have the same erasure.
      Parameters:
      metadata - the message metadata
      parts - the parts, in order
      Since:
      2.1.0
  • Method Details

    • builder

      public static ToolResponseMessage.Builder builder()
    • getResults

      public List<ToolResultPart> getResults()
      The tool results of this message, in part order.
      Returns:
      an unmodifiable list of result parts
      Since:
      2.1.0
    • getResponses

      public List<ToolResponseMessage.ToolResponse> getResponses()
      The tool responses of this message: the text of each ToolResultPart, in part order. Content a string cannot carry, such as media, is not included; read getResults() for it.
      Returns:
      an unmodifiable list of responses
    • getText

      public String getText()
      Tool response messages never had a null text, even with no responses at all; only AbstractMessage's generic rule for an empty parts list would say otherwise.
      Specified by:
      getText in interface Content
      Overrides:
      getText in class AbstractMessage
      Returns:
      the content of the message
    • mutate

      public ToolResponseMessage.Builder mutate()
      A builder pre-populated with this message's parts and metadata.
      Returns:
      the builder
      Since:
      2.1.0
    • toString

      public String toString()
      Overrides:
      toString in class Object