Annotation Interface InputGuardrail
Input guardrails protect agents from malicious or malformed input by validating and sanitizing data before processing.
Usage on Method
@ActionSpec(type = ActionType.LOCAL, description = "Process user message")
@InputGuardrail(
maxLength = 10000,
validators = PiiInputValidator.class,
sanitizers = PiiInputSanitizer.class,
onFailure = InputGuardrail.FailureAction.SANITIZE,
blockPatterns = {"ignore previous", "system prompt"}
)
public String processMessage(String message) {
// message is validated and sanitized before this runs
}
Usage on Type (Role-level default)
@InputGuardrail(
maxLength = 5000,
validators = PiiInputValidator.class,
onFailure = InputGuardrail.FailureAction.REJECT
)
public class UserInputRole extends Role {
// All actions inherit these guardrails
}
Built-in PII Extensions
PiiInputValidator detects common PII and
PiiInputSanitizer replaces it with stable tokens.
Applications may supply their own implementations of
InputValidator and
InputSanitizer.
- Author:
- TnsAI Team
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumContent categories for moderation.static enumActions on guardrail failure. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionString[]Regex patterns that input must match.Content categories to block.String[]Regex patterns to block (prompt injection protection).booleanWhether to enable prompt injection detection.Custom error message on failure.doubleInjection detection sensitivity (0.0 - 1.0).JSON schema for validation (if input is JSON).booleanWhether to log validation failures.intMaximum input length in characters.intMinimum input length in characters.booleanWhether to enable content moderation.Action to take on validation failure.Class<?>[]Sanitizer classes to apply (in order).Class<?>[]Validator classes to apply.
-
Element Details
-
maxLength
int maxLengthMaximum input length in characters.0 means no limit.
- Returns:
- max length
- Default:
0
-
minLength
int minLengthMinimum input length in characters.- Returns:
- min length
- Default:
0
-
validators
Class<?>[] validatorsValidator classes to apply.Each class must implement
InputValidatorand expose a public no-argument constructor. Validators run in declaration order against every runtime String value, including values declared asObject. Booleans, default-rendering enums, and exact immutable JDK number types are validated through stable textual forms. Custom-rendering enums, containers, and arbitrary objects fail closed unless the policy isInputGuardrail.FailureAction.WARNbecause they cannot be inspected safely.- Returns:
- validator classes
- Default:
{}
-
sanitizers
Class<?>[] sanitizersSanitizer classes to apply (in order).Each class must implement
InputSanitizerand expose a public no-argument constructor. They run after a validator rejects a value whenonFailure()isInputGuardrail.FailureAction.SANITIZE.- Returns:
- sanitizer classes
- Default:
{}
-
blockPatterns
String[] blockPatternsRegex patterns to block (prompt injection protection).Input matching any pattern is rejected. Runtime enforcement limits each pattern and inspected input to 1,024 characters each, and rejects backreferences, nested quantifiers, comments mode, multi-token escapes, multiple alternation groups, and more than one backtracking quantifier per pattern before matching. Fixed repetitions and possessive quantifiers may be repeated. A single non-capturing alternation group, lookarounds, other inline flags, and lazy or possessive quantifier modifiers remain supported.
- Returns:
- block patterns
- Default:
{}
-
allowPatterns
String[] allowPatternsRegex patterns that input must match.Input not matching is rejected. The same bounded, backtracking-safe regex subset documented by
blockPatterns()applies.- Returns:
- allow patterns
- Default:
{}
-
jsonSchema
String jsonSchemaJSON schema for validation (if input is JSON).Path to schema file or inline schema.
- Returns:
- JSON schema
- Default:
""
-
detectInjection
boolean detectInjectionWhether to enable prompt injection detection.- Returns:
- true to detect injection
- Default:
true
-
injectionThreshold
double injectionThresholdInjection detection sensitivity (0.0 - 1.0).Higher values are more strict.
- Returns:
- sensitivity threshold
- Default:
0.7
-
moderateContent
boolean moderateContentWhether to enable content moderation.- Returns:
- true to moderate content
- Default:
false
-
blockCategories
InputGuardrail.ContentCategory[] blockCategoriesContent categories to block.- Returns:
- blocked categories
- Default:
{}
-
onFailure
InputGuardrail.FailureAction onFailureAction to take on validation failure.- Returns:
- failure action
- Default:
REJECT
-
errorMessage
String errorMessageCustom error message on failure.- Returns:
- error message
- Default:
""
-
logFailures
boolean logFailuresWhether to log validation failures.- Returns:
- true to log
- Default:
true
-