Skip to content

Mojo Error: value passed to 'x' cannot be converted from 'A' to 'B'

error: invalid call to 'f': value passed to 'x' cannot be converted from 'A' to 'B' means you called a function with an argument of the wrong type. f is the function, x is the name of its argument, A is the type you passed and B is the type it wants. It also shows up for operators like * and +, where f is a method name such as '__mul__'. For the other errors in this series, see Mojo Errors Explained.

def shout(msg: String):
print(msg.upper())
def main():
var n = 5
shout(n)
shout.mojo:7:11: error: invalid call to 'shout': value passed to 'msg' cannot be converted from 'Int' to 'String'
shout(n)
~~~~~ ^
shout.mojo:1:5: note: function declared here
def shout(msg: String):
^
mojo: error: failed to parse the provided Mojo source module

The message has everything you need:

  1. invalid call to 'shout': the call on line 7 doesn’t fit the function.
  2. value passed to 'msg': the problem is the argument called msg.
  3. from 'Int' to 'String': you gave an Int, and msg needs a String.
  4. note: function declared here points to the function’s signature, so you can see what it expects.

Each argument of a Mojo function has a type, and the value you pass must be that type, or a type Mojo can convert implicitly (silently). Literals convert easily: shout("hi") is fine. But an Int variable doesn’t turn into a String by itself, so the call is rejected before the program runs.

It’s the same rule as cannot implicitly convert ‘X’ value to ‘Y’, which you get when you assign or return a value. This one is for calls.

Python would run shout(5) and crash inside the function with AttributeError: 'int' object has no attribute 'upper', maybe only when a user hits that path. Mojo checks the types at every call site, so the mistake is caught at the call, with the exact argument named. The functions page of the Mojo manual explains how arguments are typed.

If the function’s type is right and your value just isn’t that type yet, convert it at the call:

def shout(msg: String):
print(msg.upper())
def main():
var n = 5
shout(String(n) + " new messages")

Output:

5 NEW MESSAGES

String(n), Float64(n) and Int(x) are the usual conversions. See Mojo Type Conversion.

This is the version beginners see most. Operators like * and + are method calls on the left value, so price * qty calls Float64.__mul__ with qty as its argument rhs (right-hand side):

def main():
var price = 2.5
var qty = 4
print(price * qty)
cart.mojo:4:19: error: invalid call to '__mul__': value passed to 'rhs' cannot be converted from 'Int' to 'Float64'
print(price * qty)
~~~~~~~~^~~
Mojo/stdlib/std/simd.mojo:1130:1: note: function declared here
def __mul__(self, rhs: Self) -> Self # note - synthetic signature
^
mojo: error: failed to parse the provided Mojo source module

The note points into simd.mojo because Float64 is a SIMD type with one element; you can ignore that part. Both sides of * must have the same type, so convert the Int:

def main():
var price = 2.5
var qty = 4
print(price * Float64(qty))

Output:

10.0

The same happens with sized integers: Int64 + Int32 fails with cannot be converted from 'Int32' to 'Int64'. Pick one type and convert the other side. Mojo Numbers explains which number types exist.

A bracket literal on its own, [1, 2, 3], is a fixed-size Array in Mojo, not a List. Store it in a variable without a type and pass it on, and you get a surprising message:

def total(nums: List[Int]) -> Int:
var t = 0
for n in nums:
t += n
return t
def main():
var a = [1, 2, 3]
print(total(a))
total.mojo:10:17: error: invalid call to 'total': value passed to 'nums' cannot be converted from 'Array[Int, Int(3)]' to 'List[Int]'
print(total(a))
~~~~~ ^
total.mojo:1:5: note: function declared here
def total(nums: List[Int]) -> Int:
^
mojo: error: failed to parse the provided Mojo source module

Give the variable the type List[Int]:

def total(nums: List[Int]) -> Int:
var t = 0
for n in nums:
t += n
return t
def main():
var a: List[Int] = [1, 2, 3]
print(total(a))

Output:

6

Passing the literal straight in, total([1, 2, 3]), also works, because then Mojo knows it should build a List. See Mojo Lists and Mojo Arrays.

Sometimes the function’s type is the mistake. If shout should accept anything printable, or a number type should be Float64 instead of Int, change the signature, not every call. A function that should take several types can be overloaded or made generic.

  • list.append(...) with the wrong element type. xs.append("three") on a List[Int] says invalid call to 'append': value passed to 'value' cannot be converted from 'StringLiteral["three"]' to 'Int'.
  • Int and Float64 in one expression, as in Fix 2. Mojo never mixes them silently.
  • An Array where a List is expected, as in Fix 3.
  • A trait-bound argument. When a function takes T: SomeTrait, the message ends with argument type 'X' does not conform to trait 'SomeTrait'. That means your type needs to implement the trait. See Mojo Traits.

Fix the call so the program prints Total: 12.0.

def with_tax(price: Float64, rate: Float64) -> Float64:
return price * (1 + rate)
def main():
var items = 4
var rate = 0.5
print("Total:", with_tax(items * 2, rate))

Hint: items * 2 is an Int, and price needs a Float64.

Show solution
def with_tax(price: Float64, rate: Float64) -> Float64:
return price * (1 + rate)
def main():
var items = 4
var rate = 0.5
print("Total:", with_tax(Float64(items * 2), rate))

Output:

Total: 12.0

items * 2 is 8, an Int. Wrapping it in Float64(...) makes it the type price expects. The literal 1 inside with_tax is fine, since literals convert by themselves.

  • The message names the function, the argument, the type you passed and the type it wants.
  • Convert at the call with String(n), Float64(n) or Int(x).
  • a * b is a call to __mul__, so mixing Int and Float64 gives this error with 'rhs'.
  • A bare [1, 2, 3] in a variable is an Array; write var a: List[Int] = [...] for a list.
  • If the function’s type is wrong, fix the signature once instead of every call.

Next up: the SIMD type must be floating point, a confusing message with a simple cause.

Quick check

1. In value passed to 'msg' cannot be converted from 'Int' to 'String', what is msg?
2. With var price = 2.5 and var qty = 4, which line builds?
3. Why does var a = [1, 2] followed by total(a) fail when total takes List[Int]?