Skip to main content

OnFail Actions

Validators ship with several out of the box on_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 of OnFailAction. 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 here
Now suppose some unhinged LLM returns damn you!, in ths scenario:
  • OnFailAction.REASK an LLM will be reasked to correct it’s output based on the error_message provided to FailResult. 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 set num_reasks on the guard() call to determine how many times we retry.
  • OnFailAction.FIX the value is replaced with fix_value that is provided to FailResult. In this example value from the LLM damn you! will be returned as you!.
  • OnFailAction.FILTER if used in structured data generation we do not display the field that fails validation (more on this below). In this example value from the LLM damn you! will return an empty response.
  • OnFailAction.REFRAIN we do not return anything as the validation is deemed unsafe for end users. In this example value from the LLM damn you! will return an empty response.
  • OnFailAction.NOOP the value is returned as is and failures are logged in the history. In this example value we return the value as is damn you!
  • OnFailAction.EXCEPTION during a guard execution or direct validation call we raise an error indicating the validation failed.
  • OnFailAction.FIX_REASK during a fix reask, we first perform the same action as OnFailAction.FIX and re-validate the output. If it fails we run a OnFailAction.REASK action, otherwise we return the passed validation.

Structured Data

Using OnFail actions is powerful when also working with structured data as we can determine how to treat each field’s validation failure.