Skip to content

[Bug] deserialize_keras_object validates trainable field as boolean#22719

Closed
Jah-yee wants to merge 2 commits intokeras-team:masterfrom
Jah-yee:fix/22699-trainable-bool-validation
Closed

[Bug] deserialize_keras_object validates trainable field as boolean#22719
Jah-yee wants to merge 2 commits intokeras-team:masterfrom
Jah-yee:fix/22699-trainable-bool-validation

Conversation

@Jah-yee
Copy link
Copy Markdown

@Jah-yee Jah-yee commented Apr 19, 2026

Reject non-boolean trainable values when deserializing Keras objects.

Fixes #22699

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 19, 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 introduces validation logic for Keras object deserialization, specifically ensuring that the configuration is a dictionary and that the trainable attribute is a boolean. Review feedback identifies two critical issues: the validation logic incorrectly flags __typespec__ objects which use lists for configuration, and the trainable check can cause a TypeError when deserializing functions because it performs a substring search on a string instead of a dictionary lookup.

Comment on lines +624 to +628
if (
inner_config is not None
and not isinstance(inner_config, dict)
and class_name != "function"
):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The validation for inner_config being a dict incorrectly flags __typespec__ objects, which use a list for their configuration. This will cause deserialization of tf.TypeSpec objects to fail with a TypeError because their inner_config is a list, not a dict.

Suggested change
if (
inner_config is not None
and not isinstance(inner_config, dict)
and class_name != "function"
):
if (
inner_config is not None
and not isinstance(inner_config, dict)
and class_name not in ("function", "__typespec__")
):


# Validate trainable field if present in inner_config.
# trainable should always be a bool for Keras layers.
if inner_config and "trainable" in inner_config:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The check if inner_config and "trainable" in inner_config will crash with a TypeError when class_name is "function" if the function name contains the substring "trainable". For functions, inner_config is a string, and "trainable" in inner_config performs a substring check. If it returns True, the subsequent line inner_config["trainable"] attempts to index a string with a string key, which is invalid.

Suggested change
if inner_config and "trainable" in inner_config:
if isinstance(inner_config, dict) and "trainable" in inner_config:

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 19, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.00%. Comparing base (d8adf74) to head (6ff757e).

Files with missing lines Patch % Lines
keras/src/saving/serialization_lib.py 50.00% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #22719      +/-   ##
==========================================
+ Coverage   77.69%   83.00%   +5.30%     
==========================================
  Files         596      596              
  Lines       69688    69695       +7     
  Branches    10869    10872       +3     
==========================================
+ Hits        54147    57852    +3705     
+ Misses      12789     8998    -3791     
- Partials     2752     2845      +93     
Flag Coverage Δ
keras 82.82% <50.00%> (+5.22%) ⬆️
keras-jax 58.43% <50.00%> (-0.01%) ⬇️
keras-numpy 54.29% <50.00%> (-0.01%) ⬇️
keras-openvino 59.68% <50.00%> (-0.01%) ⬇️
keras-tensorflow 59.99% <50.00%> (-0.01%) ⬇️
keras-torch 58.77% <50.00%> (?)

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.

RoomWithOutRoof and others added 2 commits April 22, 2026 21:53
When deserialize_keras_object was called with a config dict where
the 'config' field was not a dict (e.g., a list or string), the
code would raise a confusing AttributeError: 'list' object has no
attribute 'get'. This was because from_config() expects a dict.

Now raises a clear TypeError:
  TypeError: Expected 'config' to be a dict, but got list: [1, 2, 3]

The fix is applied only for class deserialization (not for
'class_name == function' where inner_config is a string name).

Fixes keras-team#22701
Reject non-boolean trainable values (e.g. strings like 'yes', integers,
None) when deserializing Keras objects. Previously, invalid trainable
values were silently accepted, causing inconsistent behavior in code
relying on strict boolean checks.

Fixes keras-team#22699
@Jah-yee Jah-yee force-pushed the fix/22699-trainable-bool-validation branch from ac7f68d to 6ff757e Compare April 22, 2026 13:53
@keerthanakadiri
Copy link
Copy Markdown
Contributor

HI @Jah-yee, Can you please sign the CLA? Thank you!

@hertschuh
Copy link
Copy Markdown
Collaborator

This issue was addressed by #22740

@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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] deserialize_keras_object accepts non-boolean values for trainable field

5 participants