High severity beginner · Fix: 2-5 min

ValueError

llamacpp.LlamaCppError.ValueError

What this error means
llama.cpp throws a ValueError when the temperature parameter for sampling is outside the valid range (usually 0.0 to 1.0).

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    response = model.generate(prompt, temperature=1.5)  # Invalid temperature
  File "llamacpp.py", line 88, in generate
    raise ValueError('Temperature must be between 0.0 and 1.0')
ValueError: Temperature must be between 0.0 and 1.0
QUICK FIX
Clamp or validate temperature values to be between 0.0 and 1.0 before passing them to llama.cpp functions.

Why it happens

llama.cpp enforces that the temperature parameter controlling randomness in sampling must be within a valid range, typically 0.0 to 1.0. Passing a value outside this range causes the library to raise a ValueError to prevent undefined or unstable model behavior.

Detection

Validate temperature parameters before calling llama.cpp methods by asserting the value is within the allowed range or catch ValueError exceptions and log the invalid parameter for debugging.

Causes & fixes

1

Temperature parameter passed is greater than 1.0 or less than 0.0

✓ Fix

Ensure the temperature argument is a float between 0.0 and 1.0 inclusive before passing it to llama.cpp generate or sampling functions.

2

Temperature parameter is passed as a string or incorrect type causing implicit conversion errors

✓ Fix

Explicitly convert or validate the temperature parameter as a float before passing it to llama.cpp to avoid type errors.

3

Default temperature value in code is set outside valid range due to a typo or misconfiguration

✓ Fix

Review and correct default temperature values in configuration or code to be within the valid range 0.0 to 1.0.

Code: broken vs fixed

Broken - triggers the error
python
from llamacpp import Llama

model = Llama(model_path="./model.bin")
response = model.generate("Hello world", temperature=1.5)  # This line causes ValueError
print(response)
Fixed - works correctly
python
import os
from llamacpp import Llama

model = Llama(model_path=os.environ["LLAMA_MODEL_PATH"])
# Fixed: temperature clamped to valid range 0.0 to 1.0
temperature = 0.7
response = model.generate("Hello world", temperature=temperature)
print(response)  # Works without error
Added explicit temperature value within valid range (0.0 to 1.0) to prevent llama.cpp from raising ValueError on invalid sampling parameter.

Workaround

Wrap llama.cpp generate calls in try/except ValueError and if caught, reset temperature to a safe default like 0.7 and retry the call.

Prevention

Implement input validation for all sampling parameters including temperature before calling llama.cpp APIs, and use configuration management to enforce valid defaults.

Python 3.9+ · llamacpp >=0.1.0 · tested on 0.2.0
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.