apache/beam · error

received unknown value type: want []byte, got %T

Error message

received unknown value type: want []byte, got %T

What it means

The bytes encoder in the exec package expects a FullValue whose Elm is a []byte. If the element holds any other type, this error is returned instead of silently mis-encoding. It means the element's runtime type doesn't match the coder the pipeline assigned to it.

Source

Thrown at sdks/go/pkg/beam/core/runtime/exec/coder.go:313

	case coder.ShardedKey:
		return &shardedKeyDecoder{
			key: MakeElementDecoder(c.Components[0]),
		}

	default:
		panic(fmt.Sprintf("Unexpected coder: %v", c))
	}
}

type bytesEncoder struct{}

func (*bytesEncoder) Encode(val *FullValue, w io.Writer) error {
	// Encoding: size (varint) + raw data
	var data []byte
	data, ok := val.Elm.([]byte)
	if !ok {
		return errors.Errorf("received unknown value type: want []byte, got %T", val.Elm)
	}
	return coder.EncodeBytes(data, w)
}

type bytesDecoder struct{}

func (*bytesDecoder) DecodeTo(r io.Reader, fv *FullValue) error {
	// Encoding: size (varint) + raw data
	data, err := coder.DecodeBytes(r)
	if err != nil {
		return err
	}
	*fv = FullValue{Elm: data}
	return nil
}

func (d *bytesDecoder) Decode(r io.Reader) (*FullValue, error) {
	fv := &FullValue{}

View on GitHub (pinned to 12126d8942)

Solutions

  1. Make the DoFn emit []byte: convert with []byte(str) before output.
  2. Fix the PCollection type declaration so the coder matches the element type.
  3. Check beam.Encoded / coder registration so strings use a string coder, not the bytes coder.
  4. Add a type assertion in the DoFn to fail early with a clearer message.

Example fix

// before
out.Emit(ctx, s) // s is a string into a bytes-coder PCollection
// after
out.Emit(ctx, []byte(s))
Defensive patterns

Strategy: type-guard

Validate before calling

// guard elements before emitting into a bytes-coder PCollection
func assertBytes(v any) ([]byte, error) {
    b, ok := v.([]byte)
    if !ok {
        return nil, fmt.Errorf("bytes coder requires []byte, got %T", v)
    }
    return b, nil
}

Type guard

func isByteSlice(v any) bool {
    _, ok := v.([]byte)
    return ok
}

Try / catch

if err := enc.Encode(fv, w); err != nil {
    if strings.Contains(err.Error(), "want []byte") {
        return fmt.Errorf("element type %T does not match bytes coder: %w", fv.Elm, err)
    }
    return err
}

Prevention

When it happens

Trigger: bytesEncoder.Encode is called with a FullValue whose Elm is, e.g., a string or custom struct instead of []byte — usually a coder/type mismatch between the PCollection's declared type and emitted values.

Common situations: Emitting string values into a PCollection declared as []byte; schema inference picking the bytes coder for a differently-typed element; unsafe type assertions bypassed by reflection.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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