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 error serializing dictionary errors (#3977)

devin-ai-integration[bot]612aa111777b
diff --git a/marimo/_server/session/serialize.py b/marimo/_server/session/serialize.py
index 49aa32d12c..c161b3fdd9 100644
--- a/marimo/_server/session/serialize.py
+++ b/marimo/_server/session/serialize.py
@@ -5,7 +5,7 @@ import asyncio
import hashlib
import json
from pathlib import Path
-from typing import Optional, cast
+from typing import Any, Optional, Union, cast
from marimo import __version__, _loggers
from marimo._messaging.cell_output import CellChannel, CellOutput
@@ -44,12 +44,27 @@ def serialize_session_view(view: SessionView) -> NotebookSessionV1:
# Convert output
if cell_op.output:
if cell_op.output.channel == CellChannel.MARIMO_ERROR:
- for error in cast(list[MarimoError], cell_op.output.data):
+ for error in cast(
+ list[Union[MarimoError, dict[str, Any]]],
+ cell_op.output.data,
+ ):
+ # Handle both dictionary and object errors
+ # Errors can be a dictionary if they are serialized
+ error_type = (
+ error.get("type", "Unknown")
+ if isinstance(error, dict)
+ else error.type
+ )
+ error_value = (
+ error.get("msg", "")
+ if isinstance(error, dict)
+ else error.describe()
+ )
outputs.append(
ErrorOutput(
type="error",
- ename=error.type,
- evalue=error.describe(),
+ ename=error_type,
+ evalue=error_value,
traceback=[],
)
)
diff --git a/tests/_server/session/test_serialize_session.py b/tests/_server/session/test_serialize_session.py
index 2aaf69357e..fad6dae6b5 100644
--- a/tests/_server/session/test_serialize_session.py
+++ b/tests/_server/session/test_serialize_session.py
@@ -348,6 +348,34 @@ def test_deserialize_empty_data():
assert cell.output is None
+def test_serialize_session_with_dict_error():
+ """Test serialization of a session with a dictionary error"""
+ view = SessionView()
+ view.cell_operations["cell1"] = CellOp(
+ cell_id="cell1",
+ status="idle",
+ output=CellOutput(
+ channel=CellChannel.MARIMO_ERROR,
+ mimetype="text/plain",
+ data=[
+ {"type": "unknown", "msg": "Something went wrong"}
+ ], # Dictionary instead of Error object
+ ),
+ console=[],
+ timestamp=0,
+ )
+ view.last_executed_code["cell1"] = (
+ "raise RuntimeError('Something went wrong')"
+ )
+
+ result = serialize_session_view(view)
+ assert len(result["cells"]) == 1
+ assert len(result["cells"][0]["outputs"]) == 1
+ assert result["cells"][0]["outputs"][0]["type"] == "error"
+ assert result["cells"][0]["outputs"][0]["ename"] == "unknown"
+ assert result["cells"][0]["outputs"][0]["evalue"] == "Something went wrong"
+
+
class TestSessionCacheManager:
"""Test SessionCacheManager functionality"""
diff --git a/tests/_server/session/test_serialize_session_missing_type.py b/tests/_server/session/test_serialize_session_missing_type.py
new file mode 100644
index 0000000000..164b253765
--- /dev/null
+++ b/tests/_server/session/test_serialize_session_missing_type.py
@@ -0,0 +1,32 @@
+from marimo._messaging.cell_output import CellChannel, CellOutput
+from marimo._messaging.ops import CellOp
+from marimo._server.session.serialize import serialize_session_view
+from marimo._server.session.session_view import SessionView
+
+
+def test_serialize_session_with_dict_error_missing_type():
+ """Test serialization of a session with a dictionary error missing the type key"""
+ view = SessionView()
+ view.cell_operations["cell1"] = CellOp(
+ cell_id="cell1",
+ status="idle",
+ output=CellOutput(
+ channel=CellChannel.MARIMO_ERROR,
+ mimetype="text/plain",
+ data=[
+ {"msg": "Something went wrong"}
+ ], # Dictionary with missing type key
+ ),
+ console=[],
+ timestamp=0,
+ )
+ view.last_executed_code["cell1"] = (
+ "raise RuntimeError('Something went wrong')"
+ )
+
+ result = serialize_session_view(view)
+ assert len(result["cells"]) == 1
+ assert len(result["cells"][0]["outputs"]) == 1
+ assert result["cells"][0]["outputs"][0]["type"] == "error"
+ assert result["cells"][0]["outputs"][0]["ename"] == "Unknown"
+ assert result["cells"][0]["outputs"][0]["evalue"] == "Something went wrong"