{"record":{"id":"02e9421e523856c9","repo":"huggingface/transformers","slug":"found-label-key-in-inputs-loss-computation-is","errorCode":null,"errorMessage":"Found '{label_key}' in inputs. Loss computation is not supported during export. Please remove '{label_key}' from your inputs before calling export().","messagePattern":"Found '(.+?)' in inputs\\. Loss computation is not supported during export\\. Please remove '(.+?)' from your inputs before calling export\\(\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/exporters/utils.py","lineNumber":364,"sourceCode":") -> tuple[PreTrainedModel | torch.nn.Module, MutableMapping[str, Any], dict[str, Any]]:\n    \"\"\"Configure model and inputs for export. Mutates both `model` and `inputs` in place,\n    returning `(model, inputs, output_flags)` where `output_flags` holds the values popped\n    from `inputs` for `use_cache`, `return_dict`, etc. (to be applied reversibly onto\n    `model.config` by `patch_model_config` during the trace).\n\n    - Strips label inputs (`labels`, `future_values`) — loss computation is unsupported.\n    - Pops output flags (`use_cache`, `return_dict`, …) from `inputs` so they don't appear\n      as traced kwargs; the values are returned for the trace block to apply onto\n      `model.config`.\n    - Pre-computes data-dependent vision/audio kwargs registered via\n      `@register_export_input_preparer` and writes them into `inputs`.\n    - Casts input tensors to match the model's `dtype` / `device`.\n    \"\"\"\n    # Strip label inputs — loss computation is not supported during export.\n    for label_key in (\"labels\", \"future_values\"):\n        value = inputs.pop(label_key, None)\n        if value is not None:\n            raise ValueError(\n                f\"Found '{label_key}' in inputs. Loss computation is not supported during export. \"\n                f\"Please remove '{label_key}' from your inputs before calling export().\"\n            )\n    if hasattr(model, \"config\") and getattr(model.config, \"return_loss\", False):\n        raise ValueError(\n            \"Found 'model.config.return_loss=True'. Loss computation is not supported during export. \"\n            \"Please set 'model.config.return_loss=False' before calling export().\"\n        )\n    if inputs.get(\"return_loss\", False):\n        raise ValueError(\n            \"Found 'return_loss=True' in inputs. Loss computation is not supported during export. \"\n            \"Please remove 'return_loss' from your inputs or set it to False.\"\n        )\n\n    # Pop output flags from `inputs` and return them so the caller can decide how to\n    # honour them during the trace (we don't want them as traced kwargs).\n    output_flags = {flag: inputs.pop(flag) for flag in _OUTPUT_FLAGS if flag in inputs}\n","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/exporters/utils.py#L346-L382","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pop the keys before export: inputs.pop('labels', None); inputs.pop('future_values', None).","Also set model.config.return_loss = False and remove 'return_loss' from inputs, or the adjacent guards will raise next.","Build export sample_inputs from an inference-only collator rather than reusing training batches."],"exampleFix":"# before\nexporter.export(model, batch, config=cfg)  # batch contains 'labels'\n\n# after\nfor k in (\"labels\", \"future_values\", \"return_loss\"):\n    batch.pop(k, None)\nmodel.config.return_loss = False\nexporter.export(model, batch, config=cfg)","handlingStrategy":"validation","validationCode":"def sanitize_for_export(model, inputs):\n    for k in (\"labels\", \"future_values\", \"return_loss\"):\n        inputs.pop(k, None)\n    if getattr(getattr(model, \"config\", None), \"return_loss\", False):\n        model.config.return_loss = False\n    return inputs\n\nsample_inputs = sanitize_for_export(model, batch)\nexporter.export(model, sample_inputs, cfg)","typeGuard":"def is_inference_only(inputs) -> bool:\n    return not ({\"labels\", \"future_values\"} & inputs.keys()) and not inputs.get(\"return_loss\", False)","tryCatchPattern":"try:\n    exporter.export(model, inputs, cfg)\nexcept ValueError as e:\n    if \"Loss computation is not supported\" in str(e):\n        for k in (\"labels\", \"future_values\", \"return_loss\"):\n            inputs.pop(k, None)\n        model.config.return_loss = False\n        exporter.export(model, inputs, cfg)\n    else:\n        raise","preventionTips":["Use an inference-only collator for export; never feed training batches directly","Strip labels/future_values in a shared helper before any export or trace call","Check model.config.return_loss for forecasting models before exporting"],"tags":["export","inputs","training-data","validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}