{"record":{"id":"b8957ccbd16d60e0","repo":"dgraph-io/dgraph","slug":"expected-value-of-type-string-got-v","errorCode":null,"errorMessage":"Expected value of type string. Got : %v","messagePattern":"Expected value of type string\\. Got : (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"types/conversion.go","lineNumber":645,"sourceCode":"\t\tdefault:\n\t\t\treturn cantConvert(fromID, toID)\n\t\t}\n\tdefault:\n\t\treturn cantConvert(fromID, toID)\n\t}\n\treturn nil\n}\n\n// ObjectValue converts into api.Value.\nfunc ObjectValue(id TypeID, value interface{}) (*api.Value, error) {\n\tdef := &api.Value{Val: &api.Value_StrVal{StrVal: \"\"}}\n\tvar ok bool\n\t// Lets set the object value according to the storage type.\n\tswitch id {\n\tcase StringID:\n\t\tvar v string\n\t\tif v, ok = value.(string); !ok {\n\t\t\treturn def, errors.Errorf(\"Expected value of type string. Got : %v\", value)\n\t\t}\n\t\treturn &api.Value{Val: &api.Value_StrVal{StrVal: v}}, nil\n\tcase DefaultID:\n\t\tvar v string\n\t\tif v, ok = value.(string); !ok {\n\t\t\treturn def, errors.Errorf(\"Expected value of type string. Got : %v\", value)\n\t\t}\n\t\treturn &api.Value{Val: &api.Value_DefaultVal{DefaultVal: v}}, nil\n\tcase IntID:\n\t\tvar v int64\n\t\tif v, ok = value.(int64); !ok {\n\t\t\treturn def, errors.Errorf(\"Expected value of type int64. Got : %v\", value)\n\t\t}\n\t\treturn &api.Value{Val: &api.Value_IntVal{IntVal: v}}, nil\n\tcase FloatID:\n\t\tvar v float64\n\t\tif v, ok = value.(float64); !ok {\n\t\t\treturn def, errors.Errorf(\"Expected value of type float64. Got : %v\", value)","sourceCodeStart":627,"sourceCodeEnd":663,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/types/conversion.go#L627-L663","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Convert the value to string before calling: string(byteSlice) or fmt.Sprintf / strconv where appropriate","Route raw data through types.ParseValue/types.Marshal first so the Val.Value is a proper string","Use the correct type ID matching the actual Go type of the value"],"exampleFix":"// before\ntypes.ObjectValue(types.StringID, []byte(\"hello\")) // error\n// after\ntypes.ObjectValue(types.StringID, string([]byte(\"hello\")))","handlingStrategy":"type-guard","validationCode":"func isString(v interface{}) bool { _, ok := v.(string); return ok }\nif isString(value) { types.ObjectValue(types.StringID, value) } else { /* convert first */ }","typeGuard":"func asString(v interface{}) (string, bool) { s, ok := v.(string); return s, ok }","tryCatchPattern":"av, err := types.ObjectValue(types.StringID, value)\nif err != nil && strings.Contains(err.Error(), \"Expected value of type string\") {\n    // coerce: value = fmt.Sprintf(\"%v\", value) and retry\n}","preventionTips":["Ensure upstream producers emit string for string predicates","Convert []byte to string at the boundary before ObjectValue","Match the type ID argument to the value's actual Go type"],"tags":["types","objectvalue","string","type-mismatch"],"backgroundTag":"value-type-mismatch","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}