Skip to content

Lesson 7 of 9 · 21m

OpenAPI as a Contract, Not a Generated Decoration

Design schemas and operation metadata deliberately, version breaking changes, generate clients safely, and use OpenAPI drift checks to prevent accidental API changes.

FastAPI generates OpenAPI automatically, which is both the blessing and the trap. Teams start believing the schema is correct because it exists. In reality, OpenAPI is a public contract. It deserves the same care as database migrations because clients, mobile apps, partners, and generated SDKs build against it.

Generated does not mean finished

FastAPI can infer response models from type hints, but inference is not design. Specify response models explicitly at public boundaries:

@app.get("/projects/{project_id}", response_model=ProjectOut)
def read_project(project_id: int):
    ...

This prevents accidental field leakage and keeps output stable when internal objects change. Add examples, descriptions, and operation IDs where they help humans or generated clients:

@app.get(
    "/projects/{project_id}",
    response_model=ProjectOut,
    operation_id="getProject",
    summary="Fetch a project by ID",
)

Clean operation IDs become clean generated method names. Random duplicates become ugly client APIs forever.

Breaking change means client-visible, not internals-visible

Changing a Python type internally may be harmless. Removing a response field, tightening validation, changing enum values, or altering error semantics can break clients. Version behavior when necessary, but prefer additive evolution: add optional fields, new endpoints, or new enum values only when clients tolerate unknowns.

Deprecation is communication plus time. Use deprecated=True, documentation, changelogs, and sunset headers — not silent removal.

Use the schema as a review artifact

Commit the generated OpenAPI document or produce it in CI and diff it. A small handler change that accidentally alters a public response becomes visible in review:

python scripts/export_openapi.py > openapi.json
git diff -- openapi.json

This is one of the highest-value deployment checks you can add. It catches accidental public API drift without needing a heavyweight governance process.

Security schemes must match reality

Declare bearer auth, OAuth flows, or API key schemes correctly so documentation and generated clients know how to authenticate. Do not document a cookie flow as bearer authentication because it is easier. Incorrect security metadata teaches integrators the wrong thing at scale.

Documentation is for decisions, not repetition

A useful description explains semantics: pagination order, idempotency behavior, filtering limitations, consistency guarantees. Repeating 'project ID' as the description of project_id adds noise. Document what a competent developer cannot infer from the type.

Field notes

  • Explicit response models are an allowlist against accidental data exposure.
  • Keep nullable fields deliberate: null means something different from omitted in many clients.
  • Unknown enum values are a client compatibility question; design accordingly.
  • If you expose multiple API versions, make the versioning rule consistent — URL prefix, header, or media type — not a mixture.

Was this lesson helpful?

Sign in to rate