keras-team/keras · error · ValueError

Requested the deserialization of a `Lambda` layer whose `fun

Error message

Requested the deserialization of a `Lambda` layer whose `function` is a Python lambda. This carries a potential risk of arbitrary code execution and thus it is disallowed by default. If you trust the source of the artifact, you can override this error by passing `safe_mode=False` to the loading function, or calling `keras.config.enable_unsafe_deserialization().

What it means

Error "Requested the deserialization of a `Lambda` layer whose `function` is a Python lambda. This carries a potential risk of arbitrary code execution and thus it is disallowed by default. If you trust the source of the artifact, you can override this error by passing `safe_mode=False` to the loading function, or calling `keras.config.enable_unsafe_deserialization()." thrown in keras-team/keras.

Source

Thrown at keras/src/layers/core/lambda_layer.py:172

            return {
                "class_name": "__lambda__",
                "config": {
                    "code": code,
                    "defaults": defaults,
                    "closure": closure,
                },
            }
        elif callable(fn):
            return serialization_lib.serialize_keras_object(fn)
        raise ValueError(
            "Invalid input type for serialization. "
            f"Received: {fn} of type {type(fn)}."
        )

    @staticmethod
    def _raise_for_lambda_deserialization(safe_mode):
        if safe_mode:
            raise ValueError(
                "Requested the deserialization of a `Lambda` layer whose "
                "`function` is a Python lambda. This carries a potential risk "
                "of arbitrary code execution and thus it is disallowed by "
                "default. If you trust the source of the artifact, you can "
                "override this error by passing `safe_mode=False` to the "
                "loading function, or calling "
                "`keras.config.enable_unsafe_deserialization()."
            )

    @classmethod
    def from_config(cls, config, custom_objects=None, safe_mode=None):
        # When `safe_mode` is unset, fall back to the ambient deserialization
        # scope, treating an unset scope as safe. This fails closed so that
        # deserializing a `Lambda` outside of a `SafeModeScope` (e.g. a direct
        # `from_config` call) does not silently allow arbitrary code execution.
        effective_safe_mode = (
            safe_mode
            if safe_mode is not None

View on GitHub (pinned to 7a34a03db6)

When it happens

Trigger: Thrown at keras/src/layers/core/lambda_layer.py:172 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/478a71a8cc924253. Report an issue: GitHub.