tezvyn:

FastAPI non-integer query param default behavior

AI-drafted, machine-checkedSource: fastapi.tiangolo.comadvanced

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 THIS TESTS: 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.

A GOOD ANSWER COVERS: 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.

COMMON WRONG ANSWERS: 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.

LIKELY FOLLOW-UPS: 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?

ONE 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.

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.