OnFail Actions
Validators ship with several out of the boxon_fail policies. The OnFailAction specifies the corrective action that should be taken if the quality criteria is not met. The corrective action can be one of the following:
Example
Let’s assess the output of a trivial validator in diffent cases ofOnFailAction. Take the following Validator a basic implementation of a Toxic Language Validator:
For more information on how to write Custom Validators refer to our guide hereNow suppose some unhinged LLM returns
damn you!, in ths scenario:
OnFailAction.REASKan LLM will be reasked to correct it’s output based on theerror_messageprovided toFailResult. In this example it will reask the LLM with a reask prompt which includes the error message:Value 'damn you!' contains toxic language including words: ["asshole","damn"] which is not allowed.. You can setnum_reaskson theguard()call to determine how many times we retry.OnFailAction.FIXthe value is replaced withfix_valuethat is provided toFailResult. In this example value from the LLMdamn you!will be returned asyou!.OnFailAction.FILTERif used in structured data generation we do not display the field that fails validation (more on this below). In this example value from the LLMdamn you!will return an empty response.OnFailAction.REFRAINwe do not return anything as the validation is deemed unsafe for end users. In this example value from the LLMdamn you!will return an empty response.OnFailAction.NOOPthe value is returned as is and failures are logged in the history. In this example value we return the value as isdamn you!OnFailAction.EXCEPTIONduring a guard execution or direct validation call we raise an error indicating the validation failed.OnFailAction.FIX_REASKduring a fix reask, we first perform the same action asOnFailAction.FIXand re-validate the output. If it fails we run aOnFailAction.REASKaction, otherwise we return the passed validation.
Structured Data
UsingOnFail actions is powerful when also working with structured data as we can determine how to treat each field’s validation failure.