dgraph-io/dgraph · error

Malformed option in similar_to: expected key:value pairs, go

Error message

Malformed option in similar_to: expected key:value pairs, got %v

What it means

The similar_to vector-similarity function accepts extra options as flat key/value pairs (e.g. ef, distance_threshold). parseSimilarToOptions requires an even number of argument tokens; an odd list means a key without its value, so parsing fails.

Source

Thrown at worker/task.go:2779

	}

	if err := posting.MemLayerInstance.IterateDisk(ctx, *iteratorFunc); err != nil {
		return err
	}
	span.AddEvent("handleHasFunction result", trace.WithAttributes(
		attribute.Int("uid_count", len(result.Uids))))
	out.UidMatrix = append(out.UidMatrix, result)
	return nil
}

// 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 {

View on GitHub (pinned to 759e242be6)

Solutions

  1. Ensure every option key is followed by its value: similar_to(a, 10, [v], "ef", "64", "distance_threshold", "0.5").
  2. Count your option tokens — they must be an even number of key:value pairs.
  3. Check the client library/query template isn't dropping the last option value.

Example fix

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

Strategy: validation

Validate before calling

function validateSimilarToOptions(opts) {
  if (!Array.isArray(opts)) return;
  if (opts.length % 2 !== 0)
    throw new Error(`similar_to options must be key:value pairs, got ${opts.length} tokens`);
}

Try / catch

try {
  return await txn.query(query);
} catch (e) {
  if (/Malformed option in similar_to/.test(String(e))) {
    // rebuild the option list as complete key/value pairs
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling similar_to with options like ["ef", "64", "distance_threshold"] — the trailing key has no value, making len(args) odd.

Common situations: Missing the numeric value after an option name in the query string; a template or client library dropping the last argument; copy-paste truncation of the query.

Understand the failure class

Related errors


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