{"record":{"id":"d2193412e7bbfabe","repo":"cayleygraph/cayley","slug":"unexpected-type-for-pb-field-t","errorCode":null,"errorMessage":"unexpected type for pb field: %T","messagePattern":"unexpected type for pb field: %T","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graph/nosql/quadstore.go","lineNumber":537,"sourceCode":"\t}\n\treturn vi, nil\n}\n\nfunc toQuadValue(opt *Traits, d nosql.Document) (quad.Value, error) {\n\tif len(d) == 0 {\n\t\treturn nil, nil\n\t}\n\tvar err error\n\t// prefer protobuf representation\n\tif v, ok := d[fldValPb]; ok {\n\t\tvar b []byte\n\t\tswitch v := v.(type) {\n\t\tcase nosql.String:\n\t\t\tb, err = base64.StdEncoding.DecodeString(string(v))\n\t\tcase nosql.Bytes:\n\t\t\tb = []byte(v)\n\t\tdefault:\n\t\t\terr = fmt.Errorf(\"unexpected type for pb field: %T\", v)\n\t\t}\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tvar p pquads.Value\n\t\tif err := proto.Unmarshal(b, &p); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"couldn't decode value: %v\", err)\n\t\t}\n\t\treturn p.ToNative(), nil\n\t} else if v, ok := d[fldValInt]; ok {\n\t\tif opt.Number32 {\n\t\t\t// parse from string, so we are confident that we will get exactly the same value\n\t\t\tif vs, ok := d[fldValStrInt].(nosql.String); ok {\n\t\t\t\tiv := quad.Int(stoi(string(vs)))\n\t\t\t\treturn iv, nil\n\t\t\t}\n\t\t}\n\t\tvi, err := asInt(v)","sourceCodeStart":519,"sourceCodeEnd":555,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/graph/nosql/quadstore.go#L519-L555","documentation":"toQuadValue decodes protobuf-encoded pquads.Value blobs from the document; this error is thrown when the pb field is neither a nosql.String (base64) nor nosql.Bytes. It means the stored value field has an unexpected type for the protobuf value encoding path.","triggerScenarios":"NameOf/toQuadValue reading a document whose fldValPB field holds a different nosql type (Int, Bool, Time) — data written by an incompatible writer/encoder or a mis-mapped field.","commonSituations":"Quadstore data written before the pquads protobuf value encoding or by a different storage layout; a corrupted document from a crashed bulk load; custom import tools storing raw strings without base64/proto encoding.","solutions":["Inspect %T of the value to identify the actual stored type and its origin.","Re-load the affected data through the current Cayley version so values are encoded as pquads protobuf blobs.","If you control the writer, store values as nosql.Bytes (or base64 nosql.String) after proto.Marshal of pquads.Value.","Exclude/repair corrupt documents — dump and re-import the store to rebuild value documents.","Verify you are reading the intended field (fldValPB) rather than another value-kind field."],"exampleFix":"// before\ndefault:\n    err = fmt.Errorf(\"unexpected type for pb field: %T\", v)\n// after\ncase nosql.String: // existing base64 path\n    b, err = base64.StdEncoding.DecodeString(string(v))\ncase nosql.Bytes:\n    b = []byte(v)\ndefault:\n    return nil, fmt.Errorf(\"unexpected type for pb field: %T\", v)","handlingStrategy":"type-guard","validationCode":"func isPBValue(v nosql.Value) bool {\n    switch v.(type) {\n    case nosql.String, nosql.Bytes:\n        return true\n    }\n    return false\n}","typeGuard":"func pbBytes(v interface{}) ([]byte, bool) {\n    switch t := v.(type) {\n    case nosql.String:\n        b, err := base64.StdEncoding.DecodeString(string(t))\n        return b, err == nil\n    case nosql.Bytes:\n        return []byte(t), true\n    }\n    return nil, false\n}","tryCatchPattern":"val, err := toQuadValue(opt, doc)\nif err != nil {\n    log.Printf(\"unreadable value doc (re-import needed): %v\", err)\n    return quad.Raw(\"\"), nil // or skip the document\n}","preventionTips":["Store pb values only as nosql.Bytes or base64 nosql.String","Keep pquads schema aligned with the Cayley version reading it","Re-run imports after storage layout changes","Skip-and-log unreadable documents during bulk reads"],"tags":["type-mismatch","protobuf","nosql","cayley"],"backgroundTag":"type-mismatch","analyzedSha":"81dcd7d73e45136bc0d01802a8ba4685d8a533eb","analyzedAt":"2026-09-06T06:14:12.358Z","contentChangedAt":"2026-09-06T06:14:12.358Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}