apache/beam · error

RegisterShardedKeyType: bad enc for %v: %v

Error message

RegisterShardedKeyType: bad enc for %v: %v

What it means

RegisterShardedKeyType wraps the raw enc/dec closures with funcx.New to create reflection-friendly function wrappers required by the runtime. This panic fires when funcx.New fails on the encoder function, meaning encFn's signature is not acceptable to funcx (wrong parameter or return types).

Source

Thrown at sdks/go/pkg/beam/transforms/batch/batch.go:149

	// Closures inside generic functions share the same compiler
	// symbol name for every type instantiation. We wrap them with a
	// type-qualified name so the cross-worker deserializer resolves
	// the correct enc/dec for each ShardedKey[K].
	encName := fmt.Sprintf("batch.encShardedKey[%v]", keyT)
	decName := fmt.Sprintf("batch.decShardedKey[%v]", keyT)

	encFn := reflectx.MakeFuncWithName(encName, enc)
	decFn := reflectx.MakeFuncWithName(decName, dec)

	// Register in the runtime cache under the qualified name so
	// ResolveFunction finds them at deserialization time.
	runtime.RegisterFunctionWithName(encName, enc)
	runtime.RegisterFunctionWithName(decName, dec)

	encWrapped, err := funcx.New(encFn)
	if err != nil {
		panic(fmt.Sprintf("RegisterShardedKeyType: bad enc for %v: %v", skT, err))
	}
	decWrapped, err := funcx.New(decFn)
	if err != nil {
		panic(fmt.Sprintf("RegisterShardedKeyType: bad dec for %v: %v", skT, err))
	}

	beamcoder.RegisterDeterministicCoderWithFuncs(skT, encWrapped, decWrapped)
}

// Params configures GroupIntoBatches and
// GroupIntoBatchesWithShardedKey.
//
// At least one of BatchSize or BatchSizeBytes must be > 0.
type Params struct {
	// BatchSize is the target maximum number of elements per batch. A
	// batch is emitted as soon as it holds BatchSize elements. Zero
	// disables the count-based trigger.
	BatchSize int64

View on GitHub (pinned to 12126d8942)

Solutions

  1. Upgrade or align the Beam Go SDK version so funcx.New supports the generated closures.
  2. Check that K is a simple encodable type; try a struct wrapper for exotic key types.
  3. Log the funcx.New error fully (it names the unsupported signature) and adjust the closure signature accordingly.

Example fix

// before
panic(fmt.Sprintf("RegisterShardedKeyType: bad enc for %v: %v", skT, err))
// after
panic(fmt.Errorf("RegisterShardedKeyType: bad enc for %v: %w", skT, err)) // read err to fix the closure signature
// plus: verify funcx.New supports the enc signature, or restructure enc as a named function
Defensive patterns

Strategy: validation

Validate before calling

// verify funcx wrapping before panic path
if _, err := funcx.New(encFn); err != nil {
    log.Fatalf("funcx cannot wrap enc for %v: %v", reflect.TypeOf(new(K)), err)
}

Try / catch

func init() {
    defer func() {
        if r := recover(); r != nil {
            log.Fatalf("RegisterShardedKeyType init failed: %v", r)
        }
    }()
    batch.RegisterShardedKeyType[MyKey]()
}

Prevention

When it happens

Trigger: Calling RegisterShardedKeyType[K] at init time where the generated encFn does not match funcx.New's expected shape — typically a Beam Go SDK version issue or a compiler/reflection edge case with generic closures (the code comments mention closures inside generic functions sharing compiler symbol names).

Common situations: Upgrading the Beam Go SDK where funcx.New signature expectations changed; exotic key types K causing closure specialization problems; running with a Go toolchain that alters closure representation.

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