dgraph-io/dgraph · error

Invalid value for 'distance_threshold' in similar_to: %q

Error message

Invalid value for 'distance_threshold' in similar_to: %q

What it means

The 'distance_threshold' similar_to option must parse as a float64 via strconv.ParseFloat. If the value is not a valid float literal (e.g. 'distance_threshold: near', 'distance_threshold: 0,5' with comma decimal separator), this error is returned and the threshold is not stored in fc.vsDistanceThreshold.

Source

Thrown at worker/task.go:2810

			return errors.Errorf("Duplicate key in similar_to options: %q", k)
		}
		seen[k] = struct{}{}

		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

  1. Provide a valid decimal float such as 'distance_threshold: 0.5'.
  2. Convert locale-formatted numbers (comma decimal separator) to dot notation before building the query.
  3. Validate with strconv.ParseFloat client-side before sending.

Example fix

// before
"distance_threshold: 0,75"
// after
"distance_threshold: 0.75"
Defensive patterns

Strategy: validation

Validate before calling

f := "0,75"
if _, err := strconv.ParseFloat(strings.ReplaceAll(f, ",", "."), 64); err != nil {
    return fmt.Errorf("distance_threshold must be a float, got %q", f)
}

Try / catch

if err := runQuery(ctx); err != nil && strings.Contains(err.Error(), "Invalid value for 'distance_threshold'") {
    return fmt.Errorf("distance_threshold must be a decimal float: %w", err)
}

Prevention

When it happens

Trigger: similar_to option string contains 'distance_threshold' with a non-float value: 'distance_threshold: auto', 'distance_threshold: 0.1.2', 'distance_threshold:' (empty), or a locale-formatted number like 'distance_threshold: 0,75'.

Common situations: Locale formatting where the decimal comma was not converted to a dot; string concatenation that produced a malformed number; placeholder text left in a hand-edited query.

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


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/ce0a024eee9c5bb3. Report an issue: GitHub.