fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

RequestValidationError is **not** a sub-class of Pydantic's ValidationError

Open Kludex opened this issue 1 year ago • 7 comments

Discussed in https://github.com/tiangolo/fastapi/discussions/10421

Originally posted by valentinoli October 10, 2023

First Check

  • [X] I added a very descriptive title here.
  • [X] I used the GitHub search to find a similar question and didn't find it.
  • [X] I searched the FastAPI documentation, with the integrated search.
  • [X] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to FastAPI but to Pydantic.
  • [X] I already checked if it is not related to FastAPI but to Swagger UI.
  • [X] I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

@app.exception_handler(ValidationError)
async def validation_exception_handler(request: Request, exc: ValidationError):
    return PlainTextResponse(
        str(exc), status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
    )

Description

I was trying to handle both RequestValidationError and ValidationError by writing a custom handler for ValidationError but that doesn't work.

The docs state that

RequestValidationError is a sub-class of Pydantic's ValidationError

However, looking at FastAPI source code reveals that this is not the case.

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.101.0

Pydantic Version

2.1.1

Python Version

3.11.3

Additional Context

No response

Kludex avatar Oct 11 '23 08:10 Kludex

Should we fix the docs or the code? IMO having Pydantic's error makes more sense.

iudeen avatar Oct 11 '23 17:10 iudeen

Documentation. But I'm just bringing it to @tiangolo 's attention.

Kludex avatar Oct 11 '23 18:10 Kludex

hey Please choose this format

Here is main.py

from app.infrastructure.exception import validation_exception_handler

app = FastAPI(
    title=settings.PROJECT_NAME,
    openapi_url=f"{settings.API_V1_STR}openapi.json"
)

@app.exception_handler(RequestValidationError)
async def custom_validation_exception_handler(request, exc):
    return await validation_exception_handler(request, exc)

Here is exception.py

from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from fastapi import Request

async def validation_exception_handler(request: Request, exc: RequestValidationError):
    error_messages = []
    for error in exc.errors():
        if error["type"] == "value_error.email":
            error_messages.append({"status_code": 422, "detail": "Invalid email address"})
        else:
            error_messages.append({"status_code": 422, "detail": error["msg"]})
    print(error_messages[0])
    return JSONResponse(
        status_code=422,
        content=error_messages[0],
    )

Here is my design pattern publically available https://github.com/FaizanMunsaf/FastApi-MVC

FaizanMunsaf avatar Oct 17 '23 08:10 FaizanMunsaf

+1 here;

The example code has become totally wrong since the str(exc) doesn't return the human readable version of the error(s).

It would be nice if the RequestValidationError could output the same text as ValidationError regardless of the inheritance.

sanzoghenzo avatar Dec 18 '23 10:12 sanzoghenzo

This PR ( #11176 ) seems to fix it.

codespearhead avatar Apr 01 '24 17:04 codespearhead

Any tips to work around this? It would be great to be able to to str(exc) and other goodies from the pydantic class

btonasse avatar Sep 07 '24 14:09 btonasse

Related to #11542 is a discussion I posted here (#11923) that dovetails with this same issue. The test in the discussion can be dropped into the FastAPI source and will fail because of how the default request_validation_exception_handler handles serializing the error.

I wonder if utilizing the ctx and input fields from Pydantic V2 ValidationError should be "on" by default:

(For reference, include_url=False here): https://github.com/fastapi/fastapi/blob/master/fastapi/_compat.py#L133

Pydantic V2 ErrorDetails for reference: https://docs.pydantic.dev/latest/api/pydantic_core/#pydantic_core.ErrorDetails

Perhaps this needs to be a separate issue, but it all falls within the RequestValidationError/ValidationError handling.

bertomaniac avatar Sep 16 '24 11:09 bertomaniac