mikefarah/yq · error

cannot pick indices from type %v (%v)

Error message

cannot pick indices from type %v (%v)

What it means

`pick` with an array of indices can only be applied to sequence (array) nodes; applying it to a scalar, map or other kind hits this default branch. The message reports the node's tag and nice path so you can locate the offending node. yq throws it rather than guessing how to index a non-array by position.

Source

Thrown at pkg/yqlib/operator_pick.go:77

	}

	var results = list.New()

	for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
		node := el.Value.(*CandidateNode)

		var replacement *CandidateNode
		switch node.Kind {
		case MappingNode:
			replacement = pickMap(node, indicesToPick)
		case SequenceNode:
			replacement, err = pickSequence(node, indicesToPick)
			if err != nil {
				return Context{}, err
			}

		default:
			return Context{}, fmt.Errorf("cannot pick indices from type %v (%v)", node.Tag, node.GetNicePath())
		}

		replacement.LeadingContent = node.LeadingContent
		results.PushBack(replacement)
	}

	return context.ChildContext(results), nil
}

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Select the array before picking: `.myArray | pick([0,1])` instead of `.myArray | pick(...)` on the parent map.
  2. Use map-key picking for maps (`pick(["key1"])`) and index picking only for arrays.
  3. Guard with `select(tag == "!!seq") | pick([0,1])` to skip non-array nodes.
  4. Inspect the path in the message and fix the traversal depth.

Example fix

// before
yq 'pick([0, 1])' file.yml            # root is a map
// after
yq '.items | pick([0, 1])' file.yml
Defensive patterns

Strategy: type-guard

Validate before calling

yq -e 'tag == "!!seq"' <<< "$node" || echo "pick([...]) requires an array"

Type guard

isSeq() { [ "$(yq 'tag == "!!seq"' <<< "$1")" = "true" ]; }

Try / catch

out=$(yq 'select(tag == "!!seq") | pick([0,1])' file.yml 2>&1) || { echo "$out"; echo "only arrays support index pick"; }

Prevention

When it happens

Trigger: Running `yq 'pick([0,1])'` on a document whose root (or selected nodes) are scalars or maps; a `pick` traversal that matches mixed-type nodes where at least one is not a sequence.

Common situations: Pointing pick at the wrong level of nesting (map instead of array); documents whose schema changed between versions; applying an array-oriented expression to every document in a multi-doc stream where one doc is a scalar.

Related errors


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