Lightning-AI/pytorch-lightning · error · ValueError

Precision {repr(precision)} is invalid. Allowed precision va

Error message

Precision {repr(precision)} is invalid. Allowed precision values: {supported_precision}

What it means

Raised by Fabric's connector when validating the `precision` argument passed to `Fabric(...)`. The value must be one of the supported precision flags (string forms like '32-true', '16-mixed', 'bf16-mixed', plus legacy aliases and int forms). Any other value fails this membership check against the concatenated `_PRECISION_INPUT_STR`, `_PRECISION_INPUT_INT`, and `_PRECISION_INPUT_STR_ALIAS` literals.

Source

Thrown at src/lightning/fabric/connector.py:565

        if env_value is not None and env_value != str(current) and str(current) != str(default) and _is_using_cli():
            raise ValueError(
                f"Your code has `Fabric({name}={current!r}, ...)` but it conflicts with the value "
                f"`--{name}={env_value}` set through the CLI. "
                " Remove it either from the CLI or from the Lightning Fabric object."
            )
        return env_value


def _convert_precision_to_unified_args(precision: Optional[_PRECISION_INPUT]) -> Optional[_PRECISION_INPUT_STR]:
    if precision is None:
        return None

    supported_precision = (
        get_args(_PRECISION_INPUT_STR) + get_args(_PRECISION_INPUT_INT) + get_args(_PRECISION_INPUT_STR_ALIAS)
    )
    if precision not in supported_precision:
        raise ValueError(f"Precision {repr(precision)} is invalid. Allowed precision values: {supported_precision}")

    precision = str(precision)  # convert int flags to str here to enable the legacy-conversion below

    if precision in get_args(_PRECISION_INPUT_STR_ALIAS):
        if str(precision)[:2] not in ("32", "64"):
            rank_zero_warn(
                f"`precision={precision}` is supported for historical reasons but its usage is discouraged. "
                f"Please set your precision to {_PRECISION_INPUT_STR_ALIAS_CONVERSION[precision]} instead!"
            )
        precision = _PRECISION_INPUT_STR_ALIAS_CONVERSION[precision]
    return cast(_PRECISION_INPUT_STR, precision)


def _is_using_cli() -> bool:
    return bool(int(os.environ.get("LT_CLI_USED", "0")))

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Use the new scheme: precision='16-mixed', 'bf16-mixed', '32-true', '64-true' (or legacy 16, 'bf16')
  2. Check the supported set programmatically: print the tuple shown in the error message
  3. Pin/align the lightning version so the precision string matches your installed version's supported list

Example fix

# before
fabric = Fabric(precision='fp16')
# after
fabric = Fabric(precision='16-mixed')
Defensive patterns

Strategy: validation

Validate before calling

from lightning.fabric.connector import _convert_precision_to_unified_args
try:
    _convert_precision_to_unified_args(precision)
except ValueError:
    precision = '16-mixed'

Type guard

def is_valid_precision(p):
    from typing import get_args
    from lightning.fabric.utilities import types as t
    return p in set(get_args(t._PRECISION_INPUT_STR) + get_args(t._PRECISION_INPUT_INT) + get_args(t._PRECISION_INPUT_STR_ALIAS))

Prevention

When it happens

Trigger: Passing an unsupported precision to Fabric, e.g. precision='fp16', precision='bfloat16', precision=8, or a typo like '16-mixe'. Also passing an old-style value after Lightning renamed precisions to the '<bits>-[true|mixed]' scheme.

Common situations: Migrating code from older Lightning versions that accepted 'bf16' or 16; copy-pasting precision strings from PyTorch forums (e.g. 'float16'); assuming AMP-style names like 'fp32' work.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/1e60cafa27bfd764. Report an issue: GitHub.