dgraph-io/dgraph · error

Unsupported floating point number in float field

Error message

Unsupported floating point number in float field

What it means

Dgraph serializes query results to JSON, and JSON has no representation for IEEE-754 special values. When valToBytes encounters a float field whose value is NaN, +Inf, -Inf, or is not actually a float64, it refuses to emit invalid JSON and returns this error.

Source

Thrown at query/outputnode.go:670

	case types.IntID:
		// In types.Convert(), we always convert to int64 for IntID type. fmt.Sprintf is slow
		// and hence we are using strconv.FormatInt() here. Since int64 and int are most common int
		// types we are using FormatInt for those.
		switch num := v.Value.(type) {
		case int64:
			return []byte(strconv.FormatInt(num, 10)), nil
		case int:
			return []byte(strconv.FormatInt(int64(num), 10)), nil
		default:
			return []byte(fmt.Sprintf("%d", v.Value)), nil
		}
	case types.FloatID:
		f, fOk := v.Value.(float64)

		// +Inf, -Inf and NaN are not representable in JSON.
		// Please see https://golang.org/src/encoding/json/encode.go?s=6458:6501#L573
		if !fOk || math.IsInf(f, 0) || math.IsNaN(f) {
			return nil, errors.New("Unsupported floating point number in float field")
		}

		return []byte(fmt.Sprintf("%g", f)), nil
	case types.BoolID:
		if v.Value.(bool) {
			return boolTrue, nil
		}
		return boolFalse, nil
	case types.DateTimeID:
		t := v.Value.(time.Time)
		return marshalTimeJson(t)
	case types.GeoID:
		return geojson.Marshal(v.Value.(geom.T))
	case types.BigFloatID:
		b := v.Value.(big.Float)
		return b.MarshalText()
	case types.UidID:
		return []byte(fmt.Sprintf("\"%#x\"", v.Value)), nil

View on GitHub (pinned to 759e242be6)

Solutions

  1. Fix the query math to avoid NaN/Inf: guard math.log(0), math.sqrt(-1), and division by zero with cond or math.isNaN-style checks in the query
  2. Sanitize data before mutation: reject or clamp NaN/Inf float values at write time
  3. Check the schema: ensure the predicate is declared float and the value written is a JSON number, not string or object
  4. If the value came from a stored document, re-mutate the offending node with a valid finite float

Example fix

// before (query)
root as var(func: eq(score, math.log(0)))
// after
scores as var(func: has(score))
q(func: uid(scores)) @filter(gt(score, 0.0)) {
  safeScore as score
}
Defensive patterns

Strategy: validation

Validate before calling

const v = Number(value);
if (Number.isNaN(v) || !Number.isFinite(v)) throw new Error(`float field must be finite, got ${value}`);

Type guard

function isFiniteNumber(v: unknown): v is number {
  return typeof v === 'number' && Number.isFinite(v);
}

Try / catch

try {
  const res = await dgraph.query(q);
} catch (e) {
  if (String(e).includes('Unsupported floating point number in float field')) {
    // sanitize offending math and retry without NaN-producing expressions
  }
  throw e;
}

Prevention

When it happens

Trigger: A predicate with float schema stores or computes NaN/Inf — e.g. result of math operations like log(-1), division by zero on floats, or a value unmarshalled into a non-float64 Go type before reaching valToBytes via getObjectVal/AddListValue.

Common situations: Math-heavy queries (math.sqrt of negatives, math.log(0)) in Dgraph; client writes NaN via JSON (Go encoding/json normally rejects it, but custom mutation paths may not); corrupted or wrongly-typed stored values after schema changes.

Related errors


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