Validation error responses tell users what went wrong when they send wrong data. This helps fix mistakes quickly.
0
0
Validation error responses in FastAPI
Introduction
When a user sends a form with missing or wrong information.
When an API receives data that does not match the expected format.
When you want to guide users to correct their input before processing.
When building a web service that needs to be clear about input rules.
Syntax
FastAPI
from fastapi import FastAPI, Request from pydantic import BaseModel from fastapi.responses import JSONResponse from fastapi.exceptions import RequestValidationError app = FastAPI() @app.exception_handler(RequestValidationError) async def validation_exception_handler(request: Request, exc: RequestValidationError): return JSONResponse( status_code=422, content={"detail": exc.errors(), "body": exc.body}, )
This code shows how to catch validation errors and send a clear JSON response.
FastAPI uses Pydantic models to check data automatically.
Examples
This example defines a data model and an endpoint that expects valid data. If data is wrong, FastAPI sends a validation error automatically.
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @app.post("/items/") async def create_item(item: Item): return item
This example shows how to customize the error response message and status code.
FastAPI
from fastapi import FastAPI, Request from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse app = FastAPI() @app.exception_handler(RequestValidationError) async def custom_validation_handler(request: Request, exc: RequestValidationError): return JSONResponse( status_code=400, content={"message": "Oops! Your data is not valid.", "errors": exc.errors()}, )
Sample Program
This FastAPI app defines a User model and an endpoint to create users. If the input data is invalid, it returns a JSON response with details about the errors.
FastAPI
from fastapi import FastAPI, Request from pydantic import BaseModel from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse app = FastAPI() class User(BaseModel): username: str age: int @app.post("/users/") async def create_user(user: User): return {"message": "User created", "user": user} @app.exception_handler(RequestValidationError) async def validation_exception_handler(request: Request, exc: RequestValidationError): return JSONResponse( status_code=422, content={"detail": exc.errors(), "body": exc.body}, )
OutputSuccess
Important Notes
FastAPI automatically validates request data using Pydantic models.
You can customize error responses by writing an exception handler for RequestValidationError.
Validation errors usually return HTTP status 422 (Unprocessable Entity).
Summary
Validation error responses help users fix wrong input.
FastAPI uses Pydantic to check data and sends errors automatically.
You can customize error messages by handling RequestValidationError.