dagger/dagger · error

expected instance of %T, got %T

Error message

expected instance of %T, got %T

What it means

In a cached field spec, dagql asserts that the `self` receiver passed in implements ObjectResult[T] for the field's declared type T before invoking cacheFn. If it does not (type assertion fails), this error reports the expected field type and the actual self type (dagql/objects.go:868).

Source

Thrown at dagql/objects.go:868

			return NewResultForCurrentCall(ctx, res)
		},
	}

	if cacheFn != nil {
		field.Spec.GetDynamicInput = func(ctx context.Context, self AnyResult, argVals map[string]Input, view call.View, req *CallRequest) error {
			if argsErr != nil {
				// this error is deferred until runtime, since it's better (at least
				// more testable) than panicking
				return argsErr
			}
			var args A
			if err := spec.Args.Decode(argVals, &args, view); err != nil {
				return err
			}
			inst, ok := self.(ObjectResult[T])
			if !ok {
				return fmt.Errorf("expected instance of %T, got %T", field, self)
			}
			return cacheFn(ctx, inst, args, req)
		}
	}

	return field
}

// FieldSpec is a specification for a field.
type FieldSpec struct {
	// Name is the name of the field.
	Name string
	// Description is the description of the field.
	Description string
	// Args is the list of arguments that the field accepts.
	Args InputSpecs
	// Type is the type of the field's result.
	Type Typed

View on GitHub (pinned to 82ba2681db)

Solutions

  1. Ensure the field is invoked on an instance of the same object type it was defined for (matching T in ObjectResult[T]).
  2. Check the ObjectSpec/field registration: the field should be added to the class of the object type it operates on.
  3. Fix any generic type parameter mismatch in the Field/cacheFn definition so T matches the instance type.

Example fix

// before
var wrong = dagql.Field(containerClass, "name", ...) // called with pipelineInstance
// after
pipelineClass.Fields.Add("name", ...) // invoke on an ObjectResult[Pipeline]
Defensive patterns

Strategy: type-guard

Type guard

func asObjectResult[T any](self any) (ObjectResult[T], bool) {
    inst, ok := self.(ObjectResult[T])
    return inst, ok
}

Try / catch

err := classCall(ctx)
if err != nil && strings.Contains(err.Error(), "expected instance of") {
    log.Fatalf("field invoked with wrong instance type: %v", err)
}

Prevention

When it happens

Trigger: Registering/calling a cached field with a self value whose concrete type differs from the ObjectResult[T] the field spec was built for — e.g. mixing object classes, calling a field of type A on an instance of type B.

Common situations: Copy-pasted field specs across object types; generic mis-parameterization when defining fields; custom servers wiring the wrong instance into Select.

Related errors


AI-assisted analysis of dagger/dagger@82ba2681db (2026-09-05). Data as JSON: /api/errors/698609a1319525cd. Report an issue: GitHub.