dgraph-io/dgraph · error
Value for 'distance_threshold' must be non-negative, got: %v
Error message
Value for 'distance_threshold' must be non-negative, got: %v
What it means
A parsed 'distance_threshold' must be non-negative because it filters vector-search results by distance, and negative distances do not exist. When the value parses as a float but is < 0, this error is returned.
Source
Thrown at worker/task.go:2813
v = strings.Trim(v, "\"'")
switch k {
case "ef":
n, perr := strconv.ParseInt(v, 10, 32)
if perr != nil {
return errors.Errorf("Invalid value for 'ef' in similar_to: %q", v)
}
if n <= 0 {
return errors.Errorf("Value for 'ef' must be positive, got: %d", n)
}
fc.vsEfOverride = int(n)
case "distance_threshold":
f, perr := strconv.ParseFloat(v, 64)
if perr != nil {
return errors.Errorf("Invalid value for 'distance_threshold' in similar_to: %q", v)
}
if f < 0 {
return errors.Errorf("Value for 'distance_threshold' must be non-negative, got: %v", f)
}
fc.vsDistanceThreshold = new(float64)
*fc.vsDistanceThreshold = f
default:
return errors.Errorf("Unknown option in similar_to: %q", k)
}
}
return nil
}
View on GitHub (pinned to 759e242be6)
Solutions
- Set a non-negative threshold, e.g. 'distance_threshold: 0.5'; 0 is allowed.
- Omit the option entirely instead of using a negative sentinel for 'unset'.
- Clamp the computed value to >= 0 in your query builder.
Example fix
// before
opts += "; distance_threshold: " + fmt.Sprintf("%v", thr) // thr = -1 sentinel
// after
if thr >= 0 { opts += "; distance_threshold: " + fmt.Sprintf("%v", thr) } Defensive patterns
Strategy: validation
Validate before calling
if thr < 0 {
return fmt.Errorf("distance_threshold must be >= 0, got %v", thr)
}
opts := "; distance_threshold: " + strconv.FormatFloat(thr, 'f', -1, 64) Try / catch
if err := runQuery(ctx); err != nil && strings.Contains(err.Error(), "'distance_threshold' must be non-negative") {
return retryWithoutThreshold(ctx)
} Prevention
- Omit the option entirely instead of using negative sentinel values for 'unset'.
- Clamp thresholds to >= 0 in query builders.
- Document that 0 is a valid threshold so users don't reach for -1.
When it happens
Trigger: similar_to option string like 'distance_threshold: -0.5' or 'distance_threshold: -1'. The float parses fine but fails the f < 0 check.
Common situations: Sign errors when computing the threshold from a formula; users confusing threshold with a 'score' where lower is better and attempting negative values; template defaults of -1 used as sentinel 'unset' that get serialized.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Value for 'ef' must be positive, got: %d
- Invalid value for 'distance_threshold' in similar_to: %q
- No value found
- item.Val
- Invalid compare function %q
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/b9dc158af4d71ca4.
Report an issue: GitHub.