Sub topic 17
Updating locked dependencies safely
Assume you know basic pip or uv, can read a lock file, and understand why an ML run should use the same environment in development and CI. A lock file records the resolved versions of direct and transitive dependencies. Updating it changes the software used by your training or inference job, so treat the update as a small, reviewable change.
Choose the update scope
Do not regenerate every dependency when you need one fix. A broad update can silently upgrade a numerical library, GPU package, or framework and change model behavior.
| Goal | Safer action |
|---|---|
| Fix a package bug | Update that package and its required dependencies |
| Apply security fixes | Review all affected packages, then update the smallest safe set |
| Refresh everything | Use a scheduled branch and run the full test suite |
| Change Python or CUDA | Treat it as a separate environment migration |
Keep direct requirements in a human-edited file such as requirements.in or pyproject.toml. Keep resolved files such as requirements.lock or uv.lock under version control.
A reviewable update routine
Use a separate Git branch. Record the current model, data, Python version, and lock-file commit before changing anything. Then update one logical group.
With pip-tools, edit requirements.in and compile again:
git switch -c update-scikit-learn
python -m pip install pip-tools
pip-compile --upgrade-package scikit-learn \
--generate-hashes \
-o requirements.lock requirements.in
python -m pip install --require-hashes -r requirements.lockThe --upgrade-package option asks for a targeted upgrade. Review the resulting diff. A change to one package can also change its transitive dependencies, which are dependencies of that package.
With uv, update a named package and require the lock file to remain consistent during installation:
uv lock --upgrade-package scikit-learn
uv sync --lockedDo not hand-edit resolved versions or hashes. Change the declared requirement and let the lock tool calculate compatible artifacts.
Verify before merging
Check both software behavior and the environment description.
- Build the container from a clean checkout, without using a developer's local package cache.
- Install from the lock file with hash checking enabled.
- Run unit and integration tests.
- Run a small deterministic training or inference job.
- Compare metrics, predictions, checkpoint files, and resource use with the baseline.
- Inspect licenses and known-vulnerability reports according to your project's policy.
- Record why each changed package was updated.
A CI job should fail if the declared dependencies and lock file disagree. For example, uv sync --locked rejects a stale uv.lock; pip install --require-hashes rejects missing or incorrect hashes. Rebuild the image as another check.
Roll back and repeat
If results regress, revert the lock-file commit rather than deleting individual lines. Keep the failed update branch and test output when the cause needs investigation. For routine maintenance, update one group at a time, merge only after CI passes, and schedule broad upgrades separately.
Exercise
Why should you avoid manually changing a package version inside a hash-pinned lock file?
Show answer
The version, dependency graph, and artifact hashes may no longer agree. Regenerating the lock file preserves a consistent, verifiable set.