tezvyn:

How do you mark a FastAPI endpoint as deprecated?

AI-drafted, machine-checkedSource: fastapi.tiangolo.comintermediate
How do you mark a FastAPI endpoint as deprecated?

This tests decorator-level OpenAPI configuration in FastAPI. Pass deprecated=True to the path operation decorator, e.g. @app.get("/old", deprecated=True), so Swagger UI shows a strikethrough. Red flag: burying a deprecation warning in the docstring instead.

WHAT THIS TESTS: This question checks whether you know that FastAPI path operation configuration is declared on the decorator, not inside the function body, and that OpenAPI behavioral metadata like deprecation is generated automatically from those declarative arguments rather than from manual documentation edits. It also reveals whether you understand the separation between route metadata and business logic.

A GOOD ANSWER COVERS: First, state that you pass the parameter deprecated=True directly to the path operation decorator, for example app.get with the keyword argument set to True. Second, explain that FastAPI reads this decorator argument and writes the deprecated field into the generated OpenAPI schema for that specific path operation. Third, mention that standard OpenAPI consumers such as Swagger UI read that schema field and render the endpoint with a strikethrough and a Deprecated label without any extra frontend code or custom CSS. Fourth, emphasize that this is purely a decorator configuration and does not require changes to the function implementation, return type, or manual docstring editing, keeping the API contract explicit and machine-readable.

COMMON WRONG ANSWERS: Suggesting that you write a deprecation warning inside the function docstring and expecting Swagger UI to detect it automatically. Proposing to mutate the raw OpenAPI dictionary via app.openapi() just to mark one route as deprecated. Confusing endpoint deprecation with Pydantic model field deprecation, which is a different mechanism entirely and affects input or output schemas rather than the route itself. Recommending custom middleware, response headers, or logging as a substitute for the native schema metadata, which breaks auto-generated client discovery.

LIKELY FOLLOW-UPS: How would you deprecate an individual query parameter or request body field rather than the whole endpoint? What is the difference between setting deprecated on a path operation versus setting it on a Pydantic Field? How would you programmatically list all deprecated routes in a large FastAPI application by inspecting the app.routes collection? If you need to keep the endpoint alive but hide it from the docs entirely, which decorator argument would you use instead of deprecated? How do you communicate a sunset date to API consumers beyond the boolean flag?

ONE CONCRETE EXAMPLE: Suppose you have a legacy user lookup endpoint at GET /users/by-id. You would define the route as app.get("/users/by-id", deprecated=True) async def get_user_by_id(user_id: int): followed by the implementation returning the user record. When developers open your Swagger UI, that route appears struck through with a clear Deprecated badge, signaling that clients should migrate to the newer replacement endpoint without you writing any documentation boilerplate or maintaining separate wiki pages.

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.