Class AbstractToolGuardrailExecutor<C extends GuardrailsConfig, P extends GuardrailRequest<P>, R extends GuardrailResult<R>, G extends Guardrail<P,R>, E extends GuardrailExecutedEvent<P,R,G>, F extends GuardrailResult.Failure>
java.lang.Object
dev.langchain4j.guardrail.AbstractGuardrailExecutor<C,P,R,G,E,F>
dev.langchain4j.guardrail.AbstractToolGuardrailExecutor<C,P,R,G,E,F>
- Type Parameters:
C- The type ofGuardrailsConfigto use for configurationP- The type ofGuardrailRequestto validateR- The type ofGuardrailResultto returnG- The type ofGuardrails being executedE- The type ofGuardrailExecutedEventto be firedF- The type ofGuardrailResult.Failureto return
- All Implemented Interfaces:
GuardrailExecutor<C,P, R, G, E>
- Direct Known Subclasses:
ToolInputGuardrailExecutor, ToolOutputGuardrailExecutor
public abstract sealed class AbstractToolGuardrailExecutor<C extends GuardrailsConfig, P extends GuardrailRequest<P>, R extends GuardrailResult<R>, G extends Guardrail<P,R>, E extends GuardrailExecutedEvent<P,R,G>, F extends GuardrailResult.Failure>
extends AbstractGuardrailExecutor<C,P,R,G,E,F>
permits ToolInputGuardrailExecutor, ToolOutputGuardrailExecutor
Base class of the
GuardrailExecutors of tool guardrails, which share the same semantics:
- the guardrails run once, with no retry or reprompt;
- a non-fatal failure does not stop the chain and is returned, not thrown, so that the caller can turn it into an error tool result;
- a fatal failure stops the chain and is thrown (async: fails the future) as the
GuardrailExceptioncreated byAbstractGuardrailExecutor.createGuardrailException(String, Throwable), which names the tool in its message and carries the tool call it stopped (toolExecutionRequest()). This covers a fatal result, a guardrail that throws, and, for the input guardrails, a rewrite to something that is not a JSON object.
- Since:
- 1.25.0
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractGuardrailExecutor
AbstractGuardrailExecutor.GuardrailExecutorBuilder<C,R, P, G, E, B> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractToolGuardrailExecutor(C config, List<G> guardrails) -
Method Summary
Modifier and TypeMethodDescriptionExecutes the guardrails on the given request.executeAsync(P request) Non-blocking counterpart ofexecute(GuardrailRequest).protected RRecords on the exception of a guardrail that threw the tool call it validated.protected CompletableFuture<R> validateAsync(P request, G guardrail, CancellationChain chain) Non-blocking counterpart ofvalidate(GuardrailRequest, Guardrail).
-
Constructor Details
-
AbstractToolGuardrailExecutor
-
-
Method Details
-
execute
Executes the guardrails on the given request.- Parameters:
request- The request to validate- Returns:
- The result of the validation. A non-fatal failure is returned, not thrown.
- Throws:
GuardrailException- If a guardrail returned a fatal failure, or threw
-
executeAsync
Non-blocking counterpart ofexecute(GuardrailRequest). A fatal failure completes the future exceptionally with aGuardrailException.- Parameters:
request- TheGuardrailRequestto validate- Returns:
- A
CompletableFuturethat completes with theGuardrailResultof the validation
-
validate
Records on the exception of a guardrail that threw the tool call it validated.- Overrides:
validatein classAbstractGuardrailExecutor<C extends GuardrailsConfig, P extends GuardrailRequest<P>, R extends GuardrailResult<R>, G extends Guardrail<P,R>, E extends GuardrailExecutedEvent<P, R, G>, F extends GuardrailResult.Failure> - Parameters:
request- TheGuardrailRequestto validateguardrail- TheGuardrailto evaluate against- Returns:
- The
GuardrailResultof the validation
-
validateAsync
Non-blocking counterpart ofvalidate(GuardrailRequest, Guardrail).- Overrides:
validateAsyncin classAbstractGuardrailExecutor<C extends GuardrailsConfig, P extends GuardrailRequest<P>, R extends GuardrailResult<R>, G extends Guardrail<P,R>, E extends GuardrailExecutedEvent<P, R, G>, F extends GuardrailResult.Failure>
-