Paho MQTT 'Unsupported callback API version' error

12.2k Views Asked by At

I am trying to implement Paho Python MQTT and connect to an online broker but the code seems to through an error.

ValueError: Unsupported callback API version: version 2.0 added a callback_api_version, see migrations.md for details

I was trying to implement a simple paho client example from the given website; the following will reproduce the issue:

from paho.mqtt import client as mqtt_client
import random

broker = 'broker.emqx.io'
port = 1883
topic = "python/mqtt"
client_id = f'python-mqtt-{random.randint(0, 1000)}'
client = mqtt_client.Client(client_id)
3

There are 3 best solutions below

2
Brits On

Release 2.0.0 of the Paho Python MQTT includes breaking changes; this means that code written for v1.x will not work without some (minimal) modifications. As v2.0.0 was only released a few days ago (11th Feb 2024) most examples, including the one you reference, will not work.

The changes required are documented here (or here); in your case it's likely that the only change needed is add a single parameter changing:

client = mqtt_client.Client(client_id)

to:

client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1, client_id)

This will configure the library to use the v1 callback API (as used with older versions of the library). I would recommend reading the document linked above and planning to migrate to CallbackAPIVersion.API_VERSION2.

An alternative option would be to install a v1 release (v1.6.1 is the latest; pip install "paho-mqtt<2.0.0" will install this). V2 does include quite a few fixes/enhancements so it is worth considering using that version.

2
user1527079 On

I hope this is of any help to anybody running into the same problem. All credits go to Brits! With his feedback I managed to fix the issue for me. I could not get the client_id fix to work so I went with the following. (EDIT: not direct answer to the original question but related to it)

The mqtt-gpio indeed installed paho-mqtt-2.0.0 (blue box). pip install paho<2.0.0 didn't work for me as you can see below. Paho-Mqtt-2.0.0 not working

I had to use pip install "paho<2.0.0" for it to work. It uninstalled Paho-2.0.0 and installs Paho1.6.1. May be for many people this is common sense but probably not for everybody. It wasn't for me as well (beginner over here!). Solution for me

Hope this is of any help for someone.

1
Amith On

Try this , if you don't want to upgrade to new version:

#Old mqttc = mqtt.Client(client_id)

#New

mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1,client_id)

ref: https://eclipse.dev/paho/files/paho.mqtt.python/html/migrations.html