dgraph-io/dgraph · error

Malformed option in similar_to: empty key

Error message

Malformed option in similar_to: empty key

What it means

While parsing similar_to options, each key is trimmed (and a trailing colon removed); if the resulting key is an empty string, the option pair is meaningless and the parser rejects it with this error.

Source

Thrown at worker/task.go:2789

// parseSimilarToOptions parses named options passed after similar_to 2 mandatory args (k, vecOrUid)
// The parser encodes these as key/value pairs: ["ef", "64", "distance_threshold", "0.5", ...]
func parseSimilarToOptions(args []string, fc *functionContext) error {
	if len(args) == 0 {
		return nil
	}
	if len(args)%2 != 0 {
		return errors.Errorf("Malformed option in similar_to: expected key:value pairs, got %v", args)
	}
	seen := make(map[string]struct{}, len(args)/2)
	for i := 0; i < len(args); i += 2 {
		k := strings.ToLower(strings.TrimSpace(args[i]))
		v := strings.TrimSpace(args[i+1])
		if strings.HasSuffix(k, ":") {
			k = strings.TrimSuffix(k, ":")
		}
		if len(k) == 0 {
			return errors.Errorf("Malformed option in similar_to: empty key")
		}
		if _, dup := seen[k]; dup {
			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":

View on GitHub (pinned to 759e242be6)

Solutions

  1. Provide a non-empty key name for every option pair (ef, distance_threshold, etc.).
  2. Inspect the rendered query for empty placeholders from string interpolation.
  3. Remove stray commas or lone colons from the option list.

Example fix

// before
q(func: similar_to(a, 10, [0.1,0.2], "", "64"))
// after
q(func: similar_to(a, 10, [0.1,0.2], "ef", "64"))
Defensive patterns

Strategy: validation

Validate before calling

function validateSimilarToKeys(pairs) {
  for (const [k] of pairs) {
    const key = k.toLowerCase().trim().replace(/:$/, "");
    if (!key) throw new Error("similar_to option key cannot be empty");
  }
}

Try / catch

try {
  return await txn.query(query);
} catch (e) {
  if (/empty key/.test(String(e))) {
    // fix interpolation and rebuild options
  }
  throw e;
}

Prevention

When it happens

Trigger: Options like similar_to(a, 10, [v], "", "64") or ":", "0.5" — a key that is empty or only a colon.

Common situations: Interpolation failures leaving an empty key in generated queries (e.g. ${key} empty); stray commas producing empty tokens; copy-paste of option blocks with a dangling colon.

Understand the failure class

Related errors


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