Class ToolExecution

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

public class ToolExecution extends Object
Represents the execution of a tool, including the request and the result.
  • Method Details

    • request

      public ToolExecutionRequest request()
      Returns the request of the tool execution.
      Returns:
      the request of the tool execution.
    • result

      public String result()
      Returns the tool execution result as text.
      Returns:
      the result of the tool execution.
      See Also:
    • resultContents

      @Experimental public List<Content> resultContents()
      Returns the contents of the tool execution result. For text-only results, returns a singleton list containing a TextContent.
      Since:
      1.13.0
      See Also:
    • resultObject

      public @Nullable Object resultObject()
      Returns the tool execution result as object. This object is the actual value returned by the tool.
      Returns:
      the result of the tool execution, or null when the tool returned none, and when a tool output guardrail rewrote or rejected the result (the object no longer matches what the LLM sees): isResultReplaced() tells the two apart
      See Also:
    • attributes

      public Map<String,Object> attributes()
      Returns the attributes associated with the tool execution. Attributes can be set by the tool itself or, in case of MCP tools, originate from the _meta field of the tool call response. They are not sent to the LLM.
      Returns:
      an unmodifiable map of attributes, or an empty map if none were set.
      Since:
      1.19.0
    • hasFailed

      public boolean hasFailed()
      Indicates whether the tool execution result represents an error.
    • startTime

      public LocalDateTime startTime()
      Returns the time when the tool execution started, or null if not recorded.
    • finishTime

      public LocalDateTime finishTime()
      Returns the time when the tool execution finished, or null if not recorded.
    • duration

      public Duration duration()
      Returns the duration of the tool execution, or null if timing was not recorded.
    • invocationContext

      public InvocationContext invocationContext()
      Returns the invocation context of the tool execution.
    • isResultReplaced

      @Experimental public boolean isResultReplaced()
      Whether a tool output guardrail replaced the result of a tool that ran: with a rewrite, or with the message of a non-fatal failure. resultObject() then no longer holds the object the tool returned (it is dropped, since it does not match what the LLM sees), even when the tool returned one. That is also the case when an output guardrail rewrote the error result of a tool that failed on its own.

      false when no guardrail replaced the result (also for a tool that failed on its own and whose error result was sent as it is), and when an input guardrail rejected the call (the tool did not run, and hasFailed() is true). A fatal guardrail failure gives no execution.

      Returns:
      whether a tool output guardrail replaced the result of the tool
      Since:
      1.25.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 ToolExecution.Builder builder()