Skip to content
tezvyn:
Menu

How FastAPI uses type hints for validation

Source: interviewMediumHow cards are made

Summary

Whether you know FastAPI leans on Pydantic and type hints.

Key points

Hints drive parsing, validation, and conversion; a path declared int is coerced or returns 422; OpenAPI is auto-generated.

What's really being asked

The interviewer wants to confirm you understand FastAPI's defining idea: ordinary Python type hints, interpreted by Pydantic, become the validation and serialization layer. Python ignores hints at runtime, but FastAPI inspects them and acts on them.

The full answer

When you annotate a parameter, FastAPI uses that annotation to parse the raw request value, validate it, and convert it to the declared type. For path and query parameters, which arrive as strings, an int annotation tells FastAPI to coerce the string to an integer. If coercion fails, FastAPI returns an automatic HTTP 422 Unprocessable Entity with a structured error describing what was wrong, and your function body never runs with bad data. The same hints feed the auto-generated OpenAPI schema and the interactive docs, and give editors autocompletion. For request bodies you declare a Pydantic BaseModel, and FastAPI validates the JSON against it the same way.

The mistakes people make

Saying Python enforces the type hints itself; it does not, hints are ignored by the interpreter at runtime and FastAPI is what reads them. Describing manual parsing with int() and try/except, which defeats the declarative point. Forgetting that a validation failure yields a 422 automatically rather than a 500 or a crash.

What usually comes next

What status code is returned on a validation error and why? How do query parameters differ from path parameters here? How would you add constraints like a minimum value?

A concrete example

Consider an endpoint declared as async def read_item(item_id: int) decorated with the path /items/{item_id}. A request to /items/42 passes 42 as an integer into the function. A request to /items/abc cannot be coerced, so FastAPI returns 422 with a message that item_id should be a valid integer, and the handler is never invoked. Adding Path(gt=0) would further reject zero or negatives with the same automatic 422 behavior.

Interview question

A FastAPI endpoint declares a path parameter as item_id: int. A request arrives for /items/abc, which cannot be coerced to an integer. What happens?

  • a.FastAPI returns an automatic 422 error describing the invalid value, and the endpoint function body never executesCorrect
  • b.Python raises a TypeError at the point where item_id is used inside the function body
  • c.FastAPI passes the string abc through unchanged, and the function must convert it manually
  • d.The server returns a 500 Internal Server Error because the type coercion crashes the process
Why?

FastAPI validates the coerced type before your function runs, so an uncoercible value short circuits into an automatic 422 with no handler code executing. Python itself does nothing with the int annotation at runtime, which is why expecting a TypeError or manual parsing misses the point of the hint.

Just read this? Test yourself on what you have been reading.

Read the original → fastapi.tiangolo.com

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.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles