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: enable weave tracing on chat widget (#8078)

Dylan Madisettid4f8e5ab52a2
diff --git a/marimo/_dependencies/dependencies.py b/marimo/_dependencies/dependencies.py
index 89b314bef0..baf673f4f0 100644
--- a/marimo/_dependencies/dependencies.py
+++ b/marimo/_dependencies/dependencies.py
@@ -252,6 +252,7 @@ class DependencyManager:
)
pydantic = Dependency("pydantic")
zmq = Dependency("zmq") # pyzmq for sandbox IPC kernels
+ weave = Dependency("weave")
# Version requirements to properly support the new superfences introduced in
# pymdown#2470
diff --git a/marimo/_plugins/ui/_impl/chat/chat.py b/marimo/_plugins/ui/_impl/chat/chat.py
index 5919c9792c..9c3d25ca3b 100644
--- a/marimo/_plugins/ui/_impl/chat/chat.py
+++ b/marimo/_plugins/ui/_impl/chat/chat.py
@@ -475,6 +475,8 @@ class ChunkSerializer:
# Handle plain text chunks
if isinstance(chunk, str):
+ # Coerce str subclasses (like weave's BoxedStr) to plain str
+ chunk = str(chunk)
if self._text_id is None:
self._text_id = f"text_{uuid.uuid4().hex}"
self.on_send_chunk({"type": "text-start", "id": self._text_id})
diff --git a/pyproject.toml b/pyproject.toml
index 3680d02489..1b19ca6902 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -203,6 +203,8 @@ test-optional = [
"mcp>=1.0.0",
# zeromq
"pyzmq>=27.1.0",
+ # weave tracing integration
+ "weave>=0.51.0",
]
diff --git a/tests/_plugins/ui/_impl/chat/test_chat.py b/tests/_plugins/ui/_impl/chat/test_chat.py
index 505d4f949a..dc4a15afe3 100644
--- a/tests/_plugins/ui/_impl/chat/test_chat.py
+++ b/tests/_plugins/ui/_impl/chat/test_chat.py
@@ -880,6 +880,64 @@ def test_serialize_only_dict_chunks():
]
+def test_serialize_str_subclass():
+ """Ensure str subclasses (like weave's BoxedStr) are coerced to plain str."""
+
+ class BoxedStr(str):
+ """Mock of weave's BoxedStr - a str subclass with extra attributes."""
+
+ def __new__(cls, value):
+ instance = super().__new__(cls, value)
+ instance._id = "trace_123"
+ instance.ref = "weave://entity/project/..."
+ return instance
+
+ sent_chunks: list[dict] = []
+
+ def on_send_chunk(chunk: dict):
+ sent_chunks.append(chunk)
+
+ serializer = ChunkSerializer(on_send_chunk=on_send_chunk)
+
+ boxed = BoxedStr("Hello from weave")
+ serializer.handle_chunk(boxed)
+
+ # Should have text-start and text-delta
+ assert len(sent_chunks) == 2
+ assert sent_chunks[0]["type"] == "text-start"
+ assert sent_chunks[1]["type"] == "text-delta"
+ # Critical: delta should be plain str, not BoxedStr
+ assert sent_chunks[1]["delta"] == "Hello from weave"
+ assert type(sent_chunks[1]["delta"]) is str # Exact type check
+
+
+ not DependencyManager.weave.has(),
+ reason="weave is not installed",
+)
+def test_serialize_weave_boxed_str():
+ """Integration test with actual weave BoxedStr."""
+ import weave
+
+ @weave.op
+ def traced_fn() -> str:
+ return "traced response"
+
+ result = traced_fn()
+
+ sent_chunks: list[dict] = []
+
+ def on_send_chunk(chunk: dict):
+ sent_chunks.append(chunk)
+
+ serializer = ChunkSerializer(on_send_chunk=on_send_chunk)
+ serializer.handle_chunk(result)
+
+ assert len(sent_chunks) == 2
+ assert sent_chunks[1]["type"] == "text-delta"
+ assert type(sent_chunks[1]["delta"]) is str
+
+
@pytest.mark.skipif(
not DependencyManager.pydantic_ai.has(),
reason="Pydantic AI is not installed",