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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: keep rich objects in mo.ui.chat contents, convert strings to markdown (#2629)
Myles Scolnick5b45c8e6fb2a
diff --git a/marimo/_plugins/ui/_impl/chat/chat.py b/marimo/_plugins/ui/_impl/chat/chat.pyindex 17b8acc8fa..af17d6e665 100644--- a/marimo/_plugins/ui/_impl/chat/chat.py+++ b/marimo/_plugins/ui/_impl/chat/chat.py@@ -6,6 +6,7 @@ from dataclasses import dataclassfrom typing import Any, Callable, Dict, Final, List, Optional, Union, castfrom marimo._output.formatting import as_html+from marimo._output.md import mdfrom marimo._output.rich_help import mddocfrom marimo._plugins.core.web_component import JSONTypefrom marimo._plugins.ui._core.ui_element import UIElement@@ -205,14 +206,8 @@ class chat(UIElement[Dict[str, Any], List[ChatMessage]]):passresponse = latest_response- content = (- as_html(response).text # convert to html if not a string- if not isinstance(response, str)- else response- )- self._chat_history = messages + [- ChatMessage(role="assistant", content=content)- ]+ response_message = ChatMessage(role="assistant", content=response)+ self._chat_history = messages + [response_message]from marimo._runtime.context import get_context@@ -234,7 +229,11 @@ class chat(UIElement[Dict[str, Any], List[ChatMessage]]):))- return content+ # Return the response as HTML+ # If the response is a string, convert it to markdown+ if isinstance(response, str):+ return md(response).text+ return as_html(response).textdef _convert_value(self, value: Dict[str, Any]) -> List[ChatMessage]:if not isinstance(value, dict) or "messages" not in value:diff --git a/marimo/_plugins/ui/_impl/chat/convert.py b/marimo/_plugins/ui/_impl/chat/convert.pyindex 5580462b1e..a2f7f7bba8 100644--- a/marimo/_plugins/ui/_impl/chat/convert.py+++ b/marimo/_plugins/ui/_impl/chat/convert.py@@ -94,7 +94,7 @@ def convert_to_google_messages(google_messages: List[Dict[Any, Any]] = []for message in messages:- parts: List[str | BlobDict] = [message.content]+ parts: List[str | BlobDict] = [str(message.content)]if message.attachments:for attachment in message.attachments:content_type = attachment.content_type or "text/plain"diff --git a/marimo/_plugins/ui/_impl/chat/llm.py b/marimo/_plugins/ui/_impl/chat/llm.pyindex 6429d5f0a8..7208208d4a 100644--- a/marimo/_plugins/ui/_impl/chat/llm.py+++ b/marimo/_plugins/ui/_impl/chat/llm.py@@ -39,7 +39,7 @@ class simple(ChatModel):self, messages: List[ChatMessage], config: ChatModelConfig) -> object:del config- prompt = messages[-1].content+ prompt = str(messages[-1].content)return self.delegate(prompt)diff --git a/marimo/_plugins/ui/_impl/chat/types.py b/marimo/_plugins/ui/_impl/chat/types.pyindex faa5612fb7..d825f52441 100644--- a/marimo/_plugins/ui/_impl/chat/types.py+++ b/marimo/_plugins/ui/_impl/chat/types.py@@ -60,7 +60,7 @@ class ChatMessage:role: Literal["user", "assistant", "system"]# The content of the message.- content: str+ content: object# Optional attachments to the message.attachments: Optional[List[ChatAttachment]] = Nonediff --git a/tests/_plugins/ui/_impl/chat/test_chat.py b/tests/_plugins/ui/_impl/chat/test_chat.pyindex 004c0807fb..910510ebc5 100644--- a/tests/_plugins/ui/_impl/chat/test_chat.py+++ b/tests/_plugins/ui/_impl/chat/test_chat.py@@ -6,6 +6,7 @@ from typing import AsyncIterator, Dict, Listimport pytest+from marimo._output.md import mdfrom marimo._plugins import uifrom marimo._plugins.ui._impl.chat.chat import SendMessageRequestfrom marimo._plugins.ui._impl.chat.types import (@@ -70,7 +71,7 @@ async def test_chat_send_prompt():)response: str = await chat._send_prompt(request)- assert response == "Response to: Hello"+ assert response == md("Response to: Hello").textassert len(chat._chat_history) == 2assert chat._chat_history[0].role == "user"assert chat._chat_history[0].content == "Hello"@@ -93,7 +94,7 @@ async def test_chat_send_prompt_async_function():)response: str = await chat._send_prompt(request)- assert response == "Response to: Hello"+ assert response == md("Response to: Hello").textassert len(chat._chat_history) == 2assert chat._chat_history[0].role == "user"assert chat._chat_history[0].content == "Hello"@@ -119,7 +120,7 @@ async def test_chat_send_prompt_async_generator():response: str = await chat._send_prompt(request)# the last yielded value is the response- assert response == "2"+ assert response == md("2").textassert len(chat._chat_history) == 2assert chat._chat_history[0].role == "user"assert chat._chat_history[0].content == "Hello"