fix KeyError: 'weight_mode' when packing v2.1 recipes with under-specified gradients#470
Open
rugeli wants to merge 4 commits into
Open
fix KeyError: 'weight_mode' when packing v2.1 recipes with under-specified gradients#470rugeli wants to merge 4 commits into
rugeli wants to merge 4 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a packing-time KeyError: 'weight_mode' for v2.1 recipes whose gradients are under-specified (i.e., they skip v2.0→v2.1 migration, so gradient defaults never get filled). The core change is to normalize gradients earlier in RecipeLoader._read() so gradients are consistently list-of-dicts and have required top-level keys before validation and before constructing Gradient instances.
Changes:
- Add
RecipeLoader._normalize_gradients()to convert dict→list gradient format and fill missing top-level gradient keys from defaults prior to validation. - Remove the older late-stage “gradients dict→list” conversion block and move
serializable_recipe_datacapture to occur after dict-level normalization. - Add a reproduction recipe/config under
cellpack/tests/for manual verification of gradient-mixing packing.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
cellpack/autopack/loaders/recipe_loader.py |
Adds gradient normalization + reorders normalization/validation/serialization to avoid missing gradient keys in v2.1 recipes. |
cellpack/tests/recipes/v2/test_peroxisome_combined_gradient.json |
Adds a v2.1 recipe fixture demonstrating combined gradients / under-specified gradient keys. |
cellpack/tests/packing-configs/test_gradient_mixing.json |
Adds a packing config used to reproduce the gradient-mixing scenario. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
meganrm
approved these changes
Jun 3, 2026
592a091 to
460ab53
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Problem
closes #465
Sorry for the delay on this one, after tracing it back through the packing pipeline, I figured the gradient packing error is not a problem with our Pydantic schema. The root cause is a data-pipeline gap. Gradient defaults were only ever filled in one place: the v2.0→v2.1 migration. But
recipe_loader._readonly runs migration when format_version != current_version. A recipe hand-authored directly as 2.1 skips migration entirely, so its gradients reachGradient.__init__still missing keys.In short, without the changes in this pr, relabeling
format_versionas2.0, everything else stays the same, the recipecellpack/tests/recipes/v2/test_peroxisome_combined_gradient.jsonwould pass.Solution
in
recipe_loader:with @mogres
Note: If we want to further refactor, we can:
validate_recipebuilds a normalized copy that_readcurrently discards, we currently only use validator for validating which makes sense to me. But if we want to, we can make the validated recipe source of truth. This will be a larger pr so I'd keep this separate.Type of change
Steps to Verify:
pack -r cellpack/tests/recipes/v2/test_peroxisome_combined_gradient.json -c cellpack/tests/packing-configs/test_gradient_mixing.json