Fix BadRequestError
in Pydantic
✅ Solution
The "BadRequestError" in pydantic, especially with `model_validator` and `mode='wrap'`, often arises because the validator is receiving the raw input data (e.g., a dictionary) instead of the partially validated model instance when it expects the latter. To fix this, ensure your validator's first argument is correctly annotated with `cls` when used as a class method or with `self` when used as an instance method, and appropriately typed with `RootModel[YourModel]` or `YourModel` to signal what type the validator expects, which helps pydantic pass the validated model. Additionally, confirm that the `handler` function within the wrapped validator correctly handles and modifies (or returns) the partially validated model before returning it for final validation.
Related Issues
Real GitHub issues where developers encountered this error: