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.

use custom renderer for marimo elements for chatbot (#8546)

Shahmir Varqha07ed04bacf5f
diff --git a/examples/ai/chat/custom.py b/examples/ai/chat/custom.py
index c031b8c9a3..d0fb74c7bc 100644
--- a/examples/ai/chat/custom.py
+++ b/examples/ai/chat/custom.py
@@ -7,7 +7,7 @@
import marimo
-__generated_with = "0.19.7"
+__generated_with = "0.20.2"
app = marimo.App(width="medium")
@@ -41,10 +41,11 @@ def _(mo):
# message.content: the content of the message
return f"You said: {messages[-1].content}!"
+
chatbot = mo.ui.chat(
simple_echo_model,
prompts=["Hello", "How are you?"],
- show_configuration_controls=False
+ show_configuration_controls=False,
)
chatbot
return (chatbot,)
@@ -65,5 +66,23 @@ def _(chatbot):
return
+def _(mo):
+ def marimo_chatbot(messages, config):
+ """Returns marimo elements"""
+ message = messages[-1]
+ return mo.vstack(
+ [
+ mo.md("## Response"),
+ mo.ui.table([1, 2, 3]),
+ mo.ui.slider(1, 10, label="slider"),
+ ]
+ )
+
+
+ mo.ui.chat(marimo_chatbot)
+ return
+
+
if __name__ == "__main__":
app.run()
diff --git a/frontend/src/components/chat/chat-display.tsx b/frontend/src/components/chat/chat-display.tsx
index c485f8dac0..02803142e4 100644
--- a/frontend/src/components/chat/chat-display.tsx
+++ b/frontend/src/components/chat/chat-display.tsx
@@ -1,6 +1,8 @@
/* Copyright 2026 Marimo. All rights reserved. */
import type { DataUIPart, ToolUIPart, UIMessage } from "ai";
+import React from "react";
+import { renderHTML } from "@/plugins/core/RenderHTML";
import { logNever } from "@/utils/assertNever";
import { Logger } from "@/utils/Logger";
import { MarkdownRenderer } from "../markdown/markdown-renderer";
@@ -46,6 +48,17 @@ export const renderUIMessage = ({
switch (part.type) {
case "text":
+ // Streamdown sanitizes the HTML which strips out marimo elements
+ // So instead, we render the HTML with our custom renderer.
+ if (part.text.includes("<marimo-")) {
+ return (
+ <React.Fragment key={index}>
+ {renderHTML({
+ html: part.text,
+ })}
+ </React.Fragment>
+ );
+ }
return <MarkdownRenderer key={index} content={part.text} />;
case "reasoning":
return (