Interface Guardrail<P extends GuardrailRequest, R extends GuardrailResult<R>>
- Type Parameters:
P- The type of theGuardrailRequestR- The type of theGuardrailResult
- All Known Subinterfaces:
InputGuardrail, OutputGuardrail
- All Known Implementing Classes:
JsonExtractorOutputGuardrail, MessageModeratorInputGuardrail, PatternBasedPromptInjectionGuardrail
-
Method Summary
Modifier and TypeMethodDescriptiondefault Stringname()Returns the logical name of this guardrail.Validate the interaction between the model and the user in one of the two directions.default CompletableFuture<R> validateAsync(P request) Non-blocking counterpart ofvalidate(GuardrailRequest), invoked by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service modes.
-
Method Details
-
name
Returns the logical name of this guardrail. Wrappers/decorators can override this method to expose the wrapped guardrail's name.- Returns:
- the logical guardrail name
-
validate
-
validateAsync
Non-blocking counterpart ofvalidate(GuardrailRequest), invoked by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service modes.Returns a
CompletableFuture(rather than a plainCompletionStage) because guardrails sit in the cancellable execution path: the AI Service may cancel an in-flight validation when the invocation is cancelled (best-effort, mirroringToolExecutor). A guardrail backed by a cancellation-aware async client should honor that cancellation.The default implementation returns a failed future carrying
AsyncNotSupportedException: a guardrail must opt in to the non-blocking paths rather than have its (potentially blocking)validate(GuardrailRequest)silently run on the model-delivery thread. A guardrail that performs blocking I/O (e.g. calling a remote moderation or PII service) must override this method to return a future completed off the calling thread (for instance via an async client, orCompletableFuture.supplyAsync(..., executor)onto its own executor). A guardrail that does not perform blocking I/O may simply returnCompletableFuture.completedFuture(validate(request)).This mirrors
ChatMemoryStoreandToolExecutor, whose asynchronous counterparts likewise report "not implemented" through the returned future rather than throwing.- Parameters:
request- The parameters of the request or the response to be validated- Returns:
- A
CompletableFuturethat completes with the result of the validation - Since:
- 1.20.0
-