Skip to content
tezvyn:
Menu

How do you directly modify FastAPI's generated OpenAPI dictionary?

HardHow cards are made

Tests deep FastAPI lifecycle knowledge. Override app.openapi: save the original, call it to get the dict, mutate it, cache on app.openapi_schema, and return. Red flag: rewriting /openapi.json in middleware or touching the schema cache directly.

What's really being asked

FastAPI generates the OpenAPI schema as a plain Python dictionary during startup or on the first request to the docs endpoint. This question tests whether you know the framework provides a hook to intercept and mutate that dictionary before it is cached and served. Senior candidates should demonstrate they understand the difference between declarative decorator tweaks and imperative programmatic control, and they should know the exact function override pattern rather than resorting to hacks.

The full answer

First, store a reference to the original app.openapi function before overriding it. Second, define a new function that calls the original to obtain the base dictionary, which preserves all normal path operation and model generation. Third, mutate that dictionary directly, for example by injecting top-level security schemes, adding custom tags, pruning internal paths, or merging in external schema fragments. Fourth, assign the mutated dictionary to app.openapi_schema so FastAPI caches it and does not regenerate it on every request. Fifth, return the mutated dictionary from the new function. Finally, assign the new function back to app.openapi. The candidate should mention that this runs lazily on first schema access unless explicitly triggered earlier.

The mistakes people make

A major red flag is suggesting middleware that intercepts requests to /openapi.json and rewrites the response body. That adds unnecessary overhead and complexity. Another red flag is modifying app.openapi_schema after the fact without wrapping the generator, which can lead to race conditions or the changes being overwritten. Candidates who suggest monkey-patching FastAPI's internal get_openapi utility or editing the JSON string directly show a lack of familiarity with the intended extension point. Simply stating you would use the openapi_url parameter or more decorators misses the point of programmatic customization.

What usually comes next

The interviewer may ask how you would serve two different OpenAPI schemas from the same app, such as a public schema and an internal schema. They might also ask when the override function actually executes, which is typically on the first request that needs the schema unless you call app.openapi() during startup. Another follow-up is how to test this override reliably in unit tests without spinning up the full application.

A concrete example

Suppose your app includes health check and admin endpoints that should not appear in the public documentation. Inside the custom app.openapi function, after calling the original generator, you iterate over openapi_schema['paths'] and remove any path whose operation metadata contains a tag of internal or admin. You then inject a global API security requirement into openapi_schema['security'] and set the version field dynamically from an environment variable before caching and returning the dict.

Interview question

You need to programmatically prune internal paths and inject top-level security into FastAPI's OpenAPI schema before it is served. Which approach aligns with the framework's intended lifecycle hook?

  • a.Call app.openapi() after startup, mutate the dict, and assign it directly to app.openapi_schema without wrapping the generator
  • b.Monkey-patch FastAPI's internal get_openapi utility to return the modified schema
  • c.Save the original app.openapi function, write a wrapper that calls it, mutates the dict, caches the result on app.openapi_schema, and assigns the wrapper back to app.openapiCorrect
  • d.Register middleware that intercepts /openapi.json and rewrites the response body
Why?

Overriding app.openapi with a wrapper that calls the original, mutates the dict, and caches it on app.openapi_schema is the supported hook. Using middleware adds unnecessary overhead and complexity, and risks missing the framework's own caching logic.

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

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. Open roles that interview on fastapi — each one lists the topics its interview covers.

See open roles