fix: raise ValueError in deserialize_keras_object when required Keras config keys are missing#22710
fix: raise ValueError in deserialize_keras_object when required Keras config keys are missing#22710AlgorithmicDeer wants to merge 2 commits intokeras-team:masterfrom
Conversation
…are missing When a config dict contains Keras-specific keys (module/registered_name) but is missing class_name or config, raise a descriptive ValueError instead of silently returning a raw dict. Fixes keras-team#22704
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request enhances the deserialize_keras_object function to explicitly validate Keras configuration dictionaries. It now raises a ValueError when a dictionary contains Keras-specific keys like module or registered_name but lacks the required class_name or config keys, preventing silent failures or incorrect passthrough. Corresponding unit tests were added to verify this behavior and ensure plain dictionaries still pass through correctly. Feedback was provided to simplify the error-raising logic and refine the error message for better readability and alignment with Keras API guidelines.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #22710 +/- ##
=======================================
Coverage 82.95% 82.96%
=======================================
Files 596 596
Lines 69252 69255 +3
Branches 10814 10815 +1
=======================================
+ Hits 57451 57454 +3
Misses 8973 8973
Partials 2828 2828
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Closing since #22704 was closed as working as designed. |
Fixes #22704
Description
deserialize_keras_objectsilently returned a rawdictwhen the input was missingclass_namebut contained Keras-specific keys likemoduleorregistered_name. This made it impossible for callers to detect a failed deserialization.Root cause: The block meant to handle plain Python dicts also accidentally caught malformed Keras configs.
Fix: Added a guard — if the incomplete dict contains
moduleorregistered_name(keys that only appear in Keras-serialized objects), raise a descriptiveValueErrorlisting exactly which keys are missing. Plain dicts without those keys continue to pass through unchanged, preserving existing behavior. This is consistent with the same validation already present in themodule_objectscode path.Tests added (
serialization_lib_test.py):test_deserialize_missing_class_name_raises— reproduces the exact bugtest_deserialize_missing_class_name_with_registered_name_raises— same bug viaregistered_nametest_deserialize_plain_dict_no_keras_keys_passthrough— regression guard for plain dictsContributor Agreement
Please check all boxes below before submitting your PR for review: