Class ToolInputGuardrailResult

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

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

A successful result may carry rewritten tool arguments (a JSON string), which replace the arguments of the tool execution request for the guardrails that follow and for the tool execution itself.

Since:
1.25.0
  • Method Details

    • success

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

      public static ToolInputGuardrailResult successWith(@Nullable String rewrittenArguments)
      Produces a successful result with rewritten tool arguments.
      Parameters:
      rewrittenArguments - the new arguments of the tool, as a JSON object (otherwise the executor fails the call with a ToolInputGuardrailException). When null, the result is a plain success.
      Returns:
      a successful result carrying the rewritten arguments
    • successWithArguments

      public static ToolInputGuardrailResult successWithArguments(Map<String,?> rewrittenArguments)
      Produces a successful result with rewritten tool arguments, given as a map of argument names to values. The map is serialized to a JSON object right away, with the same JSON codec that ToolInputGuardrailRequest.argumentsAsMap() parses the arguments with, so quotes and other special characters in the values need no escaping.

      Unlike successWith(String), a null map is rejected: a guardrail that redacts the arguments and returns null by mistake must not let the original arguments through.

      Parameters:
      rewrittenArguments - the new arguments of the tool, by name; must not be null
      Returns:
      a successful result carrying the rewritten arguments
      Throws:
      IllegalArgumentException - if rewrittenArguments is null
      RuntimeException - if a value has no JSON representation; thrown from a guardrail, it is a fatal failure of the guardrail, before the tool runs
    • result

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

      public @Nullable String successfulText()
      Description copied from interface: GuardrailResult
      The message of the successful result
      Specified by:
      successfulText in interface GuardrailResult<ToolInputGuardrailResult>
      Returns:
      the rewritten tool arguments (a JSON string), or null if the arguments were not rewritten
    • rewrittenArguments

      public Optional<String> rewrittenArguments()
      Gets the tool arguments as the guardrail chain left them: the arguments rewritten by the guardrails that ran, or empty if none of them rewrote the arguments.

      Unlike successfulText(), this is also available on a failed result, when a guardrail that ran before the failing one rewrote the arguments. The tool is not executed then, but what the caller reports for the rejected call is the latest request state.

      Returns:
      the rewritten tool arguments (a JSON string), if any guardrail of the chain rewrote them
    • 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<ToolInputGuardrailResult>
      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