From 57a3a98d073412146ddd7ffb6526ef3e518af616 Mon Sep 17 00:00:00 2001 From: Sean Doherty Date: Sat, 16 May 2026 20:14:45 -0500 Subject: [PATCH] Handle Decimal in Kaleido JSON serialization --- src/py/kaleido/_kaleido_tab/_tab.py | 3 +++ src/py/tests/test_kaleido_tab.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/py/tests/test_kaleido_tab.py diff --git a/src/py/kaleido/_kaleido_tab/_tab.py b/src/py/kaleido/_kaleido_tab/_tab.py index 5ac7de46..3837d76a 100644 --- a/src/py/kaleido/_kaleido_tab/_tab.py +++ b/src/py/kaleido/_kaleido_tab/_tab.py @@ -1,6 +1,7 @@ from __future__ import annotations import base64 +from decimal import Decimal from typing import TYPE_CHECKING import logistro @@ -27,6 +28,8 @@ def _orjson_default(obj): """Fallback for types orjson can't handle natively (e.g. NumPy string arrays).""" + if isinstance(obj, Decimal): + return float(obj) if hasattr(obj, "tolist"): return obj.tolist() raise TypeError(f"Type is not JSON serializable: {type(obj).__name__}") diff --git a/src/py/tests/test_kaleido_tab.py b/src/py/tests/test_kaleido_tab.py new file mode 100644 index 00000000..ee8e10f0 --- /dev/null +++ b/src/py/tests/test_kaleido_tab.py @@ -0,0 +1,16 @@ +from decimal import Decimal + +import orjson +import plotly.graph_objects as go + +from kaleido._kaleido_tab._tab import _orjson_default +from kaleido._utils import fig_tools + + +def test_orjson_default_handles_decimal(): + fig = go.Figure(data=[go.Bar(y=[Decimal("10.5"), Decimal(20), Decimal("-3.25")])]) + spec = fig_tools.coerce_for_js(fig, None, {"format": "json"}) + + encoded = orjson.dumps(spec, default=_orjson_default) + + assert b'"y":[10.5,20.0,-3.25]' in encoded