Sub topic 17
Updating locked dependencies safely
This chapter assumes you can already generate a hash-pinned lock file with pip-tools or uv. It also assumes you have a Dockerfile that installs from that lock file, and a CI job that checks the lock file is current. Here you learn how to change the lock file without breaking the reproducibility it provides.
Why a lock file needs updates
A lock file freezes your environment, and that includes any known flaws in the frozen versions. Three things force you to update it:
- A security advisory affects a pinned package.
- A bug fix or a needed feature ships in a newer release.
- A new Python, CUDA, or framework version requires a new set of compatible packages.
Updating is a change to your experiment's inputs, just like changing the learning rate. An update that nobody reviews or measures is the same environment drift the lock file was meant to prevent.
The update routine
The routine below treats every update as a reviewable change.
1. Upgrade as little as possible
Both tools can upgrade one package and keep everything else at its current pin. This keeps the diff small and makes a regression easy to trace.
With pip-tools, the existing requirements.lock acts as a set of constraints. -P (--upgrade-package) loosens only the named package:
pip-compile --generate-hashes --upgrade-package numpy -o requirements.lock requirements.inWith uv:
uv lock --upgrade-package numpyBoth tools also upgrade a package to an exact version:
pip-compile --generate-hashes -P "numpy==2.1.3" -o requirements.lock requirements.in
uv lock --upgrade-package "numpy==2.1.3"Avoid a blanket --upgrade for routine work. It moves every package at once, so a changed metric has dozens of possible causes. Reserve it for a scheduled refresh, and run that refresh on its own branch.
Resolve the lock file in the same environment you deploy to. pip-compile resolves for the Python version and platform it runs on. A lock file generated on a laptop with Python 3.12 can differ from one for the python:3.11-slim image. Run the command inside the project's base image:
docker run --rm -v "$PWD":/app -w /app python:3.11-slim sh -c \
"pip install pip-tools && pip-compile --generate-hashes --upgrade-package numpy -o requirements.lock requirements.in"uv records a universal resolution that covers several platforms, so it is less sensitive to this. You should still check that the requires-python range in pyproject.toml matches the interpreter in your image.
2. Read the diff
Open the lock file diff and look at every changed line:
git diff --stat requirements.lock
git diff requirements.lockCheck these points:
- Only the intended packages changed. If you upgraded
numpyand 14 other packages moved, find out why. A newnumpymay require a newer version of a shared dependency, which is acceptable. An unrelated bump is not. - Major version jumps. A change from
1.xto2.xcan break APIs and numerical behavior, and it needs its own branch and explanation. - Packages that appeared or disappeared. New transitive dependencies are new code you now trust. Removed ones may signal a changed resolution.
- Hash lines. A package with the same version but different hashes is unusual. It can mean a rebuilt wheel, so investigate before accepting it.
3. Scan for known vulnerabilities
If security motivated the update, confirm that it fixed the problem. pip-audit can read a hashed requirements file:
pip install pip-audit
pip-audit --require-hashes -r requirements.lockRun the same command on the old lock file first, so you have a before and after.
4. Rebuild the image and run checks
The lock file is only part of the environment. Rebuild the image from it with caching working normally. The changed lock file invalidates the dependency layer, which is correct:
docker build -t ml-train:update-numpy .
docker run --rm --gpus all ml-train:update-numpy python -c "import numpy, torch; print(numpy.__version__, torch.__version__, torch.cuda.is_available())"Then run your unit tests and a short seeded training run inside that image. The CI check from the previous chapter (uv lock --check, or recompiling and diffing for pip-tools) should pass on the branch.
5. Compare results, not just exit codes
A package can import and run without errors while changing numbers. Compare a short, seeded run against a stored baseline. This script needs only the standard library:
import json
import sys
def compare(baseline_path: str, candidate_path: str, rel_tol: float) -> int:
with open(baseline_path) as f:
baseline = json.load(f)
with open(candidate_path) as f:
candidate = json.load(f)
failures = []
for name, old in baseline.items():
new = candidate.get(name)
if new is None:
failures.append(f"{name}: missing in candidate")
continue
denom = max(abs(old), 1e-12)
if abs(new - old) / denom > rel_tol:
failures.append(f"{name}: {old} -> {new}")
for line in failures:
print("FAIL", line)
return 1 if failures else 0
if __name__ == "__main__":
sys.exit(compare(sys.argv[1], sys.argv[2], float(sys.argv[3])))Run it as python compare_metrics.py baseline.json candidate.json 0.01. Choose the tolerance from the run-to-run variation you measure in the current environment. With deterministic mode on and the same hardware, results can match exactly. With different GPUs or library versions, small floating-point differences are normal, and a tolerance of 1% or less is a common starting point.
6. Merge the lock file and the image definition together
If the update touched the Python version, base image, or CUDA version, commit those changes in the same pull request as the new lock file. The lock file and the image must agree. If the base image is pinned by digest, update the digest deliberately: look up the new digest, change it in the FROM line, and treat it as a reviewed diff.
Updates that need extra care
| Update | Extra risk | What to do |
|---|---|---|
| PyTorch or TensorFlow minor version | Bundled CUDA runtime changes | Check the host driver supports the new CUDA version, then test on a GPU machine |
| NumPy or SciPy major version | Changed defaults and removed APIs | Upgrade alone on a dedicated branch |
| Python minor version | Wheels may not exist for every package | Resolve in the new base image and check that every package installs with `--require-hashes` |
| Security patch to a transitive package | The direct dependency may cap the version | Upgrade the parent package, or add a constraint and record why |
Rolling back
Because the lock file and Dockerfile live in Git, a rollback is a revert:
git revert <commit-of-the-update>Keep the previous image available. If you tag images with the Git commit and record the digest of each release, you can redeploy the older environment without rebuilding it. A rebuild is only as reproducible as its base image and package index, and old wheels can be yanked from an index.
Common mistakes
- Editing the lock file by hand. The hashes and dependency relationships will no longer match what a resolver produced.
- Upgrading only the lock file. The old image keeps running until someone rebuilds it. Use the CI rebuild check to catch this.
- Resolving on a different platform than you deploy to. See step 1.
- Bundling an update with a code change. If metrics move, you cannot tell which change caused it.
- Updating only after something breaks. Schedule a small, regular update, for example monthly. Frequent small updates are easier to review than one that spans a year.
Exercise
You run pip-compile --generate-hashes --upgrade -o requirements.lock requirements.in to get a security fix for one package. The diff shows 40 changed packages, and your validation accuracy drops by 0.8 points. What went wrong with the process, and what do you do next?
Show answer
The blanket --upgrade flag moved every package, so you cannot tell which one changed the accuracy. Discard the change and redo the update with -P <package> (or uv lock --upgrade-package <package>) for only the vulnerable package. Review the smaller diff, rebuild the image, and compare seeded metrics against the baseline. Do the broader refresh later on a separate branch, upgrading in small groups if the metrics still shift.