cayleygraph/cayley · error

unexpected type for bool field: %T

Error message

unexpected type for bool field: %T

What it means

toQuadValue converts the boolean value field to quad.Bool; this error is thrown when the stored fldValBool field is anything other than nosql.Bool. The boolean slot of the value document holds an unexpected type so the value cannot be reconstructed.

Source

Thrown at graph/nosql/quadstore.go:577

		return quad.Int(vi), nil
	} else if v, ok := d[fldValFloat]; ok {
		var vf quad.Float
		switch v := v.(type) {
		case nosql.Int:
			vf = quad.Float(v)
		case nosql.Float:
			vf = quad.Float(v)
		default:
			return nil, fmt.Errorf("unexpected type for float field: %T", v)
		}
		return vf, nil
	} else if v, ok := d[fldValBool]; ok {
		var vb quad.Bool
		switch v := v.(type) {
		case nosql.Bool:
			vb = quad.Bool(v)
		default:
			return nil, fmt.Errorf("unexpected type for bool field: %T", v)
		}
		return vb, nil
	} else if v, ok := d[fldValTime]; ok {
		var vt quad.Time
		switch v := v.(type) {
		case nosql.Time:
			vt = quad.Time(v)
		case nosql.String:
			var t time.Time
			if err := t.UnmarshalJSON([]byte(`"` + string(v) + `"`)); err != nil {
				return nil, err
			}
			vt = quad.Time(t)
		default:
			return nil, fmt.Errorf("unexpected type for bool field: %T", v)
		}
		return vt, nil
	}

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Inspect %T of the stored value to find the real type.
  2. Re-load the data through the current Cayley version so booleans are stored as nosql.Bool.
  3. Fix custom importers to write nosql.Bool values.
  4. Dump and re-import the store if widespread encoding drift is present.

Example fix

// before
default:
    return nil, fmt.Errorf("unexpected type for bool field: %T", v)
// after
case nosql.String:
    vb = quad.Bool(string(v) == "true")
default:
    return nil, fmt.Errorf("unexpected type for bool field: %T", v)
Defensive patterns

Strategy: type-guard

Validate before calling

func isBoolField(v nosql.Value) bool {
    _, ok := v.(nosql.Bool)
    return ok
}

Type guard

func asBoolSafe(v interface{}) (quad.Bool, bool) {
    b, ok := v.(nosql.Bool)
    return quad.Bool(b), ok
}

Try / catch

val, err := toQuadValue(opt, doc)
if err != nil {
    log.Printf("bad bool field %T, skipping value: %v", doc[fldValBool], err)
    return quad.Raw(""), nil
}

Prevention

When it happens

Trigger: Reading a boolean quad value whose document field was stored as String/Int/Bytes/Time by an incompatible writer, or a corrupted document.

Common situations: Migration between Cayley versions or NoSQL backends with different type mappings; custom tooling writing booleans as strings ("true"); hand-edited documents.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06). Data as JSON: /api/errors/7df973d7b3b124d7. Report an issue: GitHub.