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
- Verify both operand types with 'yq -o=json' or 'tag' (e.g. '.x | tag'); ensure numeric operands are !!int/!!float
- Convert string numbers before multiplying: '(.a | tonumber) * (.b | tonumber)' (or '@yaml' style '(.x | tag) == "!!str" | select(...) | .x | to_number' depending on version)
- If you wanted string repetition, make sure one side is !!int and the other !!str, e.g. '"-" * 3'
- 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
- Check tags with '| tag' before arithmetic on document values
- Use '| tonumber' on values that may be strings
- Use '+' for concatenation, '*' only for numeric multiply or string*int repetition
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
- orderedMap: invalid yaml node
- %v (%v) cannot be subtracted from %v
- %v cannot check contained in %v
- from entries only runs against arrays
- cannot substitute with %v, can only substitute strings. Hint
AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05).
Data as JSON: /api/errors/1a7fc335cc606775.
Report an issue: GitHub.