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.

Keep rich objects in chat message (#7851)

Myles Scolnickd7f2cf1483df
diff --git a/marimo/_ai/_types.py b/marimo/_ai/_types.py
index abe6a0799c..d2e9cebe4a 100644
--- a/marimo/_ai/_types.py
+++ b/marimo/_ai/_types.py
@@ -204,12 +204,13 @@ class ChatMessage(msgspec.Struct):
role: Literal["user", "assistant", "system"]
# The content of the message.
+ # This can be a rich Python object.
content: Any
# The id of the message.
id: str = ""
- # Parts from AI SDK
+ # Parts from AI SDK Stream Protocol (must be serializable to JSON)
parts: list[ChatPart] = []
# Optional attachments to the message.
diff --git a/marimo/_plugins/ui/_impl/chat/chat.py b/marimo/_plugins/ui/_impl/chat/chat.py
index f3b6538966..40b11e1514 100644
--- a/marimo/_plugins/ui/_impl/chat/chat.py
+++ b/marimo/_plugins/ui/_impl/chat/chat.py
@@ -333,7 +333,7 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):
)
)
- async def _send_prompt(self, args: SendMessageRequest) -> str | None:
+ async def _send_prompt(self, args: SendMessageRequest) -> None:
messages = args.messages
self._chat_history = messages
@@ -368,15 +368,21 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):
)
# Add assistant response to chat history
- assistant_message = ChatMessage(role="assistant", content=response_str)
+ assistant_message = ChatMessage(
+ role="assistant",
+ content=response,
+ id=f"message_{uuid.uuid4().hex}",
+ )
self._chat_history.append(assistant_message)
+ await self._handle_streaming_response([response_str])
# Update the chat history to trigger UI updates and on_message callback
self._update_chat_history(self._chat_history)
- return response_str
+ return None
def _convert_value(self, value: dict[str, Any]) -> list[ChatMessage]:
+ """Convert the frontend's chat history format to a list of ChatMessage objects."""
if not isinstance(value, dict) or "messages" not in value:
raise ValueError("Invalid chat history format")
@@ -392,22 +398,39 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):
# as Vercel UIMessagePart
part_validator_class = UIMessagePart
- msg_to_content = {
- msg.get("id"): msg.get("content")
- for msg in messages
- if msg.get("content") is not None
- }
-
- return [
- ChatMessage.create(
- role=msg.get("role", "user"),
- message_id=msg.get("id"),
- content=msg_to_content.get(msg.get("id")),
- parts=msg.get("parts", []),
- part_validator_class=part_validator_class,
+ def get_prev_content(idx: int) -> Any:
+ # Only get the prev content if messages are the same size
+ if len(messages) == len(self._chat_history):
+ return self._chat_history[idx].content
+ return None
+
+ result: list[ChatMessage] = []
+ for i, msg in enumerate(messages):
+ prev_content = get_prev_content(i)
+ if isinstance(msg, ChatMessage):
+ if prev_content is not None:
+ msg.content = prev_content
+ continue
+
+ msg_id = msg.get("id")
+ role = msg.get("role", "user")
+ # Prefer the content in Python object format over the serialized content from the frontend,
+ # since this is the most accurate representation of the message and more valuable to the user in Python-land.
+ content = (
+ prev_content
+ if prev_content is not None
+ else msg.get("content")
+ )
+ result.append(
+ ChatMessage.create(
+ role=role,
+ message_id=msg_id,
+ content=content,
+ parts=msg.get("parts", []),
+ part_validator_class=part_validator_class,
+ )
)
- for msg in messages
- ]
+ return result
@dataclass
diff --git a/marimo/_smoke_tests/ai/chat-variations.py b/marimo/_smoke_tests/ai/chat-variations.py
index 959e7e127a..6de360a3aa 100644
--- a/marimo/_smoke_tests/ai/chat-variations.py
+++ b/marimo/_smoke_tests/ai/chat-variations.py
@@ -52,6 +52,12 @@ def _(chat):
return
+def _(chat):
+ chat.value[1].content if len(chat.value) else None
+ return
+
+
@app.cell
def _(uuid, vercel):
async def async_streaming_chunks(messages, config):
diff --git a/tests/_plugins/ui/_impl/chat/test_chat.py b/tests/_plugins/ui/_impl/chat/test_chat.py
index f7b07376ba..f4c76bfe11 100644
--- a/tests/_plugins/ui/_impl/chat/test_chat.py
+++ b/tests/_plugins/ui/_impl/chat/test_chat.py
@@ -28,6 +28,48 @@ if TYPE_CHECKING:
from collections.abc import AsyncIterator
+def assert_single_message(
+ sent_messages: list[dict],
+ expected_delta: str,
+ *,
+ use_contains: bool = False,
+) -> None:
+ """Helper to assert the standard 4-message pattern for non-streaming responses.
+
+ Args:
+ sent_messages: List of messages sent via _send_message
+ expected_delta: The expected text in the text-delta message
+ use_contains: If True, check if expected_delta is contained in the delta,
+ otherwise check for exact equality
+ """
+ # Verify proper messages were sent: text-start, text-delta, text-end, final
+ assert len(sent_messages) == 4
+
+ # Message 0: text-start
+ assert sent_messages[0]["type"] == "stream_chunk"
+ assert sent_messages[0]["content"]["type"] == "text-start"
+ assert sent_messages[0]["is_final"] is False
+
+ # Message 1: text-delta
+ assert sent_messages[1]["type"] == "stream_chunk"
+ assert sent_messages[1]["content"]["type"] == "text-delta"
+ if use_contains:
+ assert expected_delta in sent_messages[1]["content"]["delta"]
+ else:
+ assert sent_messages[1]["content"]["delta"] == expected_delta
+ assert sent_messages[1]["is_final"] is False
+
+ # Message 2: text-end
+ assert sent_messages[2]["type"] == "stream_chunk"
+ assert sent_messages[2]["content"]["type"] == "text-end"
+ assert sent_messages[2]["is_final"] is False
+
+ # Message 3: final
+ assert sent_messages[3]["type"] == "stream_chunk"
+ assert sent_messages[3]["content"] is None
+ assert sent_messages[3]["is_final"] is True
+
+
def test_chat_init():
def mock_model(
messages: list[ChatMessage], config: ChatModelConfig
@@ -84,13 +126,26 @@ async def test_chat_send_prompt():
return f"Response to: {content}"
chat = ui.chat(mock_model)
+
+ sent_messages: list[dict] = []
+
+ def capture_send_message(message: dict, buffers): # noqa: ARG001
+ sent_messages.append(message)
+
+ chat._send_message = capture_send_message
+
request = SendMessageRequest(
messages=[ChatMessage(role="user", content="Hello")],
config=ChatModelConfig(),
)
response = await chat._send_prompt(request)
- assert response == "Response to: Hello"
+ # Non-streaming now also returns None
+ assert response is None
+
+ # Verify proper messages were sent
+ assert_single_message(sent_messages, "Response to: Hello")
+
assert len(chat._chat_history) == 2
assert chat._chat_history[0].role == "user"
assert chat._chat_history[0].content == "Hello"
@@ -107,13 +162,26 @@ async def test_chat_send_prompt_async_function():
return f"Response to: {messages[-1].content}"
chat = ui.chat(mock_model)
+
+ sent_messages: list[dict] = []
+
+ def capture_send_message(message: dict, buffers): # noqa: ARG001
+ sent_messages.append(message)
+
+ chat._send_message = capture_send_message
+
request = SendMessageRequest(
messages=[ChatMessage(role="user", content="Hello")],
config=ChatModelConfig(),
)
response = await chat._send_prompt(request)
- assert response == "Response to: Hello"
+ # Non-streaming now also returns None
+ assert response is None
+
+ # Verify proper messages were sent
+ assert_single_message(sent_messages, "Response to: Hello")
+
assert len(chat._chat_history) == 2
assert chat._chat_history[0].role == "user"
assert chat._chat_history[0].content == "Hello"
@@ -1054,3 +1122,353 @@ async def test_streaming_with_dict_chunks():
"is_final": True,
},
]
+
+
+async def test_chat_value_sync_non_generator():
+ """Test chat.value with sync model returning text (non-generator)."""
+
+ def mock_model(
+ messages: list[ChatMessage], config: ChatModelConfig
+ ) -> str:
+ del config
+ return f"Response to: {messages[-1].content}"
+
+ chat = ui.chat(mock_model)
+ assert chat.value == []
+
+ sent_messages: list[dict] = []
+
+ def capture_send_message(message: dict, buffers): # noqa: ARG001
+ sent_messages.append(message)
+
+ chat._send_message = capture_send_message
+
+ request = SendMessageRequest(
+ messages=[ChatMessage(role="user", content="Hello")],
+ config=ChatModelConfig(),
+ )
+ response = await chat._send_prompt(request)
+
+ # Non-streaming now also returns None
+ assert response is None
+
+ # Verify proper messages were sent
+ assert_single_message(sent_messages, "Response to: Hello")
+
+ # Verify chat.value contains both user and assistant messages
+ assert len(chat.value) == 2
+ assert chat.value[0].role == "user"
+ assert chat.value[0].content == "Hello"
+ assert chat.value[1].role == "assistant"
+ assert chat.value[1].content == "Response to: Hello"
+
+
+async def test_chat_value_sync_non_generator_with_rich_object():
+ """Test chat.value with sync model returning rich object (non-generator)."""
+
+ class RichObject:
+ def __init__(self, text: str):
+ self.text = text
+
+ def __str__(self):
+ return f"Response to: {self.text}"
+
+ def mock_model(
+ messages: list[ChatMessage], config: ChatModelConfig
+ ) -> RichObject:
+ del config, messages
+ return RichObject("Hello")
+
+ chat = ui.chat(mock_model)
+ assert chat.value == []
+
+ sent_messages: list[dict] = []
+
+ def capture_send_message(message: dict, buffers): # noqa: ARG001
+ sent_messages.append(message)
+
+ chat._send_message = capture_send_message
+
+ request = SendMessageRequest(
+ messages=[ChatMessage(role="user", content="Hello", id="msg-1")],
+ config=ChatModelConfig(),
+ )
+ response = await chat._send_prompt(request)
+
+ # Non-streaming now also returns None
+ assert response is None
+
+ # Verify proper messages were sent (rich object is converted to HTML)
+ assert_single_message(
+ sent_messages, "<span>Response to: Hello</span>", use_contains=True
+ )
+
+ # Verify chat.value contains both user and assistant messages
+ assert len(chat.value) == 2
+ assert chat.value[0].role == "user"
+ assert chat.value[0].content == "Hello"
+ assert chat.value[1].role == "assistant"
+ assert isinstance(chat.value[1].content, RichObject)
+
+ msg_id_1 = chat.value[0].id
+ msg_id_2 = chat.value[1].id
+ assert msg_id_1
+ assert msg_id_2
+
+ # Simulate the frontend sending back the message
+ converted = chat._convert_value(
+ {
+ "messages": [
+ {
+ "id": msg_id_1,
+ "role": "user",
+ "content": "Hello",
+ },
+ {
+ "id": msg_id_2,
+ "role": "assistant",
+ "content": "Response to: Hello",
+ },
+ ]
+ }
+ )
+
+ # Verify chat.value still contains the rich object
+ assert len(converted) == 2
+ assert converted[0].role == "user"
+ assert converted[0].content == "Hello"
+ assert converted[0].id == msg_id_1
+ assert converted[1].role == "assistant"
+ assert converted[1].id == msg_id_2
+ assert isinstance(converted[1].content, RichObject)
+
+
+async def test_chat_value_async_non_generator():
+ """Test chat.value with async model returning text (non-generator)."""
+
+ async def mock_model(
+ messages: list[ChatMessage], config: ChatModelConfig
+ ) -> str:
+ del config
+ await asyncio.sleep(0.01)
+ return f"Async response to: {messages[-1].content}"
+
+ chat = ui.chat(mock_model)
+ assert chat.value == []
+
+ sent_messages: list[dict] = []
+
+ def capture_send_message(message: dict, buffers): # noqa: ARG001
+ sent_messages.append(message)
+
+ chat._send_message = capture_send_message
+
+ request = SendMessageRequest(
+ messages=[ChatMessage(role="user", content="Test message")],
+ config=ChatModelConfig(),
+ )
+ response = await chat._send_prompt(request)
+
+ # Non-streaming now also returns None
+ assert response is None
+
+ # Verify proper messages were sent
+ assert_single_message(sent_messages, "Async response to: Test message")
+
+ # Verify chat.value contains both user and assistant messages
+ assert len(chat.value) == 2
+ assert chat.value[0].role == "user"
+ assert chat.value[0].content == "Test message"
+ assert chat.value[1].role == "assistant"
+ assert chat.value[1].content == "Async response to: Test message"
+
+
+async def test_chat_value_sync_generator_text():
+ """Test chat.value with sync generator yielding text chunks."""
+
+ def mock_streaming_model(
+ messages: list[ChatMessage], config: ChatModelConfig
+ ):
+ del messages, config
+ yield "Hello"
+ yield " "
+ yield "world"
+
+ chat = ui.chat(mock_streaming_model)
+ assert chat.value == []
+
+ # Mock _send_message to avoid needing kernel context
+ chat._send_message = lambda message, buffers: None # noqa: ARG005
+
+ request = SendMessageRequest(
+ messages=[ChatMessage(role="user", content="Stream this")],
+ config=ChatModelConfig(),
+ )
+ response = await chat._send_prompt(request)
+
+ # Streaming returns None
+ assert response is None
+
+ # Verify chat._chat_history contains the user message
+ # (streaming responses are managed by frontend, so only user message is in backend history)
+ assert len(chat._chat_history) == 1
+ assert chat._chat_history[0].role == "user"
+ assert chat._chat_history[0].content == "Stream this"
+
+ # Verify value is empty (until the frontend sends back the message)
+ assert chat.value == []
+
+
+async def test_chat_value_async_generator_text():
+ """Test chat.value with async generator yielding text chunks."""
+
+ async def mock_streaming_model(
+ messages: list[ChatMessage], config: ChatModelConfig
+ ):
+ del messages, config
+ for word in ["Async", " ", "streaming"]:
+ await asyncio.sleep(0.001)
+ yield word
+
+ chat = ui.chat(mock_streaming_model)
+ assert chat.value == []
+
+ # Mock _send_message to avoid needing kernel context
+ chat._send_message = lambda message, buffers: None # noqa: ARG005
+
+ request = SendMessageRequest(
+ messages=[ChatMessage(role="user", content="Stream async")],
+ config=ChatModelConfig(),
+ )
+ response = await chat._send_prompt(request)
+
+ # Streaming returns None
+ assert response is None
+
+ # Verify chat._chat_history contains the user message
+ # (streaming responses are managed by frontend, so only user message is in backend history)
+ assert len(chat._chat_history) == 1
+ assert chat._chat_history[0].role == "user"
+ assert chat._chat_history[0].content == "Stream async"
+
+ # Verify value is empty (until the frontend sends back the message)
+ assert chat.value == []
+
+
+async def test_chat_value_sync_generator_dicts():
+ """Test chat.value with sync generator yielding dict chunks."""
+
+ def mock_streaming_model(
+ messages: list[ChatMessage], config: ChatModelConfig
+ ):
+ del messages, config
+ yield {"type": "text-start", "id": "text-1"}
+ yield {"type": "text-delta", "id": "text-1", "delta": "Dict"}
+ yield {"type": "text-delta", "id": "text-1", "delta": " chunks"}
+ yield {"type": "text-end", "id": "text-1"}
+
+ chat = ui.chat(mock_streaming_model)
+ assert chat.value == []
+
+ # Mock _send_message to avoid needing kernel context
+ chat._send_message = lambda message, buffers: None # noqa: ARG005
+
+ request = SendMessageRequest(
+ messages=[ChatMessage(role="user", content="Send dicts")],
+ config=ChatModelConfig(),
+ )
+ response = await chat._send_prompt(request)
+
+ # Streaming returns None
+ assert response is None
+
+ # Verify chat._chat_history contains the user message
+ # (streaming responses with dicts are managed by frontend)
+ assert len(chat._chat_history) == 1
+ assert chat._chat_history[0].role == "user"
+ assert chat._chat_history[0].content == "Send dicts"
+
+ # Verify value is empty (until the frontend sends back the message)
+ assert chat.value == []
+
+
+async def test_chat_value_async_generator_dicts():
+ """Test chat.value with async generator yielding dict chunks."""
+
+ async def mock_streaming_model(
+ messages: list[ChatMessage], config: ChatModelConfig
+ ):
+ del messages, config
+ chunks = [
+ {"type": "text-start", "id": "text-1"},
+ {"type": "text-delta", "id": "text-1", "delta": "Async"},
+ {"type": "text-delta", "id": "text-1", "delta": " dict"},
+ {"type": "text-end", "id": "text-1"},
+ ]
+ for chunk in chunks:
+ await asyncio.sleep(0.001)
+ yield chunk
+
+ chat = ui.chat(mock_streaming_model)
+ assert chat.value == []
+
+ # Mock _send_message to avoid needing kernel context
+ chat._send_message = lambda message, buffers: None # noqa: ARG005
+
+ request = SendMessageRequest(
+ messages=[ChatMessage(role="user", content="Send async dicts")],
+ config=ChatModelConfig(),
+ )
+ response = await chat._send_prompt(request)
+
+ # Streaming returns None
+ assert response is None
+
+ # Verify chat._chat_history contains the user message
+ # (streaming responses with dicts are managed by frontend)
+ assert len(chat._chat_history) == 1
+ assert chat._chat_history[0].role == "user"
+ assert chat._chat_history[0].content == "Send async dicts"
+
+ # Verify value is empty (until the frontend sends back the message)
+ assert chat.value == []
+
+
+async def test_chat_value_multiple_exchanges():
+ """Test chat.value accumulates messages across multiple exchanges."""
+
+ def mock_model(
+ messages: list[ChatMessage], config: ChatModelConfig
+ ) -> str:
+ del config
+ # Echo back the last user message
+ return f"Echo: {messages[-1].content}"
+
+ chat = ui.chat(mock_model)
+ assert chat.value == []
+
+ # First exchange
+ request1 = SendMessageRequest(
+ messages=[ChatMessage(role="user", content="First")],
+ config=ChatModelConfig(),
+ )
+ await chat._send_prompt(request1)
+
+ assert len(chat.value) == 2
+ assert chat.value[0].content == "First"
+ assert chat.value[1].content == "Echo: First"
+
+ # Second exchange - chat history accumulates
+ request2 = SendMessageRequest(
+ messages=[
+ ChatMessage(role="user", content="First"),
+ ChatMessage(role="assistant", content="Echo: First"),
+ ChatMessage(role="user", content="Second"),
+ ],
+ config=ChatModelConfig(),
+ )
+ await chat._send_prompt(request2)
+
+ assert len(chat.value) == 4
+ assert chat.value[2].content == "Second"
+ assert chat.value[3].content == "Echo: Second"