🚨Refactor: Update text2text generation pipelines to use max_new_tokens…
Author: lilin-1
Pull request: yes

---
name: Pull Request
---
about: Create a pull request to contribute to 🤗 Transformers
---
title: "Refactor: Update text2text generation pipelines to use max_new_tokens and resolve max_length warning"
---
labels: bug, summarization
---
assignees: ''
---

## Related Issue

Closes #40768

## Summary

This PR fixes an incorrect warning in `SummarizationPipeline` where a `max_length`-based message is emitted even when `max_new_tokens` is explicitly provided and used. The change standardizes the logic and messaging to consistently respect and reference `max_new_tokens` so users receive accurate guidance.

## Reproduction

1. Case A — Provide `max_new_tokens` (recommended):

    ```python
    from transformers import pipeline, AutoTokenizer

    summarizer = pipeline("summarization", model="Falconsai/text_summarization", device_map="auto")

    text = (
        "This is a long article about the history of artificial intelligence, covering its origins, "
        "key milestones, and future prospects. It delves into various subfields like machine learning, "
        "natural language processing, and computer vision, highlighting their impact on modern technology "
        "and society. The article also discusses the ethical considerations and challenges associated with "
        "the rapid advancement of AI."
    )

    # Optional: show tokenized input length
    tokenizer = AutoTokenizer.from_pretrained("Falconsai/text_summarization")
    input_length = len(tokenizer.encode(text, add_special_tokens=False))
    print(f"Input text length (after tokenization): {input_length}")

    outputs = summarizer([text], max_new_tokens=64, min_length=10, do_sample=False)
    print(outputs)
    ```

    You may observe a warning that references `max_length` even though `max_new_tokens` is set.

2. Case B — Provide only `max_length` when a default `max_new_tokens` exists:

    ```python
    outputs = summarizer([text], max_length=200, do_sample=False)
    ```

    In pipelines/configurations where a default `max_new_tokens` is present (e.g., via `generation_config` or the pipeline default), generation is still constrained by that `max_new_tokens` value, effectively overriding `max_length`. 

## Observed Behavior

- A warning suggests tuning `max_length` with a value derived from the input length (for example, showing `max_length=200`) although generation is in fact constrained by `max_new_tokens` (e.g., 64). The generated summary respects `max_new_tokens`, but the warning is confusing and misleading.
- When only `max_length` is provided but a default `max_new_tokens` exists, generation remains bounded by that `max_new_tokens` default

## Background and Rationale

- The input-checking code dates back to an older generation logic where `max_length` was commonly the controlling parameter. Over time, generation APIs encouraged users to prefer `max_new_tokens`. However, the checker can still read model/generation defaults (for example, a default `max_length` from `generation_config`) even when runtime priority is already overridden by a user-specified `max_new_tokens`. This causes the checker to infer an outdated `max_length` and emit a misleading warning.
- In current generation behavior, when both `max_length` and `max_new_tokens` are provided, `max_new_tokens` takes precedence and effectively constrains the output length. Setting `max_length` alongside `max_new_tokens` is therefore not meaningful and may trigger additional prompts/warnings elsewhere. This is why the check is updated to reflect that precedence and avoid encouraging users to tune `max_length` when `max_new_tokens` is present.
- Additionally, in pipelines/configs where a default `max_new_tokens` exists, providing only `max_length` still leaves the effective generation limit governed by `max_new_tokens` (default). 

## Implementation Details

- **Update `check_inputs` methods in `SummarizationPipeline`, `TranslationPipeline`, and other relevant text2text generation pipelines**: 
    - These methods will be modified to accurately determine the effective `max_new_tokens` value, taking into account user-provided arguments, model defaults, and generation configuration. 
    - The warning logic will be adjusted to:
        - Only reference `max_new_tokens` in warnings related to output length constraints.
        - Avoid emitting misleading warnings based on outdated `max_length` values from model defaults when `max_new_tokens` is the actual controlling parameter.

## Impact and Compatibility

- No breaking changes. Behavior of generation is unchanged; only the warning logic and message are corrected to match `max_new_tokens`.
- Backwards-compatible with existing scripts; users will see clearer, more relevant warnings.

## Tests and Quality

- Added/updated tests to cover:
  - Precedence and correctness when `max_new_tokens` is provided.
  - Warning text alignment with `max_new_tokens`.
  - The case where only `max_length` is provided but a default `max_new_tokens` exists.
- Local quality checks executed per contributor guide:
  - Style/formatting: `make fixup` (or `make style`) ran cleanly.
  - Lint/quality: `make quality` ran without issues on the modified files.

## Notes for Reviewers

This is submitted by a beginner contributor. I would greatly appreciate feedback and a response, whether the PR is accepted or not. If you decide not to merge, please share the reasons so I can learn and improve future contributions. Thank you for your time and guidance!

## PR Checklist

Please verify the following before submitting the PR:

- [x] Pull Request title summarizes the contribution.
- [x] Pull Request description links the associated Issue (e.g., `Closes #40768`).
- [x] Existing tests pass locally.
- [x] Added or updated tests where appropriate.
- [x] All public methods have informative docstrings.
- [x] No large non-text assets (images/videos) are added to the repository.

Comments:
Thanks for your detailed review and valuable feedback,  @Rocketknight1  ! I’ve made the changes as suggested:

Removed redundant docstring explanations, keeping only basic parameter definitions
Deleted all "updated"-related comments to keep code comments concise
Fully removed the input length check logic based on max_new_tokens

The updates have been synced to this PR. Could you please take another look to see if they align with your expectations?

Makes total sense! Should we update with `min_new_tokens` as well instead of `min_length` though, to make it coherent? 🤗

@lilin-1, up to you! If you want to do the `min_new_tokens` change in this PR too, you can, if not I think we can merge this now.

merge this now.

From a quick glance, it makes sense to me :)

(I'd add a 🚨 to the PR header, so we know this is a little breaking)

Merging!

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_40928). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.