keras-team/keras · error · ValueError

Improper config format for

Error message

Improper config format for {config}. Expecting python dict contains `class_name` and `config` as keys

What it means

Raised during deserialization when the config passed to class_and_config_for_serialized_keras_object is not a dict containing both the 'class_name' and 'config' keys. This is the standard envelope written by serialize_keras_object; a malformed or hand-constructed dict (missing keys, wrong casing, or a plain dict that was double-wrapped) fails this guard.

Solutions

  1. Use configs produced by serialize_keras_object or model_to_json unchanged: {'class_name': ..., 'config': {...}}
  2. If nesting configs manually, ensure the outer envelope has both 'class_name' and 'config' keys
  3. Avoid double-wrapping: do not pass an already-serialized object through serialize again
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/legacy/saving/serialization.py:343 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/040196c37b157c99. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/legacy/saving/serialization.py:343

        f"Cannot serialize {instance} because it doesn't implement "
        "`get_config()`."
    )


def class_and_config_for_serialized_keras_object(
    config,
    module_objects=None,
    custom_objects=None,
    printable_module_name="object",
):
    """Returns the class name and config for a serialized keras object."""

    if (
        not isinstance(config, dict)
        or "class_name" not in config
        or "config" not in config
    ):
        raise ValueError(
            f"Improper config format for {config}. "
            "Expecting python dict contains `class_name` and `config` as keys"
        )

    class_name = config["class_name"]
    cls = object_registration.get_registered_object(
        class_name, custom_objects, module_objects
    )
    if cls is None:
        raise ValueError(
            f"Unknown {printable_module_name}: '{class_name}'. "
            "Please ensure you are using a `keras.utils.custom_object_scope` "
            "and that this object is included in the scope. See "
            "https://www.tensorflow.org/guide/keras/save_and_serialize"
            "#registering_the_custom_object for details."
        )

    cls_config = config["config"]

View on GitHub (pinned to 7a34a03db6)