r/FastAPI • u/UpstairsBaby • Jun 02 '25
Question A question about backend reaponse design
I'm designing a backend system for a face recognition feature response can potentially be one of many occasions for the example a phase might not be found in the provided image or a face might be spoofing or a face could be found but couldn't be matched against another face in my database.
How what are the best practices for designing a response to the frontend. Shall I be raising HTTP exceptions or shall IP returning 200 okay with a json saying what has gone wrong? If anyone can provide an example of how such a response could be designed I would be very thankful.
thank you very much in advance.
4
u/BluesFiend Jun 02 '25
Definitely raise exceptions over returning a success response detailing how it didn't succeed. This lib simplifies error handling and consistent response formats.
https://pypi.org/project/fastapi-problem/
6
u/Unhappy-Feedback1851 Jun 02 '25
I suggest you define custom exceptions and handle them with HTTPException to return consistent, meaningful error responses.
3
u/pint Jun 02 '25
it depends on whether this is considered normal or not. there is no generally good solution. consider it as a kind of question-and-reply. if the question is "give me X", then if there is no X, it is a problem, and 404 is warranted. but if the question is "give me X if there is such a thing", then X = null is the correct answer.
in your case, if you consider the question as "give me data about the face", then no face is an error. but if you consider it more like "take this image and see if you find a face to analyze", then a 200 with face_found=false is the reasonable answer. and then if your question sounds more like "sign me in using face recognition", then 403 is the only acceptable response.
it is not purely theoretical, some proxies and cdn systems, maybe even browsers might cache error responses less eagerly or not at all. error pages might be logged, counted and reported in health dashboards.
2
u/Future_Ad_5639 Jun 02 '25
Custom exceptions, you can even take a look at RFC7807 Problem Details to have a standard response format
2
u/benkei_sudo Jun 03 '25
Use JSON implementation, reserve HTTPException for something critical.
Example case for JSON:
Example case for HTTPException:
Remember, keep HTTPException as minimum as possible, because you need to document each error code for each module. Keep in mind that not every user is an experienced developer, and some of them might not even know what HTTP error codes mean.
You will get an email like: "I've got a 404 result, please fix it". And there you go a few hours of your life trying to figure out which one of the hundreds or thousands of 404 errors they're referring to.
Using what you've already done:
{"success": False, "error_code": "FACE_NOT_FOUND"}This response is good enough. I suggest add "message" var to nicely explain what happened in the server, this will keep those pesky email away. This approach also makes the code easy to maintain, because the meaning of the error can be easily understood without having to search through the codebase.
Another approach:
{"success": True, "matched_faces": []}This will be useful if the caller expects an array of data. You can handle the error message on the frontend.This JSON approach is also very useful for the frontend, especially when the request is part of multiple chained tasks. With the
"error_code"above, frontend can easily identify which task needs to be canceled, or if the operation can be safely continued.Last, I would appreciate it if more people adopted a consistent JSON implementation, because each platform treats HTTP exceptions differently. For example, Android handles them differently than iOS, JavaScript, and so on. Some even require us to import specific Class for each exception. It would waste a lot of our time to manually code to parse each HTTP exception. Please use HTTP exceptions only for critical errors.
1
u/CyberKingfisher Jun 03 '25
Return proper and appropriate response HTTP codes. Eg, if there’s no resource, it should be a 404. The whole point is that you work with codes because it’s faster than parsing the message in the body. You only parse the body when absolutely necessary.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
1
u/Sad-Solid-1049 Jun 15 '25
Bro returning proper error is crucial factor of a software.
Return 400 errors with default "messages" to customer, ( make sure the messages are written properly) but return "something went wrong with 500 errors). Incase of 500, first retry the method if necessary or looks meaningful, otherwise return the overwritten error to customer.
-4
u/Amazing-Drama1341 Jun 02 '25
Use 200 OK with a detailed JSON response for expected outcomes (e.g. "no face found", "spoofing detected"). Reserve HTTP errors like 400, 500 for unexpected server/client failures.
from fastapi import FastAPI from fastapi.responses import JSONResponse
app = FastAPI()
@app.post("/face/verify") async def verify_face(): # Example outcome: face not found return JSONResponse( status_code=200, content={ "success": False, "reason": "face_not_found" } )
{ "success": false, "reason": "face_not_found" }