marimo icon indicating copy to clipboard operation
marimo copied to clipboard

Add streaming support for chat: async generator bug and add documentation

Open adamwdraper opened this issue 2 weeks ago • 3 comments

Summary

This PR adds real-time streaming support to mo.ui.chat, enabling ChatGPT-like experiences where responses appear word-by-word as they're generated.

What's New

✨ Streaming Support for Chat Models

Users can now stream responses from chat models in real-time:

With built-in models (OpenAI, etc.):

chat = mo.ui.chat(
    mo.ai.llm.openai(
        "gpt-4o",
        stream=True,  # Enable streaming!
    )
)

With custom models using async generators:

async def streaming_model(messages, config):
    for word in words:
        accumulated += word + " "
        yield accumulated  # Each yield updates the UI
        await asyncio.sleep(0.1)

chat = mo.ui.chat(streaming_model)

📚 Documentation

  • Added comprehensive "Streaming Responses" section to chat API docs
  • Explains both built-in model streaming and custom async generator approach
  • Includes links to working examples (streaming_openai.py and streaming_custom.py)

🎯 Examples

  • streaming_openai.py: Demonstrates streaming with OpenAI models
  • streaming_custom.py: Shows how to build custom streaming chatbots
  • Fixed quote escaping in examples to prevent file corruption on auto-save

Implementation Details

  • Added stream parameter to openai ChatModel class
  • Implemented async generator support in chat UI element
  • Frontend receives incremental updates via stream_chunk messages
  • Final message marked with is_final: true flag

Testing

  • Added test_chat_streaming_sends_messages to verify streaming behavior
  • Manually tested both OpenAI and custom streaming examples
  • Code passes lint and format checks

User Experience

Users will see responses appear progressively in the chat interface, creating a more engaging and responsive experience similar to ChatGPT, Claude, and other modern AI chat interfaces.

adamwdraper avatar Nov 16 '25 19:11 adamwdraper

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
marimo-docs Ready Ready Preview Comment Nov 19, 2025 4:30am

vercel[bot] avatar Nov 16 '25 19:11 vercel[bot]

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

github-actions[bot] avatar Nov 16 '25 19:11 github-actions[bot]

I have read the CLA Document and I hereby sign the CLA

adamwdraper avatar Nov 16 '25 21:11 adamwdraper

Additional UI Improvement: Chat Reset Icon Change

Changed the chat reset button icon from trash to rotate-clockwise to improve UX clarity.

Why this change? The trash icon was confusing as it looked like it would delete the cell itself rather than reset the conversation. The rotate icon better represents the "reset/restart" action.

Additional improvements:

  • Reset button is now disabled when there are no messages (consistent with send button behavior)
  • Individual message delete buttons still use the trash icon appropriately

See commit: 4b987ea12

adamwdraper avatar Nov 18 '25 16:11 adamwdraper

@adamwdraper looks great! few build errors to fix, but otherwise good to merge afterwards.

we can followup as a team to figure out the correct default for stream

mscolnick avatar Nov 18 '25 20:11 mscolnick

@adamwdraper looks great! few build errors to fix, but otherwise good to merge afterwards.

we can followup as a team to figure out the correct default for stream

@mscolnick i tried to fix the build issues, but it seems like they are waiting for approval to run

adamwdraper avatar Nov 19 '25 21:11 adamwdraper