{"record":{"id":"b9dc158af4d71ca4","repo":"dgraph-io/dgraph","slug":"value-for-distance-threshold-must-be-non-negativ","errorCode":null,"errorMessage":"Value for 'distance_threshold' must be non-negative, got: %v","messagePattern":"Value for 'distance_threshold' must be non-negative, got: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"worker/task.go","lineNumber":2813,"sourceCode":"\n\t\tv = strings.Trim(v, \"\\\"'\")\n\t\tswitch k {\n\t\tcase \"ef\":\n\t\t\tn, perr := strconv.ParseInt(v, 10, 32)\n\t\t\tif perr != nil {\n\t\t\t\treturn errors.Errorf(\"Invalid value for 'ef' in similar_to: %q\", v)\n\t\t\t}\n\t\t\tif n <= 0 {\n\t\t\t\treturn errors.Errorf(\"Value for 'ef' must be positive, got: %d\", n)\n\t\t\t}\n\t\t\tfc.vsEfOverride = int(n)\n\t\tcase \"distance_threshold\":\n\t\t\tf, perr := strconv.ParseFloat(v, 64)\n\t\t\tif perr != nil {\n\t\t\t\treturn errors.Errorf(\"Invalid value for 'distance_threshold' in similar_to: %q\", v)\n\t\t\t}\n\t\t\tif f < 0 {\n\t\t\t\treturn errors.Errorf(\"Value for 'distance_threshold' must be non-negative, got: %v\", f)\n\t\t\t}\n\t\t\tfc.vsDistanceThreshold = new(float64)\n\t\t\t*fc.vsDistanceThreshold = f\n\t\tdefault:\n\t\t\treturn errors.Errorf(\"Unknown option in similar_to: %q\", k)\n\t\t}\n\t}\n\treturn nil\n}\n","sourceCodeStart":2795,"sourceCodeEnd":2823,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/worker/task.go#L2795-L2823","documentation":"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.","triggerScenarios":"similar_to option string like 'distance_threshold: -0.5' or 'distance_threshold: -1'. The float parses fine but fails the f < 0 check.","commonSituations":"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.","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."],"exampleFix":"// before\nopts += \"; distance_threshold: \" + fmt.Sprintf(\"%v\", thr) // thr = -1 sentinel\n// after\nif thr >= 0 { opts += \"; distance_threshold: \" + fmt.Sprintf(\"%v\", thr) }","handlingStrategy":"validation","validationCode":"if thr < 0 {\n    return fmt.Errorf(\"distance_threshold must be >= 0, got %v\", thr)\n}\nopts := \"; distance_threshold: \" + strconv.FormatFloat(thr, 'f', -1, 64)","typeGuard":null,"tryCatchPattern":"if err := runQuery(ctx); err != nil && strings.Contains(err.Error(), \"'distance_threshold' must be non-negative\") {\n    return retryWithoutThreshold(ctx)\n}","preventionTips":["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."],"tags":["dql","vector-search","validation"],"backgroundTag":"invalid-enum-value","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}