Skip to content

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

@Kludex

Description

@Kludex

Discussed in #10421

Originally posted by valentinoli October 10, 2023

First Check

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

Commit to Help

  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation about how to use FastAPIquestionQuestion or problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions