apache/beam · error

mongodbio.Write: invalid option: %v

Error message

mongodbio.Write: invalid option: %v

What it means

mongodbio.Write validates each variadic WriteOption by invoking it; if an option func returns an error, Write panics at graph-construction time because options are expected to be statically valid. Note Write also infers the _id field index from the element type via structx tags right after.

Source

Thrown at sdks/go/pkg/beam/io/mongodbio/write.go:86

//   - Ordered: whether to execute the writes in order. Defaults to true
func Write(
	s beam.Scope,
	uri string,
	database string,
	collection string,
	col beam.PCollection,
	opts ...WriteOptionFn,
) beam.PCollection {
	s = s.Scope("mongodbio.Write")

	option := &WriteOption{
		BatchSize: defaultWriteBatchSize,
		Ordered:   defaultWriteOrdered,
	}

	for _, opt := range opts {
		if err := opt(option); err != nil {
			panic(fmt.Sprintf("mongodbio.Write: invalid option: %v", err))
		}
	}

	t := col.Type().Type()
	idIndex := structx.FieldIndexByTag(t, bsonTag, "_id")

	var keyed beam.PCollection

	if idIndex == -1 {
		pre := beam.ParDo(s, createIDFn, col)
		keyed = beam.Reshuffle(s, pre)
	} else {
		keyed = beam.ParDo(
			s,
			newExtractIDFn(idIndex),
			col,
			beam.TypeDefinition{Var: beam.XType, T: t.Field(idIndex).Type},
		)

View on GitHub (pinned to 12126d8942)

Solutions

  1. Fix the invalid input the option validates (positive BatchSize, sane Ordered flag)
  2. Inspect custom WriteOption funcs for validation bugs
  3. Validate batch size / ordering config before calling Write
  4. Wrap construction in recover() to surface the message cleanly

Example fix

// before
opt := func(o *mongodbio.WriteOption) error {
    o.BatchSize = cfg.BatchSize // 0
    return nil
}
// after
opt := func(o *mongodbio.WriteOption) error {
    if cfg.BatchSize <= 0 {
        return fmt.Errorf("batchSize must be > 0, got %d", cfg.BatchSize)
    }
    o.BatchSize = cfg.BatchSize
    return nil
}
Defensive patterns

Strategy: validation

Validate before calling

func validateWriteOption(o *mongodbio.WriteOption) error {
    if o.BatchSize <= 0 { return fmt.Errorf("BatchSize must be > 0") }
    return nil
}

Try / catch

defer func() { if r := recover(); r != nil { log.Fatalf("Write options invalid: %v", r) } }()

Prevention

When it happens

Trigger: Passing a custom func(*WriteOption) error that returns non-nil, e.g. a WithBatchSize helper rejecting non-positive batch sizes or an invalid Ordered combination, when calling Write.

Common situations: Options sourced from config flags (batch size 0 or negative); custom option funcs with buggy validation; conflicting options like generate-ID plus an existing _id tag mishandled.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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