Symfony Validation Attributes
Instructor uses the Symfony Validator component under the hood. Add constraint attributes to your response model to enforce field-level rules:For a full list of available constraints, see the Symfony Validation documentation.
Retries
Retries are configured on the runtime, not on individual requests. When validation fails and retries are available, Instructor sends the validation errors back to the model and asks it to try again:maxRetries value controls how many additional attempts are allowed after the first
one. With maxRetries(3), Instructor will try up to 4 times total (1 initial + 3 retries).
If all attempts fail validation, Instructor throws an exception.
name: "JX" and age: -28. Validation
catches both issues, and the retry prompt tells the model what went wrong so it can
return name: "Jason" and age: 28 on the next attempt.
Custom Validation With ValidationMixin
For object-level validation logic that goes beyond simple field constraints, use the
ValidationMixin trait. Implement a validate() method that returns a ValidationResult:
ValidationResult class provides several factory methods:
| Method | Purpose |
|---|---|
ValidationResult::valid() | Indicates the object passed validation |
ValidationResult::invalid($errors) | Wraps one or more ValidationError instances |
ValidationResult::fieldError($field, $value, $message) | Shorthand for a single field error |
ValidationResult::make($errors, $message) | General-purpose constructor |
ValidationResult::merge($results) | Combines multiple validation results |
Custom Validation With Symfony #[Assert\Callback]
You can also use Symfony’s #[Assert\Callback] attribute directly for full access to
the Symfony validation context. This is useful when you want to leverage Symfony’s
violation builder API:
See the Symfony Callback constraint docs for more details on the violation builder API.
How Retries Work
When a response fails validation, Instructor:- Collects all validation errors (from Symfony constraints,
ValidationMixin, or both). - Formats them into a retry prompt that describes what went wrong.
- Appends the retry prompt to the conversation history.
- Sends the updated conversation back to the model for another attempt.
"JSON generated incorrectly, fix following errors:\n",
followed by the list of violations. You can customize it through StructuredOutputConfig: