ValueError
llamacpp.LlamaCppError.ValueError
Stack trace
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 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
Temperature parameter passed is greater than 1.0 or less than 0.0
Ensure the temperature argument is a float between 0.0 and 1.0 inclusive before passing it to llama.cpp generate or sampling functions.
Temperature parameter is passed as a string or incorrect type causing implicit conversion errors
Explicitly convert or validate the temperature parameter as a float before passing it to llama.cpp to avoid type errors.
Default temperature value in code is set outside valid range due to a typo or misconfiguration
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
from llamacpp import Llama
model = Llama(model_path="./model.bin")
response = model.generate("Hello world", temperature=1.5) # This line causes ValueError
print(response) 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 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.