Skip to main content

mo.ui.chat

Create an AI-powered chat interface with conversation history.

Signature

mo.ui.chat(
    model: Callable[[list[ChatMessage], ChatModelConfig], object],
    *,
    prompts: list[str] | None = None,
    on_message: Callable[[list[ChatMessage]], None] | None = None,
    show_configuration_controls: bool = False,
    config: ChatModelConfigDict | None = None,
    allow_attachments: bool | list[str] = False,
    max_height: int | None = None,
    disabled: bool = False
)

Parameters

model
Callable
required
Function that takes messages and config, returns response
prompts
list[str]
Suggested prompts displayed to user
on_message
Callable
Callback when new message is sent
show_configuration_controls
bool
default:"False"
Show temperature/tokens controls
config
ChatModelConfigDict
Default model configuration
allow_attachments
bool | list[str]
default:"False"
Allow file attachments (True for all types, or list of extensions)
max_height
int
Maximum height in pixels
disabled
bool
default:"False"
Whether chat is disabled

Examples

import marimo as mo
from pydantic_ai import Agent

# Create agent
agent = Agent("openai:gpt-4")

# Create chat interface
chat = mo.ui.chat(
    agent,
    prompts=[
        "Explain quantum computing",
        "Write a Python function",
    ]
)
chat
# Custom model function
def my_model(messages, config):
    # Process messages and return response
    last_message = messages[-1].content
    return f"You said: {last_message}"

chat = mo.ui.chat(
    my_model,
    show_configuration_controls=True
)
# With attachments
chat = mo.ui.chat(
    agent,
    allow_attachments=[".pdf", ".txt", ".md"]
)

ChatMessage Structure

Messages have:
  • role: “user” or “assistant”
  • content: Message text
  • attachments: List of attached files
  • timestamp: When message was sent
Use chat.value to access the full conversation history.

Build docs developers (and LLMs) love