How can I reformat default response for validation errors? I find the default formatting too verbose for my app, and wanted to return errors in different structure.
I thought of adding error handler explicitly for pydantic.ValidationError but this is ignored as the error is caught before that and handled internally by FastAPI:
Is this possible to override default formatter and handle ValidationError on my own? I tried to search through issues and docs but couldn't find anything related.
You could add a custom handler for 422 errors: https://fastapi.tiangolo.com/tutorial/handling-errors/#installing-custom-handlers
And extract the original message from the HTTPExcpetion.detail.
Let me know if that works for you.
Otherwise, we can think about refactoring how ValidationErrors are handled to allow custom handlers (without passing through the HTTPException error).
Some way of providing custom handler for validation errors would be nice. Currently I added handler for 422 errors but it doesn鈥檛 solve the issue entirely. I could reformat the dict, however it would make more sense to just get the original exception somehow and prices that. The other thing is, adding handler for 422 status doesn鈥檛 overwrite APIdocs for that status even if I added custom 422 definition in responses for @api.route(). Is there any way to override the default until some more flexible validation error handler is available?
First, about having custom validation handlers, I'll take it as a feature request.
For overriding 422 errors, I would need to check it. I think that might be another feature request/issue too.
This is supported now in the latest versions. :tada:
You can now add handlers for RequestValidationError and get the full Pydantic ValidationError: https://fastapi.tiangolo.com/tutorial/handling-errors/#override-request-validation-exceptions
Make sure to update your FastAPI version. The current latest is 0.27.0.
I'll close this issue now, but feel free to add more comments or create new issues.
This is supported now in the latest versions. 馃帀
You can now add handlers for
RequestValidationErrorand get the full PydanticValidationError: https://fastapi.tiangolo.com/tutorial/handling-errors/#override-request-validation-exceptionsMake sure to update your FastAPI version. The current latest is
0.27.0.I'll close this issue now, but feel free to add more comments or create new issues.
Docs builders don't know about exception overriding.
Please add an example this page.
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
return PlainTextResponse(str(exc), status_code=422)

Most helpful comment
This is supported now in the latest versions. :tada:
You can now add handlers for
RequestValidationErrorand get the full PydanticValidationError: https://fastapi.tiangolo.com/tutorial/handling-errors/#override-request-validation-exceptionsMake sure to update your FastAPI version. The current latest is
0.27.0.I'll close this issue now, but feel free to add more comments or create new issues.