Interface ToolOutputGuardrail

All Superinterfaces:
Guardrail<ToolOutputGuardrailRequest, ToolOutputGuardrailResult>

@Experimental public interface ToolOutputGuardrail extends Guardrail<ToolOutputGuardrailRequest, ToolOutputGuardrailResult>
A tool output guardrail is a rule applied after a tool was executed, to validate or rewrite the result before the LLM sees it.

Unlike OutputGuardrail, 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 result of a tool execution, including error results (ToolOutputGuardrailRequest.isError()).

      Implementations must not modify the request.

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

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

      default ToolOutputGuardrailResult successWith(@Nullable String rewrittenText)
      Produces a successful result with rewritten text: the text contents of the tool result are replaced, the other contents and the error flag are kept.
      Parameters:
      rewrittenText - the new text of the tool result. null means no rewrite (a plain success). To remove the text, prefer a short placeholder to an empty text, which some LLM providers reject
      Returns:
      the result of a successful tool output guardrail validation carrying the rewritten text
    • successWith

      default 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 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:
      the result of a successful tool output guardrail validation carrying the rewritten contents
      Throws:
      IllegalArgumentException - if rewrittenContents is null or empty, or contains null
    • failure

      default ToolOutputGuardrailResult failure(String message)
      Produces a non-fatal failure. The tool result is replaced by an error tool result, and the message is sent to the LLM verbatim as the content of an error tool result, so it is fully under your control. The one addition: when an input guardrail changed the arguments of the call, the note that says so comes before the message (see ToolInputGuardrailsConfig), since the changed arguments may be why the result was rejected.

      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 output guardrail validation
    • failure

      default ToolOutputGuardrailResult 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 output guardrail validation
    • fatal

      default ToolOutputGuardrailResult fatal(String message)
      Produces a fatal failure. It stops the other guardrails and aborts the AI service invocation with a ToolOutputGuardrailException; 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 output guardrail validation
    • fatal

      default ToolOutputGuardrailResult 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 output guardrail validation