{"record":{"id":"71550fb6de158460","repo":"huggingface/transformers","slug":"problem-type-single-label-classification-requi","errorCode":null,"errorMessage":"`problem_type=\"single_label_classification\"` requires `num_labels > 1`. For binary classification use `num_labels=2`, or use `problem_type=\"regression\"` for a single-output regression head.","messagePattern":"`problem_type=\"single_label_classification\"` requires `num_labels > 1`\\. For binary classification use `num_labels=2`, or use `problem_type=\"regression\"` for a single-output regression head\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/configuration_utils.py","lineNumber":305,"sourceCode":"\n            self.dtype = getattr(torch, self.dtype)\n\n        # Keep the default value of `num_labels=2` in case users have saved a classifier with 2 labels\n        # Our configs prev wouldn't save `id2label` for 2 labels because it is the default. In all other\n        # cases we expect the config dict to have an `id2label` field if it's a clf model, or not otherwise\n        if self.id2label is None:\n            self.num_labels = kwargs.get(\"num_labels\", self.num_labels if self.num_labels is not None else 2)\n        else:\n            if kwargs.get(\"num_labels\") is not None and len(self.id2label) != kwargs.get(\"num_labels\"):\n                logger.warning(\n                    f\"You passed `num_labels={kwargs.get('num_labels')}` which is incompatible to \"\n                    f\"the `id2label` map of length `{len(self.id2label)}`.\"\n                )\n            # Keys are always strings in JSON so convert ids to int\n            self.id2label = {int(key): value for key, value in self.id2label.items()}\n\n        if self.problem_type == \"single_label_classification\" and self.num_labels == 1:\n            raise ValueError(\n                '`problem_type=\"single_label_classification\"` requires `num_labels > 1`. For binary '\n                'classification use `num_labels=2`, or use `problem_type=\"regression\"` for a '\n                \"single-output regression head.\"\n            )\n\n        # BC for rotary embeddings. We will pop out legacy keys from kwargs and rename to new format\n        if hasattr(self, \"rope_parameters\"):\n            kwargs = self.convert_rope_params_to_dict(**kwargs)\n        elif kwargs.get(\"rope_scaling\") and kwargs.get(\"rope_theta\"):\n            logger.warning(\n                f\"{self.__class__.__name__} got `key=rope_scaling` in kwargs but hasn't set it as attribute. \"\n                \"For RoPE standardization you need to set `self.rope_parameters` in model's config. \"\n            )\n            kwargs = self.convert_rope_params_to_dict(**kwargs)\n\n        # Parameters for sequence generation saved in the config are popped instead of loading them.\n        for parameter_name in GenerationConfig._get_default_generation_params().keys():\n            kwargs.pop(parameter_name, None)","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/configuration_utils.py#L287-L323","documentation":"ValueError raised during config post-init when problem_type is 'single_label_classification' but num_labels resolves to 1. Single-label classification mathematically needs at least two classes; a single output is regression, so the config refuses the contradictory combination instead of producing a broken head at runtime.","triggerScenarios":"MyConfig(problem_type='single_label_classification', num_labels=1) or num_labels defaulting to 1 via an id2label map of length 1. Also passing id2label={0: 'label'} which sets num_labels=1.","commonSituations":"Adapting a binary classifier and setting num_labels=1 out of habit from other frameworks; datasets with a single class after filtering; converting a regression checkpoint and forgetting to change problem_type.","solutions":["For binary classification use num_labels=2 (or drop num_labels to let it default)","If you truly have one continuous output, set problem_type='regression'","If id2label drove num_labels to 1, provide a two-entry id2label for classification"],"exampleFix":"# before\ncfg = MyConfig(problem_type='single_label_classification', num_labels=1)\n# after\ncfg = MyConfig(problem_type='single_label_classification', num_labels=2)\n# or for one continuous target\ncfg = MyConfig(problem_type='regression', num_labels=1)","handlingStrategy":"validation","validationCode":"if problem_type == 'single_label_classification':\n    assert num_labels and num_labels > 1, 'use num_labels=2 (binary) or problem_type=regression'","typeGuard":"def is_valid_label_setup(problem_type: str, num_labels: int) -> bool:\n    return not (problem_type == 'single_label_classification' and num_labels == 1)","tryCatchPattern":"try:\n    cfg = MyConfig(problem_type=pt, num_labels=n)\nexcept ValueError as e:\n    if 'num_labels > 1' in str(e):\n        n = 2 if pt == 'single_label_classification' else n\n        cfg = MyConfig(problem_type=pt, num_labels=n)\n    else:\n        raise","preventionTips":["Remember binary classification in transformers is num_labels=2, not 1","Derive num_labels from len(id2label) and sanity-check it against problem_type"],"tags":["config","classification","validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}