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
- Inspect the wrapped bson error to find the offending field and change its type to a BSON-encodable one.
- Add bson struct tags to ensure all fields map to valid BSON types.
- Ensure restriction/range structs only contain primitives, strings, time.Time, and ObjectIDs.
- 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
- Keep custom restriction/range fields restricted to BSON-encodable primitives.
- Add bson:"..." struct tags to every field you control.
- Run bson.Marshal round-trip tests on restriction structs in CI.
- Re-check encodability after upgrading go.mongodb.org/mongo-driver major versions.
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
- error decoding BSON: %w
- error decoding document ID: %w
- error decoding document: %w
- mongodbio.newReadFn: %v
- varint too long
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/7d34f2cce6390752.
Report an issue: GitHub.