Changing temperature in model.generate() does not change the output
Your error
The following generation flags are not valid and may be ignored: ['temperature']. Set `TRANSFORMERS_VERBOSITY=info` for more details.2 more lines it can showFix it fast
Most common cause, 1 of 2do_sample is not True, so generate() uses greedy search
Turn sampling on in the same generate() call that sets temperature.
outputs = model.generate(
**inputs,
do_sample=True,
temperature=0.7,
max_new_tokens=100,
)
# or once for all calls:
model.generation_config.do_sample = TrueShort answer
In Hugging Face Transformers, temperature only acts in sampling mode. Without do_sample=True, generate() runs greedy decoding, ignores temperature and logs that the generation flags may be ignored. Pass do_sample=True with a positive temperature; for fixed output, keep do_sample=False and drop temperature, since 0 raises ValueError. Applies to Transformers 5.23.
Which cause do you have?
Print the generation config the loaded model will actually use. If do_sample is missing or False, every temperature you pass is skipped (cause 1). If it is True and you pass 0, you get the ValueError of cause 2.
# add right after from_pretrained() in your script
print(model.generation_config)Then find the line you see in this list and open its fix:
- 1do_sample is not True, so generate() uses greedy search
Every run returns the same text whatever temperature you pass, and the log lists temperature among flags that may be ignored - 2temperature=0 is passed together with do_sample=True
generate() stops with ValueError that temperature (=0.0) has to be a strictly positive float
What does the error look like?
The following generation flags are not valid and may be ignored: ['temperature']. Set `TRANSFORMERS_VERBOSITY=info` for more details.`do_sample` is not set to `True`. However, `temperature` is set to `0.7` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.ValueError: `temperature` (=0.0) has to be a strictly positive float, otherwise your next token scores will be invalid. If you're looking for greedy decoding strategies, set `do_sample=False`.
Cause 1 of 2
do_sample is not True, so generate() uses greedy search
You seeEvery run returns the same text whatever temperature you pass, and the log lists temperature among flags that may be ignored
Check
The temperature setting is ignored here because Transformers adds its temperature step only to sampling strategies. Greedy search is the default when neither your call nor the model's generation_config.json sets do_sample to True, and greedy always takes the most likely next token.
TRANSFORMERS_VERBOSITY=info python generate.py 2>&1 | grep -i do_sampleFix
Turn sampling on in the same generate() call that sets temperature. You can also set it once on model.generation_config so every call samples.
outputs = model.generate(
**inputs,
do_sample=True,
temperature=0.7,
max_new_tokens=100,
)
# or once for all calls:
model.generation_config.do_sample = TrueVerify
Two runs with temperature=1.2 now give different text, and the warning about ignored flags is gone.
python generate.py && python generate.pyConfirmed by raw.githubusercontent.com
Cause 2 of 2
temperature=0 is passed together with do_sample=True
You seegenerate() stops with ValueError that temperature (=0.0) has to be a strictly positive float
Check
The ValueError about a strictly positive float comes from TemperatureLogitsWarper, which divides the scores by the temperature and refuses zero. Setting 0 to mean 'no randomness', as some hosted APIs allow, does not work in Transformers.
grep -n "temperature" generate.pyFix
For repeatable output, use greedy decoding: do_sample=False and no temperature. For output that is only a little random, keep sampling and use a small positive float.
# repeatable output
outputs = model.generate(**inputs, do_sample=False, max_new_tokens=100)
# slightly random output
outputs = model.generate(**inputs, do_sample=True, temperature=0.2, max_new_tokens=100)Verify
generate() returns tokens without the ValueError, and the greedy version gives the same text on every run.
python generate.pyConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Keep do_sample, temperature, top_p and max_new_tokens together in one GenerationConfig object in your code, not spread across calls.
- Run once with TRANSFORMERS_VERBOSITY=info after changing generation settings, so ignored flags show up before release.
- Read the model's generation_config.json when you switch checkpoints; each model can ship its own sampling defaults.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.