{"record":{"id":"12c4768b4716c978","repo":"dgraph-io/dgraph","slug":"fail-to-convert-from-api-value-to-types-val","errorCode":null,"errorMessage":"Fail to convert from api.Value to types.Val","messagePattern":"Fail to convert from api\\.Value to types\\.Val","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/aggregator.go","lineNumber":54,"sourceCode":"\nfunc isTernary(f string) bool {\n\treturn f == \"cond\"\n}\n\nfunc isBinary(f string) bool {\n\treturn f == \"+\" || f == \"*\" || f == \"-\" || f == \"/\" || f == \"%\" ||\n\t\tf == \"max\" || f == \"min\" || f == \"logbase\" || f == \"pow\" ||\n\t\tf == \"dot\"\n}\n\nfunc convertTo(from *pb.TaskValue) (types.Val, error) {\n\tvh, _ := getValue(from)\n\tif bytes.Equal(from.Val, x.Nilbyte) {\n\t\treturn vh, ErrEmptyVal\n\t}\n\tva, err := types.Convert(vh, vh.Tid)\n\tif err != nil {\n\t\treturn vh, errors.Wrapf(err, \"Fail to convert from api.Value to types.Val\")\n\t}\n\treturn va, err\n}\n\nfunc compareValues(ag string, va, vb types.Val) (bool, error) {\n\tif !isBinaryBoolean(ag) {\n\t\tx.Fatalf(\"Function %v is not binary boolean\", ag)\n\t}\n\n\t_, err := types.Less(va, vb)\n\tif err != nil {\n\t\t//Try to convert values.\n\t\tswitch {\n\t\tcase va.Tid == types.IntID:\n\t\t\tva.Tid = types.FloatID\n\t\t\tva.Value = float64(va.Value.(int64))\n\t\tcase vb.Tid == types.IntID:\n\t\t\tvb.Tid = types.FloatID","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/aggregator.go#L36-L72","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Align the schema with the actual stored data, or migrate/rewrite the offending values","Find the offending predicate and fix the stored value (delete and re-set with correct type)","Validate data before bulk load so stored types match the declared schema","Rebuild the index for the predicate after a schema type change"],"exampleFix":"// before: schema changed to int but old string data remains\n// after: migrate values before querying\nfor each node of predicate {\n  v := readRaw(node)\n  if !validForSchema(v) { rewrite(node, convert(v)) }\n}","handlingStrategy":"validation","validationCode":"// Validate stored value matches schema type before querying\nval, _ := node.Value(pred)\nif val != nil {\n    if _, err := types.Convert(val, schema.Type(pred)); err != nil {\n        // fix or migrate the value before running conversions\n    }\n}","typeGuard":"func isConvertible(v *api.Value) bool {\n    if v == nil || bytes.Equal(v.Val, x.Nilbyte) { return false }\n    _, err := types.Convert(v, v.Tid)\n    return err == nil\n}","tryCatchPattern":"va, err := convertTo(from)\nif err != nil && strings.Contains(err.Error(), \"Fail to convert from api.Value\") {\n    return vh, fmt.Errorf(\"stored value for predicate %q does not match schema type: %w\", pred, err)\n}","preventionTips":["Never change a predicate's schema type in place; migrate data first","Validate bulk-load data against the schema before import","Rebuild indexes after any type change","Alert on conversion failures per predicate to catch schema drift early"],"tags":["type-conversion","schema","query"],"backgroundTag":"value-type-conversion-failed","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}