r/FastAPI • u/JanGiacomelli • Jun 03 '26
Tutorial Prevent unintentional breaking API changes in FastAPI apps
Things are changing all the time. It's no different with APIs. As we develop our products, APIs need to be updated as well. Everything is great until we introduce an unintentional breaking change. For example, if we rename the attribute in the response. With a faster development pace enabled by AI tooling, this is even more likely to happen unintentionally.
To prevent such changes from going to production, we can add a check for breaking API changes to our CI/CD pipeline. It's easy to do so for FastAPI apps with GitHub Actions and oasdiff. The flow is the following:
- Export OpenAPI schema that's auto-generated by FastAPI using
app.openapi()from PR's branch. - Check out the main branch and export the OpenAPI schema for it as well.
- Use oasdiff to detect and report potential breaking changes
Example workflow:
name: CI
on:
pull_request:
branches:
jobs:
breaking-changes:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v6
with:
ref: main
path: main-branch
- uses: astral-sh/setup-uv@v8.1.0
with:
python-version: "3.14"
- name: Generate schema from PR branch
run: |
uv sync
uv run python scripts/export_openapi.py new.json
- name: Generate schema from main branch
working-directory: main-branch
run: |
uv sync
uv run python scripts/export_openapi.py ../old.json
- name: Install oasdiff
run: |
curl -fsSL https://raw.githubusercontent.com/oasdiff/oasdiff/main/install.sh | sh
- name: Check for breaking changes
run: oasdiff breaking old.json new.json --fail-on ERR
Example OpenAPI schema export script:
# scripts/export_openapi.py
=
You can find the full tutorial here: https://jangiacomelli.com/blog/prevent-unintentional-breaking-api-changes-fastapi/
View all comments
Show parent comment
2
u/JanGiacomelli Jun 03 '26
You do, but it's easy to forget that something is breaking change. For example, you add a new required attribute to the API as you need it now. You also update tests to pass. You have green pipeline, but clients will break. When AI agents are writing code, such things are even more likely. This doesn't replace tests. This is just yet another protection layer.