Skip to content
tezvyn:
Menu

Python & FastAPI

Learn Python and FastAPI in bite-sized cards. Revise key concepts, practice interview questions and test what you remember with quick quizzes.

111 bites

Test yourself: Top 30 intermediate Python & FastAPI interview questions →Multiple choice, with the correct answer and why it is correct on every question. Free, no sign-in.

Intermediate everything in Python & FastAPI, page 4

How do you structure concurrent API calls with asyncio.gather in FastAPI?
intermediate2 min read

How do you structure concurrent API calls with asyncio.gather in FastAPI?

Tests FastAPI async concurrency. Strong answer: async def endpoint with two async HTTP requests in asyncio.gather, cutting total latency from sum to max of the two. Red flag: using sync clients or threads instead of async I/O.

intermediate2 min read

Write an async decorator that logs execution time for FastAPI

Use functools.wraps, wrap perf_counter around awaited call, log ms, and place decorator above path operation.

How does FastAPI leverage Pydantic for request validation and serialization?
intermediate2 min read

How does FastAPI leverage Pydantic for request validation and serialization?

This tests your understanding of FastAPI's declarative validation. Explain that type hints trigger auto-parsing, Pydantic enforces schemas and errors, and return types auto-serialize responses. Red flag: manually parsing request.body() or json.loads in routes.

intermediate2 min read

Docker Compose for Local FastAPI Stacks

Docker Compose turns your laptop into a one-command datacenter. Define Postgres, Redis, and your FastAPI app in one YAML file and they boot as a networked stack.

intermediate3 min read

Custom Field Serialization with @field_serializer

@field_serializer is an exit-only adapter for one field: it reshapes data leaving the Pydantic model without changing internals. Use it to format decimals, mask secrets, or tweak datetimes for FastAPI JSON. Never use it for validation; it only runs on output.

intermediate2 min read

Per-Field Validation with @field_validator

@field_validator scrubs a single Pydantic field before it enters the model. Use it for rules like 'password must contain a digit' or 'port must exceed 1024'. It only sees one field at a time, so cross-field checks belong in a model validator instead.

intermediate2 min read

Serialize Pydantic Models with model_dump

model_dump turns a Pydantic model into a plain Python dict, bridging typed objects and JSON serializers in FastAPI endpoints. Call it when you need raw data before returning a response. Do not confuse it with model_dump_json, which emits a string, not a dict.

intermediate2 min read

Testing FastAPI Lifespan Events

FastAPI lifespan events only run when TestClient is used as a context manager. Use this to test startup logic like DB pools before endpoints. Using TestClient(app) without with skips lifespan, leaving your app uninitialized and tests silently wrong.

intermediate2 min read

ARQ for FastAPI: Async Background Tasks

ARQ lets your FastAPI app offload heavy work to background workers, keeping the API responsive. It's a task queue built for asyncio. Use it for slow tasks like sending emails or processing data. The footgun is using blocking task libraries with async code.

asyncio Streams: High-Level Async Network I/O
intermediate2 min read

asyncio Streams: High-Level Async Network I/O

asyncio Streams are like async file handles for the network. You get a reader/writer pair to await data, simplifying TCP clients and servers for basic protocols. The footgun: the default buffer limit is small; reading large data will fail unexpectedly.

Layered Architecture: Separating API from Business Logic
intermediate2 min read

Layered Architecture: Separating API from Business Logic

A layered architecture separates your API into distinct jobs: routing, controlling, and serving. This keeps code maintainable, like an organized toolbox. It's crucial for growing FastAPI apps.

intermediate2 min read

FastAPI's StreamingResponse: Send Data in Chunks

StreamingResponse sends data piece by piece, like a live broadcast, instead of sending a complete file all at once. This keeps your server's memory low for huge responses like file downloads, video streams, or live data from AI models.

Pydantic: Reusable Validation with Annotated Types
intermediate2 min read

Pydantic: Reusable Validation with Annotated Types

Pydantic's Annotated attaches validation logic directly to a type, making it reusable. Define a custom type like SquareNumber once and apply it to any model field, ensuring consistent validation without repeating code.

intermediate2 min read

FastAPI: Validating Models with Pydantic's Field

Pydantic's Field adds guardrails directly to your data model's attributes. Use it to enforce constraints like string length (max_length=50) or numeric ranges (gt=0), making your models self-validating.

intermediate2 min read

From Dev Server to Production: Running FastAPI with Workers

Your dev server is a single process. For production, you need a process manager to run multiple Uvicorn worker processes, handling concurrent requests and providing fault tolerance.

Exclude a FastAPI Endpoint from OpenAPI Docs
intermediate1 min read

Exclude a FastAPI Endpoint from OpenAPI Docs

Hide an endpoint from your API docs by setting include_in_schema=False. Use this for internal or deprecated endpoints. The footgun: this only hides the endpoint from documentation; it remains fully functional and accessible if the URL is known.

FastAPI: Documenting Additional API Responses
intermediate2 min read

FastAPI: Documenting Additional API Responses

Document every possible API response, not just the happy path. The responses decorator parameter lets you define alternative status codes and schemas, like a 404 error model, making your OpenAPI docs complete.

intermediate2 min read

Testing Async FastAPI with pytest-asyncio

To test async code, your tests must also be async. pytest-asyncio lets you write async def test_... functions to await operations like database checks after an API call.

intermediate2 min read

Run One Test with Many Inputs using pytest.parametrize

Run one test function with many inputs using @pytest.mark.parametrize, avoiding repetitive code. It's ideal for checking a function against various inputs, edge cases, and expected failures. The footgun: mutable parameters like lists are passed by reference.

intermediate2 min read

Custom FastAPI Middleware: The BaseHTTPMiddleware Helper

FastAPI's BaseHTTPMiddleware lets you wrap endpoints to run code before and after they execute. Use it to add custom headers or log request times. The footgun: reading request.body() in the middleware will break the endpoint, as the body can only be read…

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