keras-team/keras · error · ImportError

To use NNX with the JAX backend, you must install `flax`.

Error message

To use NNX with the JAX backend, you must install `flax`.

What it means

Raised by keras.config.set_nnx_enabled(True) when the flax package cannot be imported. NNX features for the JAX backend are built on flax.nnx, so the import guard in this setter fails with an ImportError when flax is missing.

Solutions

  1. Install flax: pip install flax (an optional dependency for JAX-backend NNX features).
  2. If NNX is not intended, do not enable it (set_nnx_enabled(False) or leave it off).
  3. Verify with: python -c "from flax import nnx".
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at keras/src/backend/config.py:260 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/c5163f00a2c79a69. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/config.py:260

    """Checks whether NNX specific features are enabled for the JAX backend.

    Returns:
        bool: `True` if NNX backend features are enabled, `False` otherwise.
        Defaults to `False`.
    """
    return _NNX_ENABLED


def set_nnx_enabled(value):
    global _NNX_ENABLED
    from keras.src.backend.common import global_state

    _NNX_ENABLED = bool(value)
    if _NNX_ENABLED:
        try:
            from flax import nnx  # noqa F401
        except ImportError:
            raise ImportError(
                "To use NNX with the JAX backend, you must install `flax`."
            )
    global_state.set_global_attribute("nnx_enabled", bool(value))


def standardize_data_format(data_format):
    if data_format is None:
        return image_data_format()
    data_format = str(data_format).lower()
    if data_format not in {"channels_first", "channels_last"}:
        raise ValueError(
            "The `data_format` argument must be one of "
            "{'channels_first', 'channels_last'}. "
            f"Received: data_format={data_format}"
        )
    return data_format

View on GitHub (pinned to 7a34a03db6)