# How to set 'max\_length' properly when using pipeline?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-set-max-length-properly-when-using-pipeline%2F125714%3E%5C
**Category:** 🤗Transformers\
**Created:** [November 18, 2024, 4:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-set-max-length-properly-when-using-pipeline%2F125714 "2024-11-18T16:06:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiaweihuang](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@jiaweihuang](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjiaweihuang)%5C
**Post date:** [November 18, 2024, 4:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-set-max-length-properly-when-using-pipeline%2F125714%2F1 "2024-11-18T16:06:51Z")

</div>

I try to use pipeline, and want to set the maximal length for both tokenizer and the generation process. However, if I try:

```auto

prompt = 'What is the answer of 1 + 1?'
pipe = pipeline(
            "text-generation",
            tokenizer=tokenizer,
            model=model,
            do_sample=True,
            truncation=True,
            padding='max_length',
            num_return_sequences=2,
            temperature=1.0,
            num_beams=1,
            max_length=1024,
        )
messages = [
    {"role": "user", "content": prompt},
]
ret = pipe(messages)

```

I will get an error message:

```auto
ValueError: Input length of input_ids is 1024, but `max_length` is set to 1024. This can lead to unexpected behavior. You should consider increasing `max_length` or, better yet, setting `max_new_tokens`.

```

Therefore, I set `max_new_tokens` according to the error message as follow:

```auto
prompt = 'What is the answer of 1 + 1?'
pipe = pipeline(
            "text-generation",
            tokenizer=tokenizer,
            model=model,
            do_sample=True,
            truncation=True,
            padding='max_length',
            num_return_sequences=2,
            temperature=1.0,
            num_beams=1,
            max_length=1024,
            max_new_tokens=512,
        )
messages = [
    {"role": "user", "content": prompt},
]
ret = pipe(messages)

```

However, I will get a warning all the time:

```auto
Both `max_new_tokens` (=512) and `max_length`(=1024) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)`.

```

I wonder what would be the correct way to set `max_length` parameter for both tokenizer and the momdel.generate(…)? Should I use alternative arguments in pipeline?

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [November 18, 2024, 4:12pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-set-max-length-properly-when-using-pipeline%2F125714%2F2 "2024-11-18T16:12:43Z")

</div>

If you are not familiar with the LLM options, it works better to use the defaults. It will work better if you use them when you really need them.  
It is faster and more reliable to truncate the output string to 1024 characters in Python than to limit the number of characters in the LLM output.

---

<div class="post-metadata">

**Author:** ![jiaweihuang](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@jiaweihuang](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjiaweihuang)%5C
**Post date:** [November 18, 2024, 4:37pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-set-max-length-properly-when-using-pipeline%2F125714%2F3 "2024-11-18T16:37:29Z")

</div>

If I understand correctly, you do not recommend to use pipeline?

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [November 18, 2024, 5:07pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-set-max-length-properly-when-using-pipeline%2F125714%2F4 "2024-11-18T17:07:59Z")

</div>

No, I recommend it because the pipeline is a value set that can do a lot of work on your behalf, unless you need to make detailed adjustments.  
However, adjusting the number of output characters using max\_length doesn’t work very well, so it’s faster to output more and then trim it using Python string processing, rather than specifying it.

---

<div class="post-metadata">

**Author:** ![not-lain](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/not-lain/32/59092_2.png) [@not-lain](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnot-lain)%5C
**Post date:** [November 18, 2024, 11:23pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-set-max-length-properly-when-using-pipeline%2F125714%2F5 "2024-11-18T23:23:01Z")

</div>

@jiaweihuang

```diff
prompt = 'What is the answer of 1 + 1?'
pipe = pipeline(
            "text-generation",
            tokenizer=tokenizer,
            model=model,
            do_sample=True,
            truncation=True,
            padding='max_length',
            num_return_sequences=2,
            temperature=1.0,
            num_beams=1,
            max_length=1024,
            max_new_tokens=512,
        )
messages = [
    {"role": "user", "content": prompt},
]
-ret = pipe(messages)
+ret = pipe(messages, max_length= 1024)

```

also pretty sure **all of these prameters** are used in the generation and not in the initialization
