apache/beam · error

bad field type

Error message

bad field type

What it means

encodeType encodes each struct field's type recursively; 'bad field type' means one of the struct's field types failed to encode (e.g. a field of map or array type, which the wire format does not support). The error context names the struct being encoded so the offending field can be located.

Solutions

  1. Replace map/array struct fields with slices of key-value structs.
  2. Wrap the map/array as a field of an inner struct per beam issue 23101 guidance (note the outer struct may still fail; convert to slice-of-structs is safer).
  3. Apply a custom coder for the affected type so the default structural encoding is bypassed.
  4. Read the wrapped context chain ('encoding struct X ... encoding ...') to pinpoint the offending field.

Example fix

// before
type Record struct {
    Tags map[string]string
}
// after
type Tag struct { K, V string }
type Record struct {
    Tags []Tag
}
Defensive patterns

Strategy: validation

Validate before calling

t := reflect.TypeOf(rec)
for i := 0; i < t.NumField(); i++ {
    if err := isEncodableType(t.Field(i).Type); err != nil {
        return fmt.Errorf("field %s of %v unencodable: %w", t.Field(i).Name, t, err)
    }
}

Type guard

func encodableStructFields(v any) bool { t := reflect.TypeOf(v); if t.Kind() != reflect.Struct { return false }; for i := 0; i < t.NumField(); i++ { if isEncodableType(t.Field(i).Type) != nil { return false } }; return true }

Try / catch

if err := isEncodableType(reflect.TypeOf(rec)); err != nil {
    return fmt.Errorf("restructure struct %T (maps/arrays in fields): %w", rec, err)
}

Prevention

When it happens

Trigger: Serializing a struct (PCollection element or DoFn struct) where a field's reflect.Type hits an unencodable case in encodeType - most commonly map or array field kinds.

Common situations: Event records with embedded maps (e.g. Attributes map[string]string) streamed through a pipeline exported to a remote runner; nested structs containing arrays.

Related errors


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

Appendix: source

Thrown at sdks/go/pkg/beam/core/runtime/graphx/serialize.go:448

		if err != nil {
			wrapped := errors.Wrap(err, "bad element type")
			return nil, errors.WithContextf(wrapped, "encoding slice %v", t)
		}
		return &v1pb.Type{Kind: v1pb.Type_SLICE, Element: elm}, nil

	case reflect.Struct:
		var fields []*v1pb.Type_StructField
		for i := 0; i < t.NumField(); i++ {
			f := t.Field(i)

			if f.PkgPath != "" {
				wrapped := errors.Errorf("type has unexported field: %v", f.Name)
				return nil, errors.WithContextf(wrapped, "encoding struct %v", t)
			}

			fType, err := encodeType(f.Type)
			if err != nil {
				wrapped := errors.Wrap(err, "bad field type")
				return nil, errors.WithContextf(wrapped, "encoding struct %v", t)
			}

			field := &v1pb.Type_StructField{
				Name:      f.Name,
				PkgPath:   f.PkgPath,
				Type:      fType,
				Tag:       string(f.Tag),
				Offset:    int64(f.Offset),
				Index:     encodeInts(f.Index),
				Anonymous: f.Anonymous,
			}
			fields = append(fields, field)
		}
		return &v1pb.Type{Kind: v1pb.Type_STRUCT, Fields: fields}, nil

	case reflect.Func:
		var in []*v1pb.Type

View on GitHub (pinned to 12126d8942)