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
- Add exported fields with proper tags to the struct used as the read target
- Use the correct tag key for your struct (bson for mongodbio)
- Verify reflect.Type passed is the concrete struct, not an interface or empty alias
- 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
- Always tag struct fields with bson tags for read targets
- Assert target types are non-empty structs in unit tests
- Keep the tag key consistent (bson) across the codebase
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
- cannot make schema encoder for type %v as it has an embedded
- cannot make schema encoder for type %v as it has unexported
- panicked: %v
- cannot convert field %v to schema
- panic: %v %s
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/aa1c45aad674624e.
Report an issue: GitHub.