Snapshot Your Public API So Changes Are Never Accidental

Most accidental breaking changes are not malicious or careless. Someone moves a file, renames a helper, or stops re-exporting something from an index file, and nobody notices that it was reachable from the outside. The fix I recommend to every maintainer is simple: keep a committed snapshot of your public API and make CI fail when it changes without the snapshot being updated.

The idea in one paragraph

Generate a plain-text description of everything your package exports, including names, kinds and signatures. Commit that file to the repository. In CI, regenerate it and compare. If the regenerated file differs, the build fails until a developer updates the snapshot in the same pull request. The diff then shows reviewers exactly how the public surface changes, which makes the version decision obvious.

Tools and approaches

Several ecosystems have tooling for this, and the approach also works with a small script:

A minimal Python version

Here is an illustrative script for a hypothetical package called example_tables:

# scripts/api_snapshot.py
import inspect, example_tables as pkg

for name in sorted(pkg.__all__):
    obj = getattr(pkg, name)
    try:
        sig = str(inspect.signature(obj))
    except (TypeError, ValueError):
        sig = ""
    print(f"{name}{sig}  [{type(obj).__name__}]")

In CI, run it and compare against the committed file:

python scripts/api_snapshot.py > api.current.txt
diff -u api.txt api.current.txt

A non-zero exit from diff fails the job. It is crude, but it catches removed names and changed signatures, which are the most common accidents.

What the snapshot does not catch

A surface snapshot only sees shapes. It will not notice a changed default value, a different error being thrown, or behaviour that changed behind an identical signature. Those still need tests and careful review. I also find it worth adding a line to the pull request template: "Did the API snapshot change? If so, which version bump does that imply?" That links the mechanical check to the human semver decision.

Why it is worth the small overhead

The real benefit is cultural. When every change to the public surface shows up as a diff in a dedicated file, the team stops thinking of the API as "whatever happens to be exported" and starts treating it as a designed artifact. That is the mindset semantic versioning assumes. For the type-specific side of this, see the post on type-level breaking changes.

← Back to Home

Subscribe to Our Newsletter

Get the latest updates delivered to your inbox.