{"id":911,"hash":"bf74819b79f01d86eab086cac2f686ce3b43f9efe35dae443d840133559a3684","pattern":"Displaying of FastAPI validation errors to end users","full_message":"I'm looking for some library or example of code to format FastAPI validation messages into human-readable format. E.g. this endpoint:\n\n@app.get(\"/\")\nasync def hello(name: str):\n    return {\"hello\": name}\n\nWill produce the next json output if we miss name query parameter:\n\n{ \n    \"detail\":[ \n        { \n            \"loc\":[ \n                \"query\",\n                \"name\"\n            ],\n            \"msg\":\"field required\",\n            \"type\":\"value_error.missing\"\n        }\n    ]\n}\n\nSo my questions is, how to:\n\nTransform it into something like \"name field is required\" (for all kinds of possible errors) to show in toasts.\nUse it to display form validation messages\nGenerate forms themselves from api description if it's possible","ecosystem":"pypi","package_name":"swagger","package_version":null,"solution":"FastAPI has a great Exception Handling, so you can customize your exceptions in many ways.\n\nYou can raise an HTTPException, HTTPException is a normal Python exception with additional data relevant for APIs. But you can't return it you need to raise it because it's a Python exception\n\nfrom fastapi import HTTPException\n...\n@app.get(\"/\")\nasync def hello(name: str):\n    if not name:\n        raise HTTPException(status_code=404, detail=\"Name field is required\")\n    return {\"Hello\": name}\n\nBy adding name: str as a query parameter it automatically becomes required so you need to add Optional\n\nfrom typing import Optional\n...\n@app.get(\"/\")\nasync def hello(name: Optional[str] = None):\n    error = {\"Error\": \"Name field is required\"}\n    if name:\n        return {\"Hello\": name}\n    return error\n\n$ curl 127.0.0.1:8000/?name=imbolc\n{\"Hello\":\"imbolc\"}\n...\n$ curl 127.0.0.1:8000\n{\"Error\":\"Name field is required\"}\n\nBut in your case, and i think this is the best way to handling errors in FastAPI overriding the validation_exception_handler:\n\nfrom fastapi import FastAPI, Request, status\nfrom fastapi.encoders import jsonable_encoder\nfrom fastapi.exceptions import RequestValidationError\nfrom fastapi.responses import JSONResponse\n...\n@app.exception_handler(RequestValidationError)\nasync def validation_exception_handler(request: Request, exc: RequestValidationError):\n    return JSONResponse(\n        status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,\n        content=jsonable_encoder({\"detail\": exc.errors(), \"Error\": \"Name field is missing\"}),\n    )\n...\n@app.get(\"/\")\nasync def hello(name: str):\n    return {\"hello\": name}\n\nYou will get a response like this:\n\n$ curl 127.0.0.1:8000\n\n {\n   \"detail\":[\n      {\n         \"loc\":[\n            \"query\",\n            \"name\"\n         ],\n         \"msg\":\"field required\",\n         \"type\":\"value_error.missing\"\n      }\n   ],\n   \"Error\":\"Name field is missing\"\n}\n\nYou can customize your content however if you like:\n\n{\n\"Error\":\"Name field is missing\",\n   \"Customize\":{\n      \"This\":\"content\",\n      \"Also you can\":\"make it simpler\"\n   }\n}","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/58642528/displaying-of-fastapi-validation-errors-to-end-users","votes":28,"created_at":"2026-04-19T04:51:59.335769+00:00","updated_at":"2026-04-19T04:51:59.335769+00:00"}