dgraph-io/dgraph · error

Expected value of type string. Got : %v

Error message

Expected value of type string. Got : %v

What it means

types.ObjectValue builds an api.Value for the given type ID; for StringID it requires the Go value to be exactly a string. When the interface value holds some other type, this error is returned. It is a strict type check on the in-memory value, with the offending value printed in the message.

Source

Thrown at types/conversion.go:645

		default:
			return cantConvert(fromID, toID)
		}
	default:
		return cantConvert(fromID, toID)
	}
	return nil
}

// ObjectValue converts into api.Value.
func ObjectValue(id TypeID, value interface{}) (*api.Value, error) {
	def := &api.Value{Val: &api.Value_StrVal{StrVal: ""}}
	var ok bool
	// Lets set the object value according to the storage type.
	switch id {
	case StringID:
		var v string
		if v, ok = value.(string); !ok {
			return def, errors.Errorf("Expected value of type string. Got : %v", value)
		}
		return &api.Value{Val: &api.Value_StrVal{StrVal: v}}, nil
	case DefaultID:
		var v string
		if v, ok = value.(string); !ok {
			return def, errors.Errorf("Expected value of type string. Got : %v", value)
		}
		return &api.Value{Val: &api.Value_DefaultVal{DefaultVal: v}}, nil
	case IntID:
		var v int64
		if v, ok = value.(int64); !ok {
			return def, errors.Errorf("Expected value of type int64. Got : %v", value)
		}
		return &api.Value{Val: &api.Value_IntVal{IntVal: v}}, nil
	case FloatID:
		var v float64
		if v, ok = value.(float64); !ok {
			return def, errors.Errorf("Expected value of type float64. Got : %v", value)

View on GitHub (pinned to 759e242be6)

Solutions

  1. Convert the value to string before calling: string(byteSlice) or fmt.Sprintf / strconv where appropriate
  2. Route raw data through types.ParseValue/types.Marshal first so the Val.Value is a proper string
  3. Use the correct type ID matching the actual Go type of the value

Example fix

// before
types.ObjectValue(types.StringID, []byte("hello")) // error
// after
types.ObjectValue(types.StringID, string([]byte("hello")))
Defensive patterns

Strategy: type-guard

Validate before calling

func isString(v interface{}) bool { _, ok := v.(string); return ok }
if isString(value) { types.ObjectValue(types.StringID, value) } else { /* convert first */ }

Type guard

func asString(v interface{}) (string, bool) { s, ok := v.(string); return s, ok }

Try / catch

av, err := types.ObjectValue(types.StringID, value)
if err != nil && strings.Contains(err.Error(), "Expected value of type string") {
    // coerce: value = fmt.Sprintf("%v", value) and retry
}

Prevention

When it happens

Trigger: ObjectValue(types.StringID, value) where value is []byte, int, fmt.Stringer, or any non-string — typically values built from parsing paths that produce byte slices instead of strings.

Common situations: Converting NQuad updates whose object value came from a []byte source; wiring values from JSON decoding that produced non-string types into string predicates; test harnesses building api values by hand.

Related errors


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