cayleygraph/cayley · error

passed value was not a quad primitive: %T

Error message

passed value was not a quad primitive: %T

What it means

Quad(k) retrieves the quad for a given reference, but it requires the ref to be a *proto.Primitive — the concrete ref type this kv quadstore uses internally. If the caller passes any other graph.Ref implementation, the store cannot compute the KV key and returns this type assertion error. It is a programming/API-misuse guard, not a data problem.

Source

Thrown at graph/kv/quadstore.go:378

	if err != nil {
		return nil, err
	}
	return values, nil
}

func (qs *QuadStore) NameOf(v graph.Ref) (quad.Value, error) {
	ctx := context.TODO()
	vals, err := qs.ValuesOf(ctx, []graph.Ref{v})
	if err != nil {
		return nil, fmt.Errorf("error getting NameOf %d: %w", v, err)
	}
	return vals[0], nil
}

func (qs *QuadStore) Quad(k graph.Ref) (quad.Quad, error) {
	key, ok := k.(*proto.Primitive)
	if !ok {
		return quad.Quad{}, fmt.Errorf("passed value was not a quad primitive: %T", k)
	}
	ctx := context.TODO()
	var v quad.Quad
	err := kv.View(ctx, qs.db, func(tx kv.Tx) error {
		var err error
		v, err = qs.primitiveToQuad(ctx, tx, key)
		return err
	})
	if err == kv.ErrNotFound {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("error fetching quad %#v: %w", key, err)
	}
	return v, err
}

func (qs *QuadStore) primitiveToQuad(ctx context.Context, tx kv.Tx, p *proto.Primitive) (quad.Quad, error) {

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Only pass *proto.Primitive refs obtained from this QuadStore (iterators, QuadIterator results)
  2. Type-check before calling: if _, ok := r.(*proto.Primitive); !ok { handle }
  3. Re-resolve the quad by its value instead of by ref if you only have an IRI/BNode
  4. Avoid persisting refs across backends or store restarts

Example fix

// before
q, err := qs.Quad(someIRIRef)
// after
p, ok := someIRIRef.(*proto.Primitive)
if !ok {
    return fmt.Errorf("need a *proto.Primitive ref from this store")
}
q, err := qs.Quad(p)
Defensive patterns

Strategy: type-guard

Validate before calling

if p, ok := k.(*proto.Primitive); !ok || p == nil {
    return errors.New("Quad requires a *proto.Primitive ref from this kv store")
}

Type guard

func toPrimitive(k graph.Ref) (*proto.Primitive, bool) {
    p, ok := k.(*proto.Primitive)
    if !ok || p == nil {
        return nil, false
    }
    return p, true
}

Prevention

When it happens

Trigger: Calling QuadStore.Quad with a graph.Ref that is not *proto.Primitive — e.g. a ref from another backend, a plain quad.IRI/BNode ref, or a nil ref.

Common situations: Mixing refs between quadstore backends; constructing refs manually instead of getting them from this store's iterators; switching the store type (memory vs kv) while reusing persisted refs.

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/52c57e2d9a510c89. Report an issue: GitHub.