Skip to content

fix: raise ValueError in deserialize_keras_object when required Keras config keys are missing#22710

Closed
AlgorithmicDeer wants to merge 2 commits intokeras-team:masterfrom
AlgorithmicDeer:master
Closed

fix: raise ValueError in deserialize_keras_object when required Keras config keys are missing#22710
AlgorithmicDeer wants to merge 2 commits intokeras-team:masterfrom
AlgorithmicDeer:master

Conversation

@AlgorithmicDeer
Copy link
Copy Markdown

Fixes #22704

Description

deserialize_keras_object silently returned a raw dict when the input was missing class_name but contained Keras-specific keys like module or registered_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 module or registered_name (keys that only appear in Keras-serialized objects), raise a descriptive ValueError listing 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 the module_objects code path.
Tests added (serialization_lib_test.py):

  • test_deserialize_missing_class_name_raises — reproduces the exact bug
  • test_deserialize_missing_class_name_with_registered_name_raises — same bug via registered_name
  • test_deserialize_plain_dict_no_keras_keys_passthrough — regression guard for plain dicts

Contributor Agreement

Please check all boxes below before submitting your PR for review:

  • I am a human, and not a bot.
  • I will be responsible for responding to review comments in a timely manner.
  • I will work with the maintainers to push this PR forward until submission.

Note: Failing to adhere to this agreement may result in your future PRs no longer being reviewed.

…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
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 18, 2026

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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread keras/src/saving/serialization_lib.py
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.96%. Comparing base (e94cb07) to head (34fc240).
⚠️ Report is 37 commits behind head on master.

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           
Flag Coverage Δ
keras 82.77% <100.00%> (+<0.01%) ⬆️
keras-jax 58.72% <100.00%> (+<0.01%) ⬆️
keras-numpy 54.56% <100.00%> (+<0.01%) ⬆️
keras-openvino 59.43% <100.00%> (+<0.01%) ⬆️
keras-tensorflow 60.28% <100.00%> (+<0.01%) ⬆️
keras-torch 59.06% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@keerthanakadiri keerthanakadiri added the stat:awaiting keras-eng Awaiting response from Keras engineer label Apr 23, 2026
@hertschuh
Copy link
Copy Markdown
Collaborator

Closing since #22704 was closed as working as designed.

@hertschuh hertschuh closed this Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S stat:awaiting keras-eng Awaiting response from Keras engineer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] deserialize_keras_object returns dict when top-level structure is incomplete

5 participants