apache/beam · error

bad encode

Error message

bad encode

What it means

NewCustomCoder wraps any error returned when converting the user-supplied encode function into an internal function value via funcx.New(reflectx.MakeFunc(encode)) with the message "bad encode". It means the encode function itself could not be turned into a callable reflection wrapper — typically because it is nil or not a usable function value. This happens after validateEncoder passes, so it usually indicates a structurally invalid function value rather than a signature mismatch.

Source

Thrown at sdks/go/pkg/beam/core/graph/coder/coder.go:151

func validateDecoder(t reflect.Type, decode any) error {
	// Check if it uses the real type in question.
	if err := funcx.Satisfy(decode, funcx.Replace(decodeSig, typex.TType, t)); err != nil {
		return errors.WithContext(err, "validateDecoder: validating signature")
	}
	// TODO(lostluck): 2019.02.03 - Expand cases to avoid []byte -> any conversion
	// in exec, & a beam Decoder interface.
	return nil
}

// NewCustomCoder creates a coder for the supplied parameters defining a
// particular encoding strategy.
func NewCustomCoder(id string, t reflect.Type, encode, decode any) (*CustomCoder, error) {
	if err := validateEncoder(t, encode); err != nil {
		return nil, errors.WithContext(err, "NewCustomCoder")
	}
	enc, err := funcx.New(reflectx.MakeFunc(encode))
	if err != nil {
		return nil, errors.Wrap(err, "bad encode")
	}
	if err := validateDecoder(t, decode); err != nil {
		return nil, errors.WithContext(err, "NewCustomCoder")
	}

	dec, err := funcx.New(reflectx.MakeFunc(decode))
	if err != nil {
		return nil, errors.Wrap(err, "bad decode")
	}

	c := &CustomCoder{
		Name: id,
		Type: t,
		Enc:  enc,
		Dec:  dec,
	}
	return c, nil
}

View on GitHub (pinned to 12126d8942)

Solutions

  1. Ensure the encode argument is a non-nil Go function with signature func(T) []byte (or compatible)
  2. Check you did not swap the encode and decode arguments to NewCustomCoder
  3. Inspect the wrapped underlying error in the errors.Wrap chain for the exact funcx failure
  4. If passing a method value, verify the receiver is non-nil

Example fix

// before
cc, err := coder.NewCustomCoder("myCoder", reflect.TypeOf(x), nil, decodeFn)
// after
cc, err := coder.NewCustomCoder("myCoder", reflect.TypeOf(x), func(v T) []byte { return encode(v) }, decodeFn)
Defensive patterns

Strategy: validation

Validate before calling

if encode == nil { return errors.New("encode must be a non-nil func") }
if rv := reflect.ValueOf(encode); rv.Kind() != reflect.Func { return fmt.Errorf("encode must be a func, got %v", rv.Kind()) }

Type guard

func isFunc(v any) bool { return v != nil && reflect.ValueOf(v).Kind() == reflect.Func }

Prevention

When it happens

Trigger: Passing a nil encode function, a non-function value, or a function whose reflect.MakeFunc conversion fails to NewCustomCoder(id, t, encode, decode).

Common situations: Swapping the encode and decode arguments, passing a method value obtained from a nil receiver, or passing an interface variable holding nil instead of a concrete func.

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/b8826ef9c33a1409. Report an issue: GitHub.