huggingface/transformers · error · ValueError

You have modified the pretrained model configuration to cont

Error message

You have modified the pretrained model configuration to control generation We detected the following values set - {self.config._get_generation_parameters()}. This strategy to control generation is not supported anymore. Please use and modify `model.generation_config` (see https://huggingface.co/docs/transformers/generation_strategies#default-text-generation-configuration )

What it means

Historically users set generation knobs (`max_length`, `do_sample`, ...) on `model.config`. That pathway was removed: generation is controlled only by `model.generation_config`. Before building a fresh default `GenerationConfig`, `generate` checks `config._get_generation_parameters()` and raises if any legacy generation attribute is set on the model config.

Source

Thrown at src/transformers/generation/utils.py:1788

    def _prepare_generation_config(
        self: "GenerativePreTrainedModel",
        generation_config: GenerationConfig | None,
        **kwargs: Any,
    ) -> tuple[GenerationConfig, dict]:
        """
        Prepares the base generation config, then applies any generation configuration options from kwargs. This
        function handles retrocompatibility with respect to configuration files.
        """
        # parameterization priority:
        # user-defined kwargs or `generation_config` > `self.generation_config` > global default values
        # TODO (joao): per-model generation config classes.

        generation_config_provided = generation_config is not None
        if generation_config is None:
            # Users may modify `model.config` to control generation. This is a legacy behavior and is not supported anymore
            if len(self.config._get_generation_parameters()) > 0:
                raise ValueError(
                    "You have modified the pretrained model configuration to control generation "
                    f"We detected the following values set - {self.config._get_generation_parameters()}. "
                    "This strategy to control generation is not supported anymore. Please use and modify `model.generation_config` "
                    "(see https://huggingface.co/docs/transformers/generation_strategies#default-text-generation-configuration )",
                )
            generation_config = GenerationConfig()

        # `torch.export.export` usually raises an exception if it is called
        # with ``strict=True``. deepcopy can only be processed if ``strict=False``.
        generation_config = copy.deepcopy(generation_config)

        # First set values from the loaded `self.generation_config`, then set default values (BC)
        #
        # Only update values that are `None`, i.e. these values were not explicitly set by users to `generate()`,
        # or values that are not present in the current config, i.e. custom entries that were set via `**kwargs`.
        # Thus we use the specific kwargs `defaults_only=True` (`None` values only) and `allow_custom_entries=True`
        # (custom entries are carried over).
        global_defaults = self.generation_config._get_default_generation_params()

View on GitHub (pinned to a597f97485)

Solutions

  1. Move the settings: `model.generation_config.max_length = 100` (and unset them from `model.config`).
  2. Or pass a `GenerationConfig` directly: `model.generate(**inputs, generation_config=GenerationConfig(max_length=100))`.
  3. Strip legacy attributes from a loaded config: iterate `model.config._get_generation_parameters()` and delete/pop those keys from `model.config`.
  4. If a Hub checkpoint's `config.json` contains generation params, set them in its `generation_config.json` instead.

Example fix

# before
model.config.max_length = 100
model.config.do_sample = True
out = model.generate(**inputs)  # ValueError: legacy config-controlled generation

# after
model.generation_config.max_length = 100
model.generation_config.do_sample = True
out = model.generate(**inputs)
Defensive patterns

Strategy: validation

Validate before calling

legacy = model.config._get_generation_parameters()
if legacy:
    for k in legacy:
        setattr(model.generation_config, k, getattr(model.config, k))
        setattr(model.config, k, None)

Prevention

When it happens

Trigger: `model.config.max_length = 100` (or `do_sample`, `num_beams`, `temperature`, etc. on `model.config`) then `model.generate(**inputs)` without an explicit `generation_config` argument.

Common situations: Old tutorials/StackOverflow answers that mutate `model.config`; codebases written for transformers < 4.x behavior; loading checkpoints from before the split that stored generation parameters inside `config.json`; silent breakage after upgrading transformers.

Related errors


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