mikefarah/yq · error

cannot multiply %v with %v

Error message

cannot multiply %v with %v

What it means

yq's multiply operator ('*' expression) supports number*number and string*int (string repetition). multiplyScalars checks the tags of both operands and throws this error when the tag combination matches none of the supported cases (e.g. string*string, string*float, bool*int, null*string).

Source

Thrown at pkg/yqlib/operator_multiply.go:97

func multiplyScalars(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
	lhsTag := lhs.Tag
	rhsTag := rhs.guessTagFromCustomType()
	lhsIsCustom := false
	if !strings.HasPrefix(lhsTag, "!!") {
		// custom tag - we have to have a guess
		lhsTag = lhs.guessTagFromCustomType()
		lhsIsCustom = true
	}

	if lhsTag == "!!int" && rhsTag == "!!int" {
		return multiplyIntegers(lhs, rhs)
	} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
		return multiplyFloats(lhs, rhs, lhsIsCustom)
	} else if (lhsTag == "!!str" && rhsTag == "!!int") || (lhsTag == "!!int" && rhsTag == "!!str") {
		return repeatString(lhs, rhs)
	}
	return nil, fmt.Errorf("cannot multiply %v with %v", lhs.Tag, rhs.Tag)
}

func multiplyFloats(lhs *CandidateNode, rhs *CandidateNode, lhsIsCustom bool) (*CandidateNode, error) {
	target := lhs.CopyWithoutContent()
	target.Kind = ScalarNode
	target.Style = lhs.Style
	if lhsIsCustom {
		target.Tag = lhs.Tag
	} else {
		target.Tag = "!!float"
	}

	lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
	if err != nil {
		return nil, err
	}
	rhsNum, err := strconv.ParseFloat(rhs.Value, 64)
	if err != nil {

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Verify both operand types with 'yq -o=json' or 'tag' (e.g. '.x | tag'); ensure numeric operands are !!int/!!float
  2. Convert string numbers before multiplying: '(.a | tonumber) * (.b | tonumber)' (or '@yaml' style '(.x | tag) == "!!str" | select(...) | .x | to_number' depending on version)
  3. If you wanted string repetition, make sure one side is !!int and the other !!str, e.g. '"-" * 3'
  4. If you wanted concatenation, use '+' instead of '*'

Example fix

// before (yq expression)
yq '.msg * 3' file.yml        // fails if .msg is a quoted string number like "4"
// after
yq '(.msg | tonumber) * 3' file.yml
Defensive patterns

Strategy: validation

Validate before calling

yq '.lhs | tag' f.yml && yq '.rhs | tag' f.yml  // ensure !!int/!!float pair, or !!str+!!int
// shell guard:
[[ "$(yq '.x | tag' f.yml)" =~ "!!(int|float)" ]] || echo "convert with tonumber"

Type guard

# yq expression guard
yq 'select((.x | tag) == "!!int" or (.x | tag) == "!!float") | .x * 2' f.yml

Prevention

When it happens

Trigger: Running a yq expression like '"abc" * "def"', '"abc" * 1.5', 'true * 2', or '"abc" * "3"' (RHS string not an !!int). Also occurs when a custom-typed tag cannot be guessed to int/float/str on either side.

Common situations: Unquoted numbers in YAML/JSON that were parsed as strings (e.g. version strings like '1.2' vs quoted '1.2'), environment/config values that are always strings, typos in expressions, or accidentally multiplying two strings expecting concatenation.

Related errors


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