mlflow/mlflow · error · ValueError

Value for key {key} in onnx_session_options should be 0, 1,

Error message

Value for key {key} in onnx_session_options should be 0, 1, 2, or 99, not {value}

What it means

When `graph_optimization_level` is provided in `onnx_session_options`, MLflow restricts it to the integers 0, 1, 2, or 99, which map to ORT's ORT_DISABLE_ALL, ORT_ENABLE_BASIC, ORT_ENABLE_EXTENDED, and ORT_ENABLE_ALL levels. Any other value (e.g. 3, strings, floats) raises this ValueError.

Source

Thrown at mlflow/utils/model_utils.py:379

                raise ValueError(
                    f"Key {key} in onnx_session_options is not a valid "
                    "ONNX Runtime session options key"
                )
            elif key == "extra_session_config" and not isinstance(value, dict):
                raise TypeError(
                    f"Value for key {key} in onnx_session_options should be a dict, "
                    "not {type(value)}"
                )
            elif key == "execution_mode" and value.upper() not in [
                "PARALLEL",
                "SEQUENTIAL",
            ]:
                raise ValueError(
                    f"Value for key {key} in onnx_session_options should be "
                    f"'parallel' or 'sequential', not {value}"
                )
            elif key == "graph_optimization_level" and value not in [0, 1, 2, 99]:
                raise ValueError(
                    f"Value for key {key} in onnx_session_options should be 0, 1, 2, or 99, "
                    f"not {value}"
                )
            elif key in ["intra_op_num_threads", "intra_op_num_threads"] and value < 0:
                raise ValueError(
                    f"Value for key {key} in onnx_session_options should be >= 0, not {value}"
                )


def _get_overridden_pyfunc_model_config(
    pyfunc_config: dict[str, Any], load_config: dict[str, Any], logger
) -> dict[str, Any]:
    """
    Updates the inference configuration according to the model's configuration and the overrides.
    Only arguments already present in the inference configuration can be updated. The environment
    variable ``MLFLOW_PYFUNC_INFERENCE_CONFIG`` can also be used to provide additional inference
    configuration.
    """

View on GitHub (pinned to 6a27f2decc)

Solutions

  1. Use one of the allowed integers: 0, 1, 2, or 99
  2. Replace a string like 'ORT_ENABLE_ALL' with the equivalent integer 99
  3. Remove the key to accept the runtime default optimization level

Example fix

// before
onnx_session_options={'graph_optimization_level': 'ORT_ENABLE_ALL'}
// after
onnx_session_options={'graph_optimization_level': 99}
Defensive patterns

Strategy: validation

Validate before calling

lvl = opts.get('graph_optimization_level')
if lvl is not None and lvl not in (0, 1, 2, 99):
    raise ValueError(f'Invalid graph_optimization_level: {lvl}')

Type guard

def is_valid_opt_level(v) -> bool:
    return v is None or (isinstance(v, int) and v in (0, 1, 2, 99))

Try / catch

try:
    mlflow.onnx.save_model(model, path, onnx_session_options=opts)
except ValueError as e:
    if 'graph_optimization_level' in str(e):
        opts['graph_optimization_level'] = 99
    raise

Prevention

When it happens

Trigger: `save_model(..., onnx_session_options={'graph_optimization_level': 3})` or passing the string 'ORT_ENABLE_ALL' instead of the integer 99.

Common situations: Copying a value from onnxruntime's GraphOptimizationLevel enum (0/1/2/99) but using a level like 3; passing a string level name rather than the integer.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of mlflow/mlflow@6a27f2decc (2026-08-29). Data as JSON: /api/errors/deb42b7af0b4539e. Report an issue: GitHub.