{"record":{"id":"323629e3146f5077","repo":"mikefarah/yq","slug":"sequence-contains-elements-of-v-and-v-types","errorCode":null,"errorMessage":"sequence contains elements of %v and %v types","messagePattern":"sequence contains elements of (.+?) and (.+?) types","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_pivot.go","lineNumber":17,"sourceCode":"package yqlib\n\nimport (\n\t\"container/list\"\n\t\"fmt\"\n)\n\nfunc getUniqueElementTag(seq *CandidateNode) (string, error) {\n\tswitch l := len(seq.Content); l {\n\tcase 0:\n\t\treturn \"\", nil\n\tdefault:\n\t\tresult := seq.Content[0].Tag\n\t\tfor i := 1; i < l; i++ {\n\t\t\tt := seq.Content[i].Tag\n\t\t\tif t != result {\n\t\t\t\treturn \"\", fmt.Errorf(\"sequence contains elements of %v and %v types\", result, t)\n\t\t\t}\n\t\t}\n\t\treturn result, nil\n\t}\n}\n\nvar nullNodeFactory = func() *CandidateNode { return createScalarNode(nil, \"\") }\n\nfunc pad[E any](array []E, length int, factory func() E) []E {\n\tsz := len(array)\n\tif sz >= length {\n\t\treturn array\n\t}\n\tpad := make([]E, length-sz)\n\tfor i := 0; i < len(pad); i++ {\n\t\tpad[i] = factory()\n\t}\n\treturn append(array, pad...)","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_pivot.go#L1-L35","documentation":"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.","triggerScenarios":"`yq 'pivot(...)'` on a sequence mixing element types, e.g. `[1, \"a\"]`, `[{a: 1}, [1,2]]`, or a sequence containing both maps and scalars.","commonSituations":"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.","solutions":["Make the sequence homogeneous before pivoting: filter with `map(select(tag == \"!!map\"))` (or the desired type).","Convert elements to a common type, e.g. wrap scalars in maps/arrays.","Split the sequence and pivot each homogeneous part separately.","Inspect element tags with `.[] | tag` to find the mismatching elements."],"exampleFix":"// before\nyq '[{a: 1}, 2] | pivot(.)' file.yml\n// after\nyq '[{a: 1}, 2] | map(select(tag == \"!!map\")) | pivot(.)' file.yml","handlingStrategy":"validation","validationCode":"yq -e '[.[] | tag] | length == 1' <<< \"$seq\" || echo \"sequence elements must share one type before pivot\"","typeGuard":"isHomogeneousSeq() { [ \"$(yq '[.[] | tag] | unique | length == 1' <<< \"$1\")\" = \"true\" ]; }","tryCatchPattern":"out=$(yq 'pivot(.k)' file.yml 2>&1) || { echo \"$out\"; echo \"filter to one element type: map(select(tag == \"!!map\"))\"; }","preventionTips":["Filter or normalise the sequence to a single element type before pivoting.","Inspect element tags with [.[] | tag] | unique.","Avoid concatenating differently-typed results before pivot.","Wrap scalars into objects if mixed lists are expected."],"tags":["yq","pivot","heterogeneous-sequence","type-mismatch"],"backgroundTag":"pivot-heterogeneous-sequence","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}