Interface ToolOutputGuardrail
- All Superinterfaces:
Guardrail<ToolOutputGuardrailRequest, ToolOutputGuardrailResult>
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 Summary
Modifier and TypeMethodDescriptiondefault ToolOutputGuardrailResultProduces a non-fatal failure.default ToolOutputGuardrailResultProduces a non-fatal failure, seefailure(String).default ToolOutputGuardrailResultProduces a fatal failure.default ToolOutputGuardrailResultProduces a fatal failure, seefatal(String).default ToolOutputGuardrailResultsuccess()Produces a successful result without any rewrite.default ToolOutputGuardrailResultsuccessWith(@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.default ToolOutputGuardrailResultsuccessWith(List<Content> rewrittenContents, boolean isError) Produces a successful result that replaces all the contents of the tool result and its error flag.validate(ToolOutputGuardrailRequest request) Validates the result of a tool execution, including error results (ToolOutputGuardrailRequest.isError()).Methods inherited from interface Guardrail
name, validateAsync
-
Method Details
-
validate
Validates the result of a tool execution, including error results (ToolOutputGuardrailRequest.isError()).Implementations must not modify the request.
- Specified by:
validatein interfaceGuardrail<ToolOutputGuardrailRequest, ToolOutputGuardrailResult>- Parameters:
request- the request- Returns:
- the result of the validation
-
success
Produces a successful result without any rewrite.- Returns:
- the result of a successful tool output guardrail validation
-
successWith
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.nullmeans 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
Produces a successful result that replaces all the contents of the tool result and its error flag.Passing
isError == trueturns 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 benullor empty (a tool result always has contents) and must not containnullisError- whether the rewritten tool result is an error- Returns:
- the result of a successful tool output guardrail validation carrying the rewritten contents
- Throws:
IllegalArgumentException- ifrewrittenContentsisnullor empty, or containsnull
-
failure
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 (seeToolInputGuardrailsConfig), 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
Produces a non-fatal failure, seefailure(String).- Parameters:
message- the message describing the failure, sent to the LLMcause- the exception that caused this failure; it is not sent to the LLM- Returns:
- the result of a failed tool output guardrail validation
-
fatal
Produces a fatal failure. It stops the other guardrails and aborts the AI service invocation with aToolOutputGuardrailException; 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
Produces a fatal failure, seefatal(String).- Parameters:
message- the message describing the failurecause- the exception that caused this failure- Returns:
- the result of a fatally failed tool output guardrail validation
-