Block (default) – reject the text and return a Left error.
Fix – replace secrets with typed placeholders and continue (e.g. [REDACTED_OPENAI_KEY]).
Warn – allow the text through unchanged; the caller may inspect the Right and decide what to log.
Example usage:
// Block any input that contains a credential
new GuardrailMiddleware(input = Seq(SecretLeakGuardrail()), output = Nil)
// Mask secrets automatically and let the query proceed
new GuardrailMiddleware(input = Seq(SecretLeakGuardrail.masking), output = Nil)
// Also scrub LLM responses
new GuardrailMiddleware(input = Nil, output = Seq(SecretLeakGuardrail.masking))
// each on an agent: Agent.builder("assistant", client).withMiddleware(guardrails).build()
Value parameters
onFail
Action to take when a secret is detected (default: Block)
secretTypes
Secret types to detect (default: all common provider keys)
Validates value inside an agent, as validate(value) does, with the context of the stage that runs it: an input guardrail's is the turn's admission (AdmissionContext), an output guardrail's the afterAgent hook's ModelContext. A guardrail that calls a model overrides this to call through context.accounted, so the call is counted in the thread's usage and on the run's terminal event even when the task fails (design §4.22); one that combines guardrails passes context on to each. GuardrailMiddleware and GuardrailReviewMiddleware call this form. A guardrail that needs more than accounting matches on the context it was given.
Validates value inside an agent, as validate(value) does, with the context of the stage that runs it: an input guardrail's is the turn's admission (AdmissionContext), an output guardrail's the afterAgent hook's ModelContext. A guardrail that calls a model overrides this to call through context.accounted, so the call is counted in the thread's usage and on the run's terminal event even when the task fails (design §4.22); one that combines guardrails passes context on to each. GuardrailMiddleware and GuardrailReviewMiddleware call this form. A guardrail that needs more than accounting matches on the context it was given.
Named apart from the Stable validate(value) so that MiMa, which skips an @Experimental method with every overload of its name, still checks that one (#2835).