apache/beam · error

input must be a slice or array

Error message

input %v must be a slice or array

What it means

TryCreateList inserts a fixed set of values from a Go slice or array into the pipeline. This error is thrown when the input value's reflect.Kind is neither Slice nor Array — there is no element sequence to turn into a PCollection.

Solutions

  1. Wrap the input in a slice: CreateList(s, []int{1,2,3}) instead of CreateList(s, 1)
  2. If passing a map, extract its values into a slice first
  3. Use TryCreate instead if you intend to create a PCollection of a single value

Example fix

// before
pc, err := beam.TryCreateList(s, 42)
// after
pc, err := beam.TryCreateList(s, []int{42})
Defensive patterns

Strategy: validation

Validate before calling

v := reflect.ValueOf(list)
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array { return fmt.Errorf("%T is not a slice/array", list) }

Type guard

func isSliceOrArray(v any) bool { k := reflect.ValueOf(v).Kind(); return k == reflect.Slice || k == reflect.Array }

Try / catch

pc, err := beam.TryCreateList(s, list)
if err != nil {
    if strings.Contains(err.Error(), "must be a slice or array") { /* wrap single value in a 1-element slice */ }
    return PCollection{}, err
}

Prevention

When it happens

Trigger: Calling CreateList/TryCreateList with a map, string, struct, pointer, or nil instead of a slice/array.

Common situations: Passing a single element instead of a slice of elements, passing a map where a slice was intended, or a config value unmarshalled into any/interface{} as a non-slice type.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at sdks/go/pkg/beam/create.go:65

// TryCreate inserts a fixed non-empty set of values into the pipeline. The
// values must be of the same type.
func TryCreate(s Scope, values ...any) (PCollection, error) {
	if len(values) == 0 {
		err := errors.New("create has no values")
		return PCollection{}, addCreateCtx(err, s)
	}

	t := reflect.ValueOf(values[0]).Type()
	return createList(s, values, t)
}

// TryCreateList inserts a fixed set of values into the pipeline from a slice or
// array. The values must be of the same type. Unlike TryCreate this supports
// the creation of an empty PCollection.
func TryCreateList(s Scope, list any) (PCollection, error) {
	val := reflect.ValueOf(list)
	if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
		err := errors.Errorf("input %v must be a slice or array", list)
		return PCollection{}, addCreateCtx(err, s)
	}

	var ret []any
	for i := 0; i < val.Len(); i++ {
		ret = append(ret, val.Index(i).Interface())
	}

	var t reflect.Type
	if len(ret) == 0 {
		t = reflect.TypeOf(list).Elem()
	} else {
		t = reflect.ValueOf(ret[0]).Type()
	}
	return createList(s, ret, t)
}

func addCreateCtx(err error, s Scope) error {

View on GitHub (pinned to 12126d8942)