dgraph-io/dgraph · error

unexpected type for val for attr: %s while converting to nqu

Error message

unexpected type for val for attr: %s while converting to nquad

What it means

During JSON-to-NQuad conversion, handleBasicType in chunker/json_parser.go:259 handles scalar attribute values (bool, string, numeric, star). When a value's Go type matches none of the supported switch cases, the parser cannot represent it as an NQuad object value and returns this error naming the predicate. It means the JSON document contained a scalar type the RDF mapper does not support for an attribute.

Source

Thrown at chunker/json_parser.go:259

		// password value (types.PasswordID) - Issue#2623
		nq.ObjectValue = &api.Value{Val: &api.Value_StrVal{StrVal: v}}

	case float64:
		if v == 0 && op == DeleteNquads {
			nq.ObjectValue = &api.Value{Val: &api.Value_DefaultVal{DefaultVal: x.Star}}
			return nil
		}
		nq.ObjectValue = &api.Value{Val: &api.Value_DoubleVal{DoubleVal: v}}

	case bool:
		if !v && op == DeleteNquads {
			nq.ObjectValue = &api.Value{Val: &api.Value_DefaultVal{DefaultVal: x.Star}}
			return nil
		}
		nq.ObjectValue = &api.Value{Val: &api.Value_BoolVal{BoolVal: v}}

	default:
		return fmt.Errorf("unexpected type for val for attr: %s while converting to nquad", k)
	}
	return nil

}

func (buf *NQuadBuffer) checkForDeletion(mr mapResponse, m map[string]interface{}, op int) {
	// Since uid is the only key, this must be S * * deletion.
	if op == DeleteNquads && len(mr.uid) > 0 && len(m) == 1 && len(mr.rawFacets) == 0 {
		buf.Push(&api.NQuad{
			Subject:     mr.uid,
			Predicate:   x.Star,
			Namespace:   mr.namespace,
			ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: x.Star}},
		})
	}
}

func handleGeoType(val map[string]interface{}, nq *api.NQuad) (bool, error) {

View on GitHub (pinned to 759e242be6)

Solutions

  1. Log/inspect the offending attribute's value type in the JSON document and coerce it to a supported scalar (string, bool, number) before parsing.
  2. Replace unsupported values like null with an explicit value or drop the predicate from the document.
  3. If it must be preserved, wrap the value in a structure the parser supports (map for nested object, array for list) instead of a bare scalar.

Example fix

// before
{"age": null}
// after
{"age": "0"}
Defensive patterns

Strategy: validation

Validate before calling

func isSupportedScalar(v interface{}) bool {
	switch v.(type) {
	case string, bool, int, int64, float64, json.Number:
		return v != nil
	}
	return false
}
// reject any key whose value is not isSupportedScalar before parsing

Type guard

func supportedScalar(v interface{}) (interface{}, bool) {
	if v == nil {
		return nil, false
	}
	switch v.(type) {
	case string, bool, float64, int64, json.Number:
		return v, true
	}
	return nil, false
}

Try / catch

if err := buf.ParseJSON(b, op); err != nil && strings.Contains(err.Error(), "unexpected type for val for attr") {
	// sanitize the document (replace nulls/odd scalars) and retry once
}

Prevention

When it happens

Trigger: Calling NQuadBuffer.ParseJSON or FastParseJSON with a JSON map whose attribute value is a basic Go type outside the handled set (e.g. a raw nil, or a JSON number decoded into an unexpected numeric shape) so the type switch in handleBasicType falls to the default branch.

Common situations: Feeding documents produced by another serializer that emits unusual scalars (e.g. nulls or high-precision numbers), or a schema change where a predicate that used to hold a string now receives an unsupported scalar.

Related errors


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