ValueError
ValueError: Invalid threshold configuration in ToxicLanguage validator
Stack trace
Traceback (most recent call last):
File "your_script.py", line 45, in <module>
guard = Guard().use(ToxicLanguage(threshold=1.5), on_fail='exception')
File "/usr/local/lib/python3.11/site-packages/guardrails/hub/validators/toxic_language.py", line 112, in __init__
raise ValueError(f'Threshold must be a float between 0.0 and 1.0, got {threshold}')
ValueError: Threshold must be a float between 0.0 and 1.0, got 1.5 Why it happens
The ToxicLanguage validator from Guardrails Hub expects a threshold parameter between 0.0 (catch all toxic language) and 1.0 (catch nothing), representing a confidence score. When you pass a value outside this range, pass a string instead of a float, or omit the parameter without providing a default, the validator initialization fails. This is a validation-time error that prevents the Guard from being created at all.
Detection
Test your Guard initialization in a try/except block immediately after declaring it, or use IDE type hints to catch threshold type mismatches before runtime. Log the threshold value being passed to identify out-of-range values before deployment.
Causes & fixes
Threshold value is outside the valid range (0.0 to 1.0, e.g., passed 1.5 or -0.2)
Ensure threshold is a float between 0.0 and 1.0. Example: ToxicLanguage(threshold=0.7) for 70% confidence cutoff. Check your configuration file or environment variable for out-of-range values.
Threshold is passed as a string instead of a float (e.g., '0.7' instead of 0.7)
Convert the threshold to float explicitly: float(os.environ.get('TOXIC_THRESHOLD', '0.7')) or hard-code as 0.7 without quotes.
Threshold parameter is None or missing and no default is set
Provide a default threshold value in ToxicLanguage initialization: ToxicLanguage(threshold=0.5) or set a fallback in your config loader.
Using deprecated Guardrails <0.5 Guard pattern or importing ToxicLanguage from wrong module
Update to guardrails-ai>=0.5.0 and use: from guardrails.hub import ToxicLanguage; guard = Guard().use(ToxicLanguage(threshold=0.7), on_fail='exception')
Code: broken vs fixed
import os
from guardrails import Guard
from guardrails.hub import ToxicLanguage
# BROKEN: threshold value 1.5 is out of range (must be 0.0-1.0)
try:
guard = Guard().use(
ToxicLanguage(threshold=1.5), # ← This causes ValueError
on_fail='exception'
)
except ValueError as e:
print(f'Error: {e}') import os
from guardrails import Guard
from guardrails.hub import ToxicLanguage
# FIXED: threshold is now a valid float between 0.0 and 1.0
try:
# Load threshold from environment with type conversion
threshold = float(os.environ.get('TOXIC_THRESHOLD', '0.7'))
# Validate threshold is in range before passing to Guard
if not (0.0 <= threshold <= 1.0):
raise ValueError(f'Threshold must be between 0.0 and 1.0, got {threshold}')
guard = Guard().use(
ToxicLanguage(threshold=threshold), # ← Now valid
on_fail='exception'
)
# Test the guard with sample input
result = guard.validate('Hello, this is a clean message.')
print(f'Validation passed. Result: {result}')
except ValueError as e:
print(f'Configuration error: {e}') Workaround
If you cannot update your configuration immediately, wrap the Guard initialization in a try/except ValueError block and fall back to a hardcoded default threshold of 0.5: `try: guard = Guard().use(ToxicLanguage(threshold=bad_value), ...) except ValueError: guard = Guard().use(ToxicLanguage(threshold=0.5), ...)`. Then log the error for later investigation.
Prevention
Store threshold configuration in a typed config file (YAML/JSON) validated with Pydantic before passing to Guardrails. Example: `class GuardrailsConfig(BaseModel): toxic_threshold: float = Field(ge=0.0, le=1.0); config = GuardrailsConfig(**load_yaml('config.yaml')); guard = Guard().use(ToxicLanguage(threshold=config.toxic_threshold), ...)`. This ensures validation happens at config load time, not Guard initialization time.