Class ToolExecutionResult

java.lang.Object
dev.langchain4j.service.tool.ToolExecutionResult

public class ToolExecutionResult extends Object
Represents the result of a tool execution.

The result can be provided in three mutually exclusive ways via the builder:

Regardless of which builder method was used, the canonical accessor is resultContents(), which always returns a List<Content>. The convenience method resultText() can be used when the result is known to be a single TextContent; it throws IllegalStateException otherwise.

Since:
1.6.0
  • Constructor Details

  • Method Details

    • isError

      public boolean isError()
      Returns whether the tool execution resulted in an error.
      Returns:
      true if the tool execution failed, false otherwise.
    • result

      public Object result()
      Returns the raw object returned by the tool method. This is the original value before any conversion to Content. It is not sent to the LLM.
      Returns:
      the raw result object, or null if not set.
      See Also:
    • resultText

      public String resultText()
      Returns the tool execution result as a plain text string. This is a convenience method for when the result is known to be a single TextContent.
      Returns:
      the text of the single TextContent element.
      Throws:
      IllegalStateException - if the result contains non-text or multiple content elements. Use resultContents() instead.
      See Also:
    • resultContents

      @Experimental public List<Content> resultContents()
      Returns the contents of the tool execution result that will be sent to the LLM.

      When built with ToolExecutionResult.Builder.resultTextSupplier(Supplier), the contents are calculated lazily on first access and then cached.

      Thread-safety: in rare concurrent scenarios, the supplier may be invoked multiple times, but only one result will be cached. Suppliers should be idempotent and side-effect free.

      Virtual thread friendly: uses lock-free atomic operations that do not pin carrier threads.

      Returns:
      the list of Content elements, never null.
      Since:
      1.13.0
    • attributes

      public Map<String,Object> attributes()
      Returns attributes associated with the tool execution. Attributes are propagated into ToolExecutionResultMessage.attributes() and can be persisted in a ChatMemory. They are not sent to the LLM.
      Returns:
      an unmodifiable map of attributes, or an empty map if none were set.
      Since:
      1.12.0
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • builder

      public static ToolExecutionResult.Builder builder()