High severity beginner · Fix: 2-5 min

ValueError

ValueError: Invalid threshold configuration in ToxicLanguage validator

What this error means
The ToxicLanguage validator in Guardrails receives an invalid or out-of-range threshold value that doesn't match the expected numeric format or boundaries.

Stack trace

traceback
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
QUICK FIX
Change ToxicLanguage(threshold=1.5) to ToxicLanguage(threshold=0.7): ensure the value is a float between 0.0 and 1.0.

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

1

Threshold value is outside the valid range (0.0 to 1.0, e.g., passed 1.5 or -0.2)

✓ Fix

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.

2

Threshold is passed as a string instead of a float (e.g., '0.7' instead of 0.7)

✓ Fix

Convert the threshold to float explicitly: float(os.environ.get('TOXIC_THRESHOLD', '0.7')) or hard-code as 0.7 without quotes.

3

Threshold parameter is None or missing and no default is set

✓ Fix

Provide a default threshold value in ToxicLanguage initialization: ToxicLanguage(threshold=0.5) or set a fallback in your config loader.

4

Using deprecated Guardrails <0.5 Guard pattern or importing ToxicLanguage from wrong module

✓ Fix

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

Broken - triggers the error
python
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}')
Fixed - works correctly
python
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}')
Changed threshold from 1.5 (invalid) to 0.7 (valid), added float() type conversion from environment variables, and added range validation before passing to ToxicLanguage to catch config errors early.

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.

Python 3.9+ · guardrails-ai >=0.5.0 · tested on 0.5.x
Verified 2026-04
Verify ↗

Community Notes

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