Dash 2.8 and orjson

I have looked at using orjson for the serialisation of the callback I use to store data.
However, if I return a callback with eg.
orjson.dumps({dic full of numpy or datetime}, orjson.OPT_NAIVE_UTC | orjson.OPT_SERIALIZE_NUMPY)

I get plenty of complains that it is of type byte which is precisely what I want.
dash.exceptions.InvalidCallbackReturnValue: The callback for [<Output view_store.data>, <Output {“id”:“notifications_provider”,“idx”:4,“prop”:“children”}.data>]
returned a value having type bytes
which is not JSON serializable.

So far I have not seen an example exploiting orjson. Performance | Dash for Python Documentation | Plotly indicates that Dash would use it by default but it is not correct since I cannot send bytes between callbacks.

Am I missing something?

I have not seen anywhere an example of it working as it is not functioning “out of the box” it seems.


Hi, I think the clue is in the error message:
returned a value having type bytes
which is not JSON serializable.

I would assume you would get the same error with the standard JSON library

In general I think there are two separate things. The statement in the docs : orjson is completely optional: If it exists in the environment, then Dash will use it; if not, Dash will use the default json library. refers imho to the fact of any JSON storage needs for any internal dash state.

Whatever you do additional to that has no reflection on that. Saving and retrieving state for your app.

Maybe some user has an actual advice on how to implement this :slight_smile:

orjson serialisation is of type bytes, it is the whole point of it.

BTW, what I use is this class to be able to dump stuff between Callbacks

class JsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        elif isinstance(obj, datetime):
            return (str(obj))
            return json.JSONEncoder.default(self, obj)

That proves perfect except that if I could transfer less data between callback, I assume it would be faster to run.

From what I could see in the doc this seems about right. Either default (as you did) or setting the
Option orjson.OPT_PASSTHROUGH_SUBCLASS which would ignore unsupported subtypes.
Did you try that one to see if the error disappears and what data is available/missing?

OK, so it seems that it might be because I use dash extensions enrich
Traceback (most recent call last):

  File "/home/julien/.local/lib/python3.10/site-packages/dash_extensions/enrich.py", line 747, in decorated_function
    outputs = f(*args, **kwargs, dash_logger=logger)
  File "/media/julien/NuDrive/Consulting/The NW-Edge/Oceano/Westcoast/FarmedLiceWestCoast/app/main.py", line 347, in initialise_var
    return orjson.dumps(variables,
TypeError: unsupported datatype in numpy array

Sorry @Emil to disturb you again but is orjson integrated in the extension when I use DashProxy?
Or is it something I would not be able to use to serialize between callbacks anyway?

Apart from the case of ServersideOutput (where dash-extensions changes the serialization flow completely), I don’t believe that dash-extensions would affect the serialization mechanism. Could you provide a small example that works in Dash, but not using dash-extensions? Then I’ll take a look at what is casing the issue :slight_smile:

so if the serialisation doesn’t change that means that dumping an orjson to transfer data between callback will not work in any case, whatever the extension.
I am very confused by Performance | Dash for Python Documentation | Plotly stating we could serialise with orjson.
It is rather there that we are lacking an example no?

Is there anyone that managed to serialise with orjson as the output of a callback?