Skip to content

Sessions & persistence

Every conversation is stored, with a small dict of state you control. Read this page to keep per-conversation state, choose where conversations are stored, or plug in your own database.

The snippets below are fragments; the complete example is a whole file.

chat.session is a dict stored with the conversation. Read and write it freely. It’s saved when the handler finishes, so values must be JSON-serializable.

@app.on_message
async def reply(msg, chat):
count = chat.session.get("turns", 0) + 1
chat.session["turns"] = count
yield f"This is message {count} in this conversation."

DyUI has no user accounts. Each browser gets an anonymous dyui_uid cookie (random, HttpOnly, SameSite=Lax), and conversations, files and actions are scoped to it. chat.user_id exposes it. To restrict who can open the app, see Authentication.

App(persist=...) Store
True (default) SQLite at <data_dir>/dyui.db (./.dyui/dyui.db)
"path/to/chats.db" SQLite at that path
False In memory; everything is lost on restart
a Store instance Yours

Uploaded files are written to <data_dir>/uploads/, and their metadata goes in the store.

Implement the dyui.store.Store protocol and pass an instance as persist=. Every method is a coroutine, and records are plain dicts:

from dyui.store import Store
class PostgresStore: # implements dyui.store.Store
async def create_thread(self, user_id, *, title="New chat"): ...
async def get_thread(self, thread_id): ...
async def list_threads(self, user_id, *, limit=200): ...
async def update_thread(self, thread_id, **fields): ... # title, title_locked, session, updated_at
async def delete_thread(self, thread_id): ...
async def get_messages(self, thread_id): ...
async def get_message(self, thread_id, message_id): ...
async def put_message(self, thread_id, message): ... # insert or replace by id, keep position
async def delete_messages_after(self, thread_id, message_id, *, inclusive=False): ...
async def put_file(self, record): ...
async def get_file(self, file_id): ...
async def close(self): ...
app = dyui.App(persist=PostgresStore())

The record shapes are documented on dyui.store.Store. dyui.store.MemoryStore is a short, complete reference implementation.

A counter kept in chat.session, stored in an SQLite file you name. It needs no API key.

app.py
"""Per-conversation state with chat.session, stored in SQLite.
Save as app.py, then run: dyui run app.py
Conversations are kept in ./chats.db; restart the app and they are still there.
"""
import dyui
from dyui import ui
app = dyui.App(title="Counter", persist="chats.db", starters=["Count this"])
@app.on_message
async def reply(msg: dyui.Message, chat: dyui.Chat):
count = chat.session.get("turns", 0) + 1
chat.session["turns"] = count # saved when the handler finishes; values must be JSON
chat.session.setdefault("words", []).append(msg.text)
yield f"This is message {count} in this conversation."
yield ui.KeyValue({"Thread": chat.thread_id, "Browser": chat.user_id[:8] + "…", "Turns": count})
  1. Save it as app.py in a new, empty file, or download app.py.

  2. Run it:

    Terminal window
    pip install dyui
    dyui run app.py
  3. Open http://localhost:8000 and send a few messages. Stop the app, start it again, and the conversation and its count are still there.

DyUI Docs assistant

Answers come from the DyUI docs. gpt-oss-120b via Groq. Press Escape or the close button to return to the page.