Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: do not double encode msgspec values (#9005)

Dylan Madisetti0731654f9d35
diff --git a/marimo/_messaging/notification.py b/marimo/_messaging/notification.py
index f12bb881ec..64d98e52d5 100644
--- a/marimo/_messaging/notification.py
+++ b/marimo/_messaging/notification.py
@@ -224,11 +224,13 @@ class ModelLifecycleNotification(Notification, tag="model-lifecycle"):
import base64
d: dict[str, Any] = msgspec.to_builtins(self)
- # bytes are not JSON-serializable; base64-encode each buffer
msg = d.get("message", {})
if "buffers" in msg:
msg["buffers"] = [
- base64.b64encode(b).decode("ascii") for b in msg["buffers"]
+ b
+ if isinstance(b, str)
+ else base64.b64encode(b).decode("ascii")
+ for b in msg["buffers"]
]
return d
diff --git a/tests/_messaging/test_notifications.py b/tests/_messaging/test_notifications.py
index 2df14b9ad3..18b7d12ab1 100644
--- a/tests/_messaging/test_notifications.py
+++ b/tests/_messaging/test_notifications.py
@@ -7,6 +7,8 @@ from marimo._ast.toplevel import HINT_UNPARSABLE, TopLevelStatus
from marimo._messaging.notification import (
CellNotification,
InstallingPackageAlertNotification,
+ ModelLifecycleNotification,
+ ModelOpen,
StartupLogsNotification,
UIElementMessageNotification,
)
@@ -137,3 +139,18 @@ def test_send_ui_element_message_broadcast() -> None:
}
assert stream.parsed_operations[0] == msg
+
+
+def test_model_lifecycle_notification_to_json_serializable() -> None:
+ """to_json_serializable must not double-encode buffers (regression test)."""
+ notif = ModelLifecycleNotification(
+ model_id="model-1",
+ message=ModelOpen(
+ state={"value": 42},
+ buffer_paths=[["data"]],
+ buffers=[b"hello"],
+ ),
+ )
+
+ result = notif.to_json_serializable()
+ assert result["message"]["buffers"] == ["aGVsbG8="]