keras-team/keras · error · ValueError

A Sequential model configuration must be a dictionary contai

Error message

A Sequential model configuration must be a dictionary containing the 'name' and 'layers' keys. Received: config={config}

What it means

Sequential.from_config expects the dict produced by get_config(), which must include 'name' and 'layers'. A dict lacking 'name' is treated as an invalid configuration and rejected.

Source

Thrown at keras/src/models/sequential.py:369

        for layer in super().layers:
            # `super().layers` include the InputLayer if available (it is
            # filtered out of `self.layers`).
            layer_configs.append(serialize_fn(layer))
        config = Model.get_config(self)
        config["name"] = self.name
        config["layers"] = copy.deepcopy(layer_configs)
        if self._functional is not None:
            config["build_input_shape"] = self._layers[0].batch_shape
        return config

    @classmethod
    def from_config(cls, config, custom_objects=None):
        if "name" in config:
            name = config["name"]
            build_input_shape = config.get("build_input_shape")
            layer_configs = config["layers"]
        else:
            raise ValueError(
                "A Sequential model configuration must be "
                "a dictionary containing the 'name' and "
                f"'layers' keys. Received: config={config}"
            )
        model = cls(name=name)
        for layer_config in layer_configs:
            if "module" not in layer_config:
                # Legacy format deserialization (no "module" key)
                # used for H5 and SavedModel formats
                layer = saving_utils.model_from_config(
                    layer_config,
                    custom_objects=custom_objects,
                )
            else:
                layer = serialization_lib.deserialize_keras_object(
                    layer_config,
                    custom_objects=custom_objects,
                )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Use the dict from model.get_config() unmodified
  2. Add the missing 'name' (and keep 'layers') keys before calling from_config
  3. For bare layer lists, pass the list form that from_config also accepts

Example fix

# before
model = keras.Sequential.from_config({'layers': [...]})

# after
cfg = {'name': 'sequential_1', 'layers': [...]}
model = keras.Sequential.from_config(cfg)
Defensive patterns

Strategy: validation

Validate before calling

def is_sequential_config(cfg):
    return (isinstance(cfg, dict) and 'name' in cfg
            and isinstance(cfg.get('layers'), list))

Try / catch

try:
    model = keras.Sequential.from_config(cfg)
except ValueError:
    if 'name' not in cfg or 'layers' not in cfg:
        raise
    cfg.setdefault('name', 'sequential')
    model = keras.Sequential.from_config(cfg)

Prevention

When it happens

Trigger: keras.Sequential.from_config({'layers': [...]}) or from_config with an unexpected structure

Common situations: Loading hand-edited JSON, YAML-derived dicts, or configs from a different serialization format or Keras version

Understand the failure class

Background: Config validation failed: what "invalid value for {key}" and settings-rejection errors mean across 19 open-source libraries — this error's family across 19 libraries.

Related errors


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