apache/beam · error

error encoding BSON: %w

Error message

error encoding BSON: %w

What it means

Returned by the generic helper encodeBSON in mongodbio when bson.Marshal fails to serialize a value (restriction or range for the MongoDB splitter) into BSON bytes. This indicates the Go struct or value passed to the encoder is not BSON-encodable.

Source

Thrown at sdks/go/pkg/beam/io/mongodbio/coder.go:65

func encodeRestriction(in idRangeRestriction) ([]byte, error) {
	return encodeBSON(in)
}
func decodeRestriction(in []byte) (idRangeRestriction, error) {
	return decodeBSON[idRangeRestriction](in)
}

func encodeRange(in idRange) ([]byte, error) {
	return encodeBSON(in)
}
func decodeRange(in []byte) (idRange, error) {
	return decodeBSON[idRange](in)
}

func encodeBSON[T any](in T) ([]byte, error) {
	out, err := bson.Marshal(in)
	if err != nil {
		return nil, fmt.Errorf("error encoding BSON: %w", err)
	}

	return out, nil
}

func decodeBSON[T any](in []byte) (T, error) {
	var out T
	if err := bson.Unmarshal(in, &out); err != nil {
		return out, fmt.Errorf("error decoding BSON: %w", err)
	}

	return out, nil
}

func encodeObjectID(objectID primitive.ObjectID) []byte {
	return objectID[:]
}

View on GitHub (pinned to 12126d8942)

Solutions

  1. Inspect the wrapped bson error to find the offending field and change its type to a BSON-encodable one.
  2. Add bson struct tags to ensure all fields map to valid BSON types.
  3. Ensure restriction/range structs only contain primitives, strings, time.Time, and ObjectIDs.
  4. Check for mongo-driver version incompatibilities after dependency upgrades.

Example fix

// before: unencodable field
splitFn struct { done chan struct{}; start int64 }

// after: only BSON-friendly fields
splitFn struct { done bool; start int64 }
Defensive patterns

Strategy: try-catch

Validate before calling

// keep restriction/range structs BSON-safe:
// only string, ints, floats, bool, time.Time, primitive.ObjectID, slices; no chans/funcs/nil interfaces
func bsonSafe(v any) error { _, err := bson.Marshal(v); return err }

Try / catch

encoded, err := encodeBSON(r)
if err != nil {
    return fmt.Errorf("encoding split restriction: %w", err)
}

Prevention

When it happens

Trigger: Calling encodeBSON (via encodeRestriction/encodeRange) with a type containing unsupported fields, e.g. channels, funcs, maps with non-string keys, nil interfaces, or invalid NaN values.

Common situations: Adding custom fields to restriction/range structs after a Beam version upgrade; embedding non-serializable types; primitive type mismatches after upgrading the mongo-driver major version.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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