mikefarah/yq · error

sequence contains elements of %v and %v types

Error message

sequence contains elements of %v and %v types

What it means

The `pivot` operator requires all elements of the sequence to share a single type tag so it can decide how to pivot (as sequences or as maps). `getUniqueElementTag` walks the elements and throws as soon as it finds two different tags. Heterogeneous sequences are rejected because pivot cannot produce a consistent result.

Source

Thrown at pkg/yqlib/operator_pivot.go:17

package yqlib

import (
	"container/list"
	"fmt"
)

func getUniqueElementTag(seq *CandidateNode) (string, error) {
	switch l := len(seq.Content); l {
	case 0:
		return "", nil
	default:
		result := seq.Content[0].Tag
		for i := 1; i < l; i++ {
			t := seq.Content[i].Tag
			if t != result {
				return "", fmt.Errorf("sequence contains elements of %v and %v types", result, t)
			}
		}
		return result, nil
	}
}

var nullNodeFactory = func() *CandidateNode { return createScalarNode(nil, "") }

func pad[E any](array []E, length int, factory func() E) []E {
	sz := len(array)
	if sz >= length {
		return array
	}
	pad := make([]E, length-sz)
	for i := 0; i < len(pad); i++ {
		pad[i] = factory()
	}
	return append(array, pad...)

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Make the sequence homogeneous before pivoting: filter with `map(select(tag == "!!map"))` (or the desired type).
  2. Convert elements to a common type, e.g. wrap scalars in maps/arrays.
  3. Split the sequence and pivot each homogeneous part separately.
  4. Inspect element tags with `.[] | tag` to find the mismatching elements.

Example fix

// before
yq '[{a: 1}, 2] | pivot(.)' file.yml
// after
yq '[{a: 1}, 2] | map(select(tag == "!!map")) | pivot(.)' file.yml
Defensive patterns

Strategy: validation

Validate before calling

yq -e '[.[] | tag] | length == 1' <<< "$seq" || echo "sequence elements must share one type before pivot"

Type guard

isHomogeneousSeq() { [ "$(yq '[.[] | tag] | unique | length == 1' <<< "$1")" = "true" ]; }

Try / catch

out=$(yq 'pivot(.k)' file.yml 2>&1) || { echo "$out"; echo "filter to one element type: map(select(tag == "!!map"))"; }

Prevention

When it happens

Trigger: `yq 'pivot(...)'` on a sequence mixing element types, e.g. `[1, "a"]`, `[{a: 1}, [1,2]]`, or a sequence containing both maps and scalars.

Common situations: Data files mixing objects and arrays in one list; schemas where an optional field is sometimes a map and sometimes a scalar; concatenating differently-typed results before pivoting.

Related errors


AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05). Data as JSON: /api/errors/323629e3146f5077. Report an issue: GitHub.