tezvyn:

How do you group FastAPI endpoints under tags and describe groups?

AI-drafted, machine-checkedSource: fastapi.tiangolo.comintermediate
How do you group FastAPI endpoints under tags and describe groups?

Tests FastAPI's two-step tag system: decorators label routes, and openapi_tags supplies group descriptions. Good answers cover tagging paths with tags=["users"], then defining metadata in FastAPI(openapi_tags=[...]) with matching names.

WHAT THIS TESTS: This question checks if you know the difference between labeling individual routes and declaring metadata for the groups that appear in Swagger UI or ReDoc. It also tests whether you use FastAPI's native hooks or hack the OpenAPI schema manually.

A GOOD ANSWER COVERS four things in order. First, explain that individual endpoints receive a tags list in the path operation decorator, for example @app.get("/users/", tags=["users"]). Second, state that tag descriptions and external docs are configured at the application level through the openapi_tags argument in the FastAPI constructor. Third, describe the structure of openapi_tags as a list of dictionaries, where each dict contains name, description, and optionally externalDocs, and the name must exactly match the strings used in the endpoint tags. Fourth, mention that the docs UI will automatically pick up this metadata and render the group heading with its description.

COMMON WRONG ANSWERS: A major red flag is trying to pass a dict or description into the decorator tags parameter, which only accepts a list of strings. Another mistake is overriding app.openapi() manually just to inject tag metadata, which adds maintenance burden when FastAPI already exposes the openapi_tags hook. Some candidates also confuse API-level metadata like title and description with tag-level metadata.

LIKELY FOLLOW-UPS: An interviewer might ask how to control the display order of tags in the docs, which is done by ordering the dictionaries inside the openapi_tags list. They might also ask how to add external documentation links to a tag, which uses the externalDocs dict with url and description keys inside the same tag metadata structure. You could also be asked how to hide certain endpoints from the docs while keeping them in the application, which is a separate concern handled by include_in_schema=False.

ONE CONCRETE EXAMPLE: Imagine a FastAPI app with user and item endpoints. You would write @app.get("/users/", tags=["users"]) and @app.get("/items/", tags=["items"]). Then instantiate the app as FastAPI(openapi_tags=[{"name": "users", "description": "Operations for user management"}, {"name": "items", "description": "Product inventory operations"}]). When you open /docs, Swagger UI shows collapsible sections labeled Users and Items, each with its own description paragraph above the endpoint list.

Source: fastapi.tiangolo.com

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.