huggingface/transformers · error · ValueError

Found '{label_key}' in inputs. Loss computation is not suppo

Error message

Found '{label_key}' in inputs. Loss computation is not supported during export. Please remove '{label_key}' from your inputs before calling export().

What it means

prepare_for_export (run at the start of every exporter) refuses inputs containing 'labels' or 'future_values': export targets inference graphs only, and tracing a loss computation is unsupported. The error tells you to remove the key from your inputs before calling export(). Related guards on the same path also reject model.config.return_loss=True and inputs['return_loss']=True.

Source

Thrown at src/transformers/exporters/utils.py:364

) -> tuple[PreTrainedModel | torch.nn.Module, MutableMapping[str, Any], dict[str, Any]]:
    """Configure model and inputs for export. Mutates both `model` and `inputs` in place,
    returning `(model, inputs, output_flags)` where `output_flags` holds the values popped
    from `inputs` for `use_cache`, `return_dict`, etc. (to be applied reversibly onto
    `model.config` by `patch_model_config` during the trace).

    - Strips label inputs (`labels`, `future_values`) — loss computation is unsupported.
    - Pops output flags (`use_cache`, `return_dict`, …) from `inputs` so they don't appear
      as traced kwargs; the values are returned for the trace block to apply onto
      `model.config`.
    - Pre-computes data-dependent vision/audio kwargs registered via
      `@register_export_input_preparer` and writes them into `inputs`.
    - Casts input tensors to match the model's `dtype` / `device`.
    """
    # Strip label inputs — loss computation is not supported during export.
    for label_key in ("labels", "future_values"):
        value = inputs.pop(label_key, None)
        if value is not None:
            raise ValueError(
                f"Found '{label_key}' in inputs. Loss computation is not supported during export. "
                f"Please remove '{label_key}' from your inputs before calling export()."
            )
    if hasattr(model, "config") and getattr(model.config, "return_loss", False):
        raise ValueError(
            "Found 'model.config.return_loss=True'. Loss computation is not supported during export. "
            "Please set 'model.config.return_loss=False' before calling export()."
        )
    if inputs.get("return_loss", False):
        raise ValueError(
            "Found 'return_loss=True' in inputs. Loss computation is not supported during export. "
            "Please remove 'return_loss' from your inputs or set it to False."
        )

    # Pop output flags from `inputs` and return them so the caller can decide how to
    # honour them during the trace (we don't want them as traced kwargs).
    output_flags = {flag: inputs.pop(flag) for flag in _OUTPUT_FLAGS if flag in inputs}

View on GitHub (pinned to a597f97485)

Solutions

  1. Pop the keys before export: inputs.pop('labels', None); inputs.pop('future_values', None).
  2. Also set model.config.return_loss = False and remove 'return_loss' from inputs, or the adjacent guards will raise next.
  3. Build export sample_inputs from an inference-only collator rather than reusing training batches.

Example fix

# before
exporter.export(model, batch, config=cfg)  # batch contains 'labels'

# after
for k in ("labels", "future_values", "return_loss"):
    batch.pop(k, None)
model.config.return_loss = False
exporter.export(model, batch, config=cfg)
Defensive patterns

Strategy: validation

Validate before calling

def sanitize_for_export(model, inputs):
    for k in ("labels", "future_values", "return_loss"):
        inputs.pop(k, None)
    if getattr(getattr(model, "config", None), "return_loss", False):
        model.config.return_loss = False
    return inputs

sample_inputs = sanitize_for_export(model, batch)
exporter.export(model, sample_inputs, cfg)

Type guard

def is_inference_only(inputs) -> bool:
    return not ({"labels", "future_values"} & inputs.keys()) and not inputs.get("return_loss", False)

Try / catch

try:
    exporter.export(model, inputs, cfg)
except ValueError as e:
    if "Loss computation is not supported" in str(e):
        for k in ("labels", "future_values", "return_loss"):
            inputs.pop(k, None)
        model.config.return_loss = False
        exporter.export(model, inputs, cfg)
    else:
        raise

Prevention

When it happens

Trigger: Passing a training batch (labels included) straight from your data collator into exporter.export(); time-series models whose forward takes future_values (e.g. TimeSeriesTransformer); reusing a Trainer/prepare_inputs payload for export.

Common situations: Exporting right after a training run with the same dataloader; porting a fine-tuning script's batch into an export script; forecasting models where future_values is part of the standard forward signature.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/02e9421e523856c9. Report an issue: GitHub.