Validation
Basic validation
Instructor validates results of LLM response against validation rules specified in your data model.
NOTE: For further details on available validation rules, check Symfony Validation constraints.
Max Retries
In case maxRetries parameter is provided and LLM response does not meet validation criteria, Instructor will make subsequent inference attempts until results meet the requirements or maxRetries is reached.
Instructor uses validation errors to inform LLM on the problems identified in the response, so that LLM can try self-correcting in the next attempt.
Custom Validation
You can easily add custom validation code to your response model by using ValidationTrait
and defining validation logic in validate()
method.
Note that method validate()
has to return:
- an empty array if the object is valid,
- or an array of validation violations.
This information will be used by LLM to make subsequent attempts to correct the response.
Custom Validation via Symfony #[Assert/Callback]
Instructor uses Symfony validation component to validate extracted data.
You can use #[Assert/Callback]
annotation to build fully customized validation logic.
NOTE: See Symfony docs for more details on how to use Callback constraint.