deezer/spleeter · error · ValueError

Unknown mode {mode}

Error message

Unknown mode {mode}

What it means

model_fn is the tf.estimator model function dispatcher: it routes PREDICT/EVAL/TRAIN modes to the corresponding builder and raises ValueError for any mode outside tf.estimator.ModeKeys. In practice this fires when an unsupported estimator mode is passed by the training/evaluation driver.

Source

Thrown at spleeter/model/__init__.py:585

            loss=loss, global_step=tf.compat.v1.train.get_global_step()
        )
        return tf.estimator.EstimatorSpec(
            mode=tf.estimator.ModeKeys.TRAIN,
            loss=loss,
            train_op=train_operation,
            eval_metric_ops=metrics,
        )


def model_fn(features, labels, mode, params):
    builder = EstimatorSpecBuilder(features, params)
    if mode == tf.estimator.ModeKeys.PREDICT:
        return builder.build_predict_model()
    elif mode == tf.estimator.ModeKeys.EVAL:
        return builder.build_evaluation_model(labels)
    elif mode == tf.estimator.ModeKeys.TRAIN:
        return builder.build_train_model(labels)
    raise ValueError(f"Unknown mode {mode}")

View on GitHub (pinned to c8854001ac)

Solutions

  1. Always pass a value from tf.estimator.ModeKeys (TRAIN, EVAL, or PREDICT)
  2. If you need another behavior, add a branch before the raise in model_fn
  3. Check the caller (training/eval script) for how mode is derived and fix the mapping
  4. Verify your TensorFlow version: custom estimator modes are not supported

Example fix

// before
estimator = tf.estimator.Estimator(model_fn=lambda features, labels, mode: model_fn(features, labels, mode, params), model_dir=...)
# with mode string 'infer'
// after
mode = tf.estimator.ModeKeys.PREDICT
estimator = tf.estimator.Estimator(model_fn=..., model_dir=...)
Defensive patterns

Strategy: validation

Validate before calling

import tensorflow as tf

def assert_valid_mode(mode):
    if mode not in (tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, tf.estimator.ModeKeys.PREDICT):
        raise ValueError(f"mode must be a tf.estimator.ModeKeys member, got {mode!r}")
    return mode

Type guard

import tensorflow as tf

def is_estimator_mode(mode) -> bool:
    try:
        return mode in (tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, tf.estimator.ModeKeys.PREDICT)
    except Exception:
        return False

Try / catch

try:
    estimator.train(input_fn)  # or eval/predict
except ValueError as e:
    if 'Unknown mode' in str(e):
        raise RuntimeError(f"model_fn received a non-estimator mode; use tf.estimator.ModeKeys: {e}") from e
    raise

Prevention

When it happens

Trigger: Calling model_fn (directly or via tf.estimator.Estimator with a custom mode_fn wrapper) with a mode not in {PREDICT, EVAL, TRAIN}, e.g. a custom string like 'infer' or a None mode from a misconfigured RunConfig.

Common situations: Custom training loops passing a hand-made mode string instead of tf.estimator.ModeKeys; TF2 migration where estimator plumbing changes; tests invoking model_fn with mock modes.

Related errors


AI-assisted analysis of deezer/spleeter@c8854001ac (2026-08-28). Data as JSON: /api/errors/5adaf8037d95767b. Report an issue: GitHub.