Interface ToolInputGuardrail

All Superinterfaces:
Guardrail<ToolInputGuardrailRequest, ToolInputGuardrailResult>

@Experimental public interface ToolInputGuardrail extends Guardrail<ToolInputGuardrailRequest, ToolInputGuardrailResult>
A tool input guardrail is a rule applied before a tool is executed, to validate or rewrite the arguments the LLM chose.

Unlike InputGuardrail, it has no retry or reprompt: a non-fatal failure becomes an error tool result sent to the LLM, which may then correct itself.

A non-fatal failure does not stop the other guardrails of the chain from running; their failures are aggregated. A fatal failure stops the chain and aborts the AI service invocation.

One instance can be shared by every tool and every invocation of an AI service, and called concurrently, so implementations must be thread-safe.

Since:
1.25.0
  • Method Details

    • validate

      Validates the tool execution request before the tool is executed.

      Implementations must not modify the request.

      Specified by:
      validate in interface Guardrail<ToolInputGuardrailRequest, ToolInputGuardrailResult>
      Parameters:
      request - the request
      Returns:
      the result of the validation
    • success

      default ToolInputGuardrailResult success()
      Produces a successful result without any rewrite.
      Returns:
      the result of a successful tool input guardrail validation
    • successWith

      default ToolInputGuardrailResult successWith(@Nullable String rewrittenArguments)
      Produces a successful result with rewritten tool arguments, which replace the original ones for the following guardrails and for the tool execution.

      The chat memory keeps the tool call as the LLM made it: the rewrite changes what the tool runs with and what the events report, not the history. The tool result that the LLM gets starts with a note that the arguments were changed (see ToolInputGuardrailsConfig). Pass values that only the tool needs through InvocationParameters, not through the arguments.

      Parameters:
      rewrittenArguments - the new arguments of the tool, as a JSON object. null means no rewrite (a plain success). Anything else that is not a JSON object (malformed JSON, an array, a number) is a fatal failure of the guardrail: it aborts the invocation with a ToolInputGuardrailException, before the tool runs.
      Returns:
      the result of a successful tool input guardrail validation carrying the rewritten arguments
    • successWithArguments

      default ToolInputGuardrailResult successWithArguments(Map<String,?> rewrittenArguments)
      Produces a successful result with rewritten tool arguments, given as a map of argument names to values, which replace the original ones for the following guardrails and for the tool execution. The map is serialized to a JSON object with the same JSON codec that ToolInputGuardrailRequest.argumentsAsMap() parses the arguments with, so a guardrail can read the arguments as a map, change them and return the map, without escaping anything by hand:
      var arguments = new HashMap<>(request.argumentsAsMap());
      arguments.put("query", sanitize((String) arguments.get("query")));
      return successWithArguments(arguments);
      

      The chat memory keeps the tool call as the LLM made it, as with successWith(String). 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. A null map, or a value that has no JSON representation, is a fatal failure of the guardrail: it aborts the invocation with a ToolInputGuardrailException, before the tool runs.

      Parameters:
      rewrittenArguments - the new arguments of the tool, by name; must not be null
      Returns:
      the result of a successful tool input guardrail validation carrying the rewritten arguments
    • failure

      default ToolInputGuardrailResult failure(String message)
      Produces a non-fatal failure. The tool is not executed and the message is sent to the LLM verbatim as the content of an error tool result, so it is fully under your control.

      Do not quote the checked content in the message: that would send it to the LLM anyway. Do not leave it blank either: some LLM providers reject an empty tool result.

      Parameters:
      message - the message describing the failure, sent to the LLM
      Returns:
      the result of a failed tool input guardrail validation
    • failure

      default ToolInputGuardrailResult failure(String message, Throwable cause)
      Produces a non-fatal failure, see failure(String).
      Parameters:
      message - the message describing the failure, sent to the LLM
      cause - the exception that caused this failure; it is not sent to the LLM
      Returns:
      the result of a failed tool input guardrail validation
    • fatal

      default ToolInputGuardrailResult fatal(String message)
      Produces a fatal failure. It stops the other guardrails and aborts the AI service invocation with a ToolInputGuardrailException; the LLM does not get a chance to retry. Use it when retrying cannot help, e.g. a rejected page that would come back identical.
      Parameters:
      message - the message describing the failure
      Returns:
      the result of a fatally failed tool input guardrail validation
    • fatal

      default ToolInputGuardrailResult fatal(String message, Throwable cause)
      Produces a fatal failure, see fatal(String).
      Parameters:
      message - the message describing the failure
      cause - the exception that caused this failure
      Returns:
      the result of a fatally failed tool input guardrail validation