dgraph-io/dgraph · error

Fail to convert from api.Value to types.Val

Error message

Fail to convert from api.Value to types.Val

What it means

This error wraps a failure from types.Convert when an internal api.Value (protobuf wire value) cannot be converted into a typed types.Val using the value's type id. It occurs during result formatting or grouped-variable filling after fetching a value from the posting list. Typically the stored type id does not match the actual byte payload.

Source

Thrown at query/aggregator.go:54

func isTernary(f string) bool {
	return f == "cond"
}

func isBinary(f string) bool {
	return f == "+" || f == "*" || f == "-" || f == "/" || f == "%" ||
		f == "max" || f == "min" || f == "logbase" || f == "pow" ||
		f == "dot"
}

func convertTo(from *pb.TaskValue) (types.Val, error) {
	vh, _ := getValue(from)
	if bytes.Equal(from.Val, x.Nilbyte) {
		return vh, ErrEmptyVal
	}
	va, err := types.Convert(vh, vh.Tid)
	if err != nil {
		return vh, errors.Wrapf(err, "Fail to convert from api.Value to types.Val")
	}
	return va, err
}

func compareValues(ag string, va, vb types.Val) (bool, error) {
	if !isBinaryBoolean(ag) {
		x.Fatalf("Function %v is not binary boolean", ag)
	}

	_, err := types.Less(va, vb)
	if err != nil {
		//Try to convert values.
		switch {
		case va.Tid == types.IntID:
			va.Tid = types.FloatID
			va.Value = float64(va.Value.(int64))
		case vb.Tid == types.IntID:
			vb.Tid = types.FloatID

View on GitHub (pinned to 759e242be6)

Solutions

  1. Align the schema with the actual stored data, or migrate/rewrite the offending values
  2. Find the offending predicate and fix the stored value (delete and re-set with correct type)
  3. Validate data before bulk load so stored types match the declared schema
  4. Rebuild the index for the predicate after a schema type change

Example fix

// before: schema changed to int but old string data remains
// after: migrate values before querying
for each node of predicate {
  v := readRaw(node)
  if !validForSchema(v) { rewrite(node, convert(v)) }
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate stored value matches schema type before querying
val, _ := node.Value(pred)
if val != nil {
    if _, err := types.Convert(val, schema.Type(pred)); err != nil {
        // fix or migrate the value before running conversions
    }
}

Type guard

func isConvertible(v *api.Value) bool {
    if v == nil || bytes.Equal(v.Val, x.Nilbyte) { return false }
    _, err := types.Convert(v, v.Tid)
    return err == nil
}

Try / catch

va, err := convertTo(from)
if err != nil && strings.Contains(err.Error(), "Fail to convert from api.Value") {
    return vh, fmt.Errorf("stored value for predicate %q does not match schema type: %w", pred, err)
}

Prevention

When it happens

Trigger: convertTo is called (by formResult or fillGroupedVars) on a value fetched via getValue(from); types.Convert(vh, vh.Tid) fails, e.g. schema says the predicate is an int but the stored bytes are a string, or the stored data predates a schema change.

Common situations: Changing a predicate's type in schema without re-indexing/migrating data; corrupt or partially written values; bulk-loading data whose types disagree with the schema.

Related errors


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