apache/beam · error

mongodbio.inferProjection: no names to infer projection from

Error message

mongodbio.inferProjection: no names to infer projection from

What it means

inferProjection derives a BSON projection from a Go struct's field names (via structx.InferFieldNames with a tag key like bson). If the struct type yields zero field names, there is nothing to project and the function panics. This guards against reading into an empty or unsuitable struct type.

Source

Thrown at sdks/go/pkg/beam/io/mongodbio/read.go:150

	var err error
	if err = fn.mongoDBFn.Setup(ctx); err != nil {
		return err
	}

	fn.filter, err = decodeBSON[bson.M](fn.Filter)
	if err != nil {
		return err
	}

	fn.projection = inferProjection(fn.Type.T, bsonTag)

	return nil
}

func inferProjection(t reflect.Type, tagKey string) bson.D {
	names := structx.InferFieldNames(t, tagKey)
	if len(names) == 0 {
		panic("mongodbio.inferProjection: no names to infer projection from")
	}

	projection := make(bson.D, len(names))

	for i, name := range names {
		projection[i] = bson.E{Key: name, Value: 1}
	}

	return projection
}

func (fn *readFn) CreateInitialRestriction(
	ctx context.Context,
	_ []byte,
) (idRangeRestriction, error) {
	if err := fn.Setup(ctx); err != nil {
		return idRangeRestriction{}, err
	}

View on GitHub (pinned to 12126d8942)

Solutions

  1. Add exported fields with proper tags to the struct used as the read target
  2. Use the correct tag key for your struct (bson for mongodbio)
  3. Verify reflect.Type passed is the concrete struct, not an interface or empty alias
  4. Fall back to nil projection (read all fields) if inference legitimately finds nothing

Example fix

// before
type Row struct{} // no fields
mongodbio.Read(s, scope, uri, db, col, reflect.TypeOf(Row{}), opt)
// after
type Row struct {
    ID   primitive.ObjectID `bson:"_id"`
    Name string             `bson:"name"`
}
mongodbio.Read(s, scope, uri, db, col, reflect.TypeOf(Row{}), opt)
Defensive patterns

Strategy: type-guard

Validate before calling

t := reflect.TypeOf(Row{})
if t.NumField() == 0 { return fmt.Errorf("read target struct has no fields") }

Type guard

func hasInferableFields(t reflect.Type, tag string) bool {
    if t == nil || t.Kind() != reflect.Struct { return false }
    for i := 0; i < t.NumField(); i++ {
        if _, ok := t.Field(i).Tag.Lookup(tag); ok { return true }
    }
    return t.NumField() > 0
}

Prevention

When it happens

Trigger: Calling mongodbio.Read with a target type t that is an empty struct (no fields), an unexported/anonymous struct with no inferable fields, or a wrong tag key such that no field carries the expected tag and the inference falls back to nothing.

Common situations: Passing reflect.TypeOf on an empty result struct; mismatched tag key (e.g. expecting bson tags but the struct only has json tags combined with inference settings); generated types with no exported fields.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/aa1c45aad674624e. Report an issue: GitHub.