FastAPI non-integer query param default behavior
Tests FastAPI's automatic Pydantic validation and default error contracts. Strong answer: 422 Unprocessable Entity with JSON detail array containing loc, msg, and type fields. Red flag: saying 400 Bad Request or manual validation is needed.
What's really being asked
This question probes whether you understand FastAPI's automatic request validation pipeline powered by Pydantic. Senior engineers should know that type hints in path operation functions are not just documentation; they drive runtime validation and serialization. The interviewer wants to see if you know the exact default HTTP contract, including status code and JSON schema, without writing manual validation logic.
The full answer
First, state that FastAPI automatically validates the query parameter against the int annotation and catches coercion failures internally. Second, specify the status code: 422 Unprocessable Entity. Third, describe the JSON response body structure. It is an object with a detail key containing an array of error objects. Each error object has three fields: loc, which is an array locating the error such as query followed by the parameter name; msg, which is a human readable message like value is not a valid integer; and type, which is a machine readable error type such as type_error.integer. Fourth, note that this behavior is the default and requires no custom exception handler or manual try except block.
The mistakes people make
Saying the endpoint returns 400 Bad Request instead of 422. Claiming the application raises an uncaught Python exception and returns 500. Describing the response as plain text rather than structured JSON. Asserting that you must manually convert the value using int inside the function. Confusing this with Starlette HTTPException default formatting. Any answer suggesting Pydantic returns a different field structure than loc, msg, and type is also incorrect.
What usually comes next
How would you customize the 422 response to hide internal details from clients? What is the difference between 400 and 422 in the context of REST APIs? How does the response change if you use a Pydantic model for query parameters instead of individual annotated parameters? Can you override the default request validation exception handler to log errors before returning the response? What happens if the parameter is optional int with a default of None and the client sends an invalid string?
A concrete example
Suppose a path operation is defined as async def read_items with a parameter limit annotated as int, and the client sends GET /items?limit=foo. FastAPI returns HTTP 422 Unprocessable Entity with a JSON body structured as follows. The root object has a detail key mapped to an array. That array contains one object whose loc field is the array query, limit; whose msg field reads value is not a valid integer; and whose type field reads type_error.integer. The loc array identifies the parameter source and name, making it easy for API consumers to pinpoint the issue.
Interview question
A client sends GET /items?limit=foo to an endpoint with parameter limit: int. What is FastAPI's default response?
- a.HTTP 422 Unprocessable Entity with a JSON body whose detail array contains objects with loc, msg, and type fieldsCorrect
- b.HTTP 422 Unprocessable Entity with a JSON body containing a single detail string describing the error
- c.HTTP 500 Internal Server Error because the int conversion raises an uncaught ValueError
- d.HTTP 400 Bad Request with a plain text error message stating the parameter is invalid
Why? this is the answer
FastAPI relies on Pydantic to automatically validate query parameters and returns a 422 Unprocessable Entity with a JSON detail array of objects containing loc, msg, and type fields. Option B is tempting because the status code is correct, but the body structure is actually a detailed array rather than a single string.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
- #fastapi
- #pydantic
- #validation
- #python
- #rest-api
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.
See open roles