Add a custom x- field to a FastAPI path operation schema

Tests knowledge of FastAPI's built-in OpenAPI extension hook. Answer: cite the path operation decorator's extra-schema dict (OpenAPI Extra) to merge x-internal-id directly. Red flag: proposing manual JSON editing or schema post-processing.
WHAT THIS TESTS: This question checks if you understand that FastAPI exposes a first-class, decorator-level configuration for per-path OpenAPI metadata. The interviewer wants to see that you know the docs include an OpenAPI Extra section under Path Operation Advanced Configuration, and that you use this built-in hook rather than fighting the framework. It separates seniors who read the advanced docs from those who only know the tutorial basics and would resort to hacks.
A GOOD ANSWER COVERS: A strong response hits four things in order. First, name the OpenAPI Extra mechanism described in FastAPI's Path Operation Advanced Configuration docs as the correct place to inject custom fields. Second, explain that it takes a dictionary which FastAPI merges directly into that specific path operation's schema, so the custom field lives next to the route definition. Third, clarify that because the field is non-standard, it belongs in an OpenAPI Extension, which is why the OpenAPI Extensions section exists in the same docs. Fourth, emphasize that this avoids any need to manually edit the final JSON or run a post-processing script after startup.
COMMON WRONG ANSWERS: The biggest red flag is suggesting you export the schema, edit the JSON by hand, and serve the static file. Another weak pattern is proposing to override the global OpenAPI schema generator or adding middleware that mutates the schema object on every request. Some candidates suggest using Pydantic model Config or response_model metadata, but those affect schemas for models, not the path operation object itself. A third red flag is saying it cannot be done without a plugin.
LIKELY FOLLOW-UPS: The interviewer may ask how you would apply the same x-internal-id to every route in a router instead of one path. They might ask whether the extra dict overrides standard fields or merges with them. Another follow-up is how to validate that the custom field actually appears in the generated schema during automated testing.
ONE CONCRETE EXAMPLE: Suppose you have a GET endpoint for internal audits and you want x-internal-id on just that operation. You would use the path operation decorator's extra configuration parameter, documented under OpenAPI Extra, and pass a dictionary containing x-internal-id and your identifier. FastAPI places that key-value pair into the operation object in the generated OpenAPI schema. If you later generate a client from that schema, the custom field travels with the operation metadata without any manual JSON edits or external patching scripts.
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.