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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresents a tool output guardrail failure.Nested classes/interfaces inherited from interface GuardrailResult
GuardrailResult.Result -
Method Summary
Modifier and TypeMethodDescriptionbooleanGets 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\nin chain order.<F extends GuardrailResult.Failure>
List<F> failures()inthashCode()booleanWhether this result rewrites the contents and the error flag (successWith(List, boolean)), as opposed to the text only (successWith(String)).result()The result of the guardrailGets the contents of the tool result after applying this result to the given request.booleanGets the error flag of the tool result after applying this result to the given request.static ToolOutputGuardrailResultsuccess()Gets a successful tool output guardrail result.@Nullable StringThe message of the successful resultstatic ToolOutputGuardrailResultsuccessWith(@Nullable String rewrittenText) Produces a successful result with rewritten text.static ToolOutputGuardrailResultsuccessWith(List<Content> rewrittenContents, boolean isError) Produces a successful result that replaces all the contents of the tool result and its error flag.toString()Methods inherited from interface GuardrailResult
asString, getFirstFailureException, hasRewrittenResult, isFatal, isSuccess, validatedBy
-
Method Details
-
success
Gets a successful tool output guardrail result.- Returns:
- a successful result without any rewrite
-
successWith
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. Whennull, 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 == trueturns 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 benullor empty (a tool result always has contents) and must not containnullisError- whether the rewritten tool result is an error- Returns:
- a successful result carrying the rewritten contents and error flag
- Throws:
IllegalArgumentException- ifrewrittenContentsisnullor empty, or containsnull
-
result
Description copied from interface:GuardrailResultThe result of the guardrail- Specified by:
resultin interfaceGuardrailResult<ToolOutputGuardrailResult>
-
successfulText
Description copied from interface:GuardrailResultThe message of the successful result- Specified by:
successfulTextin interfaceGuardrailResult<ToolOutputGuardrailResult>- Returns:
- the rewritten text of the tool result: the given text for a text rewrite, the text contents joined
with
\nfor a contents rewrite (an empty string if there are none), ornullwhen 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
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
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
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\nin chain order. No framework text is added.- Returns:
- the joined failure messages, or an empty string if there are no failures
-
failures
- Specified by:
failuresin interfaceGuardrailResult<ToolOutputGuardrailResult>- Returns:
- The list of failures eventually resulting from a set of validations.
-
toString
-
equals
-
hashCode
-