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.

accumulate text for custom models with string content (#7853)

Shahmir Varqhaf1aeb8143226
diff --git a/marimo/_plugins/ui/_impl/chat/chat.py b/marimo/_plugins/ui/_impl/chat/chat.py
index 40b11e1514..5919c9792c 100644
--- a/marimo/_plugins/ui/_impl/chat/chat.py
+++ b/marimo/_plugins/ui/_impl/chat/chat.py
@@ -11,6 +11,7 @@ from marimo._ai._types import (
ChatMessage,
ChatModelConfig,
ChatModelConfigDict,
+ TextPart,
)
from marimo._dependencies.dependencies import DependencyManager
from marimo._output.formatting import as_html
@@ -273,32 +274,45 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):
)
async def _handle_streaming_response(self, response: Any) -> None:
- """Handle streaming from both sync and async generators.
+ """Handle streaming from both sync and async generators, and lists.
Generators should yield delta chunks (new content only), which this
method accumulates and sends to the frontend as complete text.
This follows the standard streaming pattern used by OpenAI, Anthropic,
and other AI providers. For frontend-managed streaming, the response is set on the frontend,
- so we don't need to return anything.
+ so we don't need to return anything. If generators just yield strings, we update the chat history with the accumulated text.
"""
message_id = str(uuid.uuid4())
def send_chunk(chunk: dict[str, Any]) -> None:
self._send_chat_message(
- message_id=message_id,
- content=chunk,
- is_final=False,
+ message_id=message_id, content=chunk, is_final=False
)
serializer = ChunkSerializer(on_send_chunk=send_chunk)
+ accumulated_text = ""
+
+ is_generator = inspect.isasyncgen(response) or inspect.isgenerator(
+ response
+ )
if inspect.isasyncgen(response):
async for delta in response:
+ if isinstance(delta, str):
+ accumulated_text += delta
serializer.handle_chunk(delta)
else:
for delta in response:
+ if isinstance(delta, str):
+ accumulated_text += delta
serializer.handle_chunk(delta)
+ # Generators that yield strings should update the 'content' field of the assistant message
+ if accumulated_text and is_generator:
+ self._add_assistant_message_to_chat_history(
+ accumulated_text, accumulated_text
+ )
+
serializer.on_end()
# Send final message to indicate streaming is complete
@@ -367,20 +381,22 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):
response if isinstance(response, str) else as_html(response).text
)
- # Add assistant response to chat history
+ await self._handle_streaming_response([response_str])
+ # Update the chat history to trigger UI updates and on_message callback
+ self._add_assistant_message_to_chat_history(response, response_str)
+
+ def _add_assistant_message_to_chat_history(
+ self, content: str | object, text: str
+ ) -> None:
assistant_message = ChatMessage(
role="assistant",
- content=response,
+ content=content,
id=f"message_{uuid.uuid4().hex}",
+ parts=[TextPart(type="text", text=text)],
)
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 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:
diff --git a/tests/_plugins/ui/_impl/chat/test_chat.py b/tests/_plugins/ui/_impl/chat/test_chat.py
index f4c76bfe11..505d4f949a 100644
--- a/tests/_plugins/ui/_impl/chat/test_chat.py
+++ b/tests/_plugins/ui/_impl/chat/test_chat.py
@@ -222,6 +222,12 @@ async def test_chat_send_prompt_async_generator():
# Check final message is marked as final
assert sent_messages[-1]["is_final"] is True
+ # String generators now update chat history with accumulated text
+ assert len(chat._chat_history) == 2
+ assert chat._chat_history[0].role == "user"
+ assert chat._chat_history[1].role == "assistant"
+ assert chat._chat_history[1].content == "012"
+
async def test_chat_streaming_sends_messages():
"""Test that streaming async generators send messages via _send_message"""
@@ -269,6 +275,12 @@ async def test_chat_streaming_sends_messages():
assert sent_messages[-1]["is_final"]
assert sent_messages[-1]["content"] is None
+ # String generators now update chat history with accumulated text
+ assert len(chat._chat_history) == 2
+ assert chat._chat_history[0].role == "user"
+ assert chat._chat_history[1].role == "assistant"
+ assert chat._chat_history[1].content == "Hello world !"
+
async def test_chat_sync_generator_streaming():
"""Test that sync generators also work for streaming"""
@@ -314,6 +326,12 @@ async def test_chat_sync_generator_streaming():
assert sent_messages[-1]["is_final"]
assert sent_messages[-1]["content"] is None
+ # String generators now update chat history with accumulated text
+ assert len(chat._chat_history) == 2
+ assert chat._chat_history[0].role == "user"
+ assert chat._chat_history[1].role == "assistant"
+ assert chat._chat_history[1].content == "Hello world !"
+
async def test_chat_streaming_complete_response():
"""Test that streaming sends all chunks correctly"""
@@ -349,6 +367,12 @@ async def test_chat_streaming_complete_response():
assert len(sent_messages) >= 4 # text-start + 3 deltas + text-end + final
assert sent_messages[-1]["is_final"]
+ # String generators now update chat history with accumulated text
+ assert len(chat._chat_history) == 2
+ assert chat._chat_history[0].role == "user"
+ assert chat._chat_history[1].role == "assistant"
+ assert chat._chat_history[1].content == "Hello world!"
+
def test_chat_get_history():
def mock_model(
@@ -1309,14 +1333,18 @@ async def test_chat_value_sync_generator_text():
# 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
+ # String generators now update chat history with accumulated text
+ assert len(chat._chat_history) == 2
assert chat._chat_history[0].role == "user"
assert chat._chat_history[0].content == "Stream this"
+ assert chat._chat_history[1].role == "assistant"
+ assert chat._chat_history[1].content == "Hello world"
- # Verify value is empty (until the frontend sends back the message)
- assert chat.value == []
+ # Verify value also contains both messages
+ assert len(chat.value) == 2
+ assert chat.value[0].role == "user"
+ assert chat.value[1].role == "assistant"
+ assert chat.value[1].content == "Hello world"
async def test_chat_value_async_generator_text():
@@ -1345,14 +1373,18 @@ async def test_chat_value_async_generator_text():
# 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
+ # String generators now update chat history with accumulated text
+ assert len(chat._chat_history) == 2
assert chat._chat_history[0].role == "user"
assert chat._chat_history[0].content == "Stream async"
+ assert chat._chat_history[1].role == "assistant"
+ assert chat._chat_history[1].content == "Async streaming"
- # Verify value is empty (until the frontend sends back the message)
- assert chat.value == []
+ # Verify value also contains both messages
+ assert len(chat.value) == 2
+ assert chat.value[0].role == "user"
+ assert chat.value[1].role == "assistant"
+ assert chat.value[1].content == "Async streaming"
async def test_chat_value_sync_generator_dicts():