# Ensure the sentence is complete during generation

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fensure-the-sentence-is-complete-during-generation%2F4489%3E%5C
**Category:** 🤗Transformers\
**Created:** [March 17, 2021, 3:45pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fensure-the-sentence-is-complete-during-generation%2F4489 "2021-03-17T15:45:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![prajjwal1](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/prajjwal1/32/2246_2.png) [@prajjwal1](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fprajjwal1)%5C
**Post date:** [March 17, 2021, 3:45pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fensure-the-sentence-is-complete-during-generation%2F4489%2F1 "2021-03-17T15:45:22Z")

</div>

During generation, I’m using the constraint of `max_length` to stop if longer sequences are not required. However, I do not want the generation to stop if the sentence is not complete. Is there any reliable way to stop after one sentence has been generated ?

---

<div class="post-metadata">

**Author:** ![100worte](https://avatars.discourse-cdn.com/v4/letter/1/7bcc69/32.png) [@100worte](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2F100worte)%5C
**Post date:** [March 17, 2021, 5:38pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fensure-the-sentence-is-complete-during-generation%2F4489%2F2 "2021-03-17T17:38:49Z")

</div>

AFAIK, the generation should stop once it generates an end-of-sentence token, if you _don’t_ specify `max_length`. You can use [StoppingCriteria](https://huggingface.co/transformers/internal/generation_utils.html#stoppingcriteria) (which you implicitly do by setting `max_length`) to construct arbitrary constraints on when to stop your generation.

---

<div class="post-metadata">

**Author:** ![prajjwal1](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/prajjwal1/32/2246_2.png) [@prajjwal1](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fprajjwal1)%5C
**Post date:** [March 18, 2021, 2:52am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fensure-the-sentence-is-complete-during-generation%2F4489%2F3 "2021-03-18T02:52:28Z")

</div>

You’re right about EOS token. If I don’t specify `max_length` parameter, then the model can generate a long text which may stop making sense halfway through or deviates from the context provided. I want the generation to be a bit more natural. Can you please share an example of how `StoppingCriteria` would work ? Didn’t find the usage example in docs.

---

<div class="post-metadata">

**Author:** ![100worte](https://avatars.discourse-cdn.com/v4/letter/1/7bcc69/32.png) [@100worte](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2F100worte)%5C
**Post date:** [March 19, 2021, 7:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fensure-the-sentence-is-complete-during-generation%2F4489%2F4 "2021-03-19T07:41:34Z")

</div>

You can find some implementations [here](https://github.com/huggingface/transformers/blob/master/src/transformers/generation_stopping_criteria.py). And you can search for “stopping\_criteria” in [generation\_utils.py](https://github.com/huggingface/transformers/blob/master/src/transformers/generation_utils.py) to understand the usage.

---

<div class="post-metadata">

**Author:** ![jxm](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jxm/32/29614_2.png) [@jxm](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjxm)%5C
**Post date:** [April 18, 2023, 7:20pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fensure-the-sentence-is-complete-during-generation%2F4489%2F6 "2023-04-18T19:20:12Z")

</div>

I was struggling with this issue today. The easiest way (instead of providing a custom stopping criteria) is to set both [min\_length and max\_length](https://huggingface.co/docs/transformers/main_classes/text_generation#transformers.GenerationConfig) parameters to the same value. This ensures generations of exactly a given length, no shorter or longer.

---

<div class="post-metadata">

**Author:** ![omgwenxx](https://avatars.discourse-cdn.com/v4/letter/o/4af34b/32.png) [@omgwenxx](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fomgwenxx)%5C
**Post date:** [December 19, 2024, 10:19am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fensure-the-sentence-is-complete-during-generation%2F4489%2F7 "2024-12-19T10:19:01Z")

</div>

Did it lead to the sentences naturally ending e.g. with a “.” or just abruptly finishing?  
In my case, I set max\_length to 100 and sentences end abruptly.

---

<div class="post-metadata">

**Author:** ![lebe1](https://avatars.discourse-cdn.com/v4/letter/l/4bbf92/32.png) [@lebe1](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flebe1)%5C
**Post date:** [July 28, 2025, 1:24pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fensure-the-sentence-is-complete-during-generation%2F4489%2F8 "2025-07-28T13:24:50Z")

</div>

If anyone still struggles with this problem today as I was just a few minutes ago, I was able to find a solution by setting a system prompt that insists on it.

```auto
# standard hf code above
messages = [
    {"role": "system", "content": "You are a friendly chatbot who always responds in full sentences only, marking the end of each sentence with a dot. Do not end your answer in the middle of a sentence!",},
    {"role": "user", "content": prompt}
]
formatted_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
print("[FORMATTED PROMPT]:", formatted_prompt)

# Tokenize input
input_data = tokenizer(formatted_prompt, return_tensors="pt", padding=True).to(model.device)
# standard hf code below

```
