Class ToolOutputGuardrailResult

java.lang.Object
dev.langchain4j.guardrail.ToolOutputGuardrailResult
All Implemented Interfaces:
GuardrailResult<ToolOutputGuardrailResult>

@Experimental public final class ToolOutputGuardrailResult extends Object implements GuardrailResult<ToolOutputGuardrailResult>
The result of the validation of a ToolOutputGuardrail.

A successful result may carry a rewrite of the tool result, in one of two forms:

  • a text rewrite (successWith(String)): the text contents are replaced by the given text, the other contents and the error flag are kept;
  • a contents rewrite (successWith(List, boolean)): the contents and the error flag are both replaced.
Since:
1.25.0
  • Method Details

    • success

      public static ToolOutputGuardrailResult success()
      Gets a successful tool output guardrail result.
      Returns:
      a successful result without any rewrite
    • successWith

      public static ToolOutputGuardrailResult successWith(@Nullable String rewrittenText)
      Produces a successful result with rewritten text. The text contents of the tool result are replaced by a single text content with the given text; any non-text contents and the error flag are kept.
      Parameters:
      rewrittenText - the new text of the tool result. When null, the result is a plain success.
      Returns:
      a successful result carrying the rewritten text
    • successWith

      public static ToolOutputGuardrailResult successWith(List<Content> rewrittenContents, boolean isError)
      Produces a successful result that replaces all the contents of the tool result and its error flag.

      Passing isError == true turns the tool result into an error result, which counts as a failed step for compensation purposes.

      Parameters:
      rewrittenContents - the new contents of the tool result, must not be null or empty (a tool result always has contents) and must not contain null
      isError - whether the rewritten tool result is an error
      Returns:
      a successful result carrying the rewritten contents and error flag
      Throws:
      IllegalArgumentException - if rewrittenContents is null or empty, or contains null
    • result

      public GuardrailResult.Result result()
      Description copied from interface: GuardrailResult
      The result of the guardrail
      Specified by:
      result in interface GuardrailResult<ToolOutputGuardrailResult>
    • successfulText

      public @Nullable String successfulText()
      Description copied from interface: GuardrailResult
      The message of the successful result
      Specified by:
      successfulText in interface GuardrailResult<ToolOutputGuardrailResult>
      Returns:
      the rewritten text of the tool result: the given text for a text rewrite, the text contents joined with \n for a contents rewrite (an empty string if there are none), or null when the result was not rewritten
    • hasRewrittenContents

      public boolean hasRewrittenContents()
      Whether this result rewrites the contents and the error flag (successWith(List, boolean)), as opposed to the text only (successWith(String)).
      Returns:
      true for a contents rewrite
    • rewrittenContents

      public List<Content> rewrittenContents(ToolOutputGuardrailRequest request)
      Gets the contents of the tool result after applying this result to the given request.
      Parameters:
      request - the request this result was produced for
      Returns:
      the rewritten contents, or the contents of the request if this result carries no rewrite
    • rewrittenIsError

      public boolean rewrittenIsError(ToolOutputGuardrailRequest request)
      Gets the error flag of the tool result after applying this result to the given request.
      Parameters:
      request - the request this result was produced for
      Returns:
      the rewritten error flag for a contents rewrite, otherwise the error flag of the request
    • failureMessage

      public String failureMessage()
      Gets the text of the error tool result that reports the failures of this result to the LLM: the messages of all the failures, joined with \n in chain order. No framework text is added.
      Returns:
      the joined failure messages, or an empty string if there are no failures
    • failures

      public <F extends GuardrailResult.Failure> List<F> failures()
      Specified by:
      failures in interface GuardrailResult<ToolOutputGuardrailResult>
      Returns:
      The list of failures eventually resulting from a set of validations.
    • toString

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

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

      public int hashCode()
      Overrides:
      hashCode in class Object