Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: stream durable chat events without re-parsing stored payloads (#16159)
Daniel Han6cd1423cb42d
diff --git a/studio/backend/routes/chat_generation_runs.py b/studio/backend/routes/chat_generation_runs.pyindex d6b0e24b901..2f6ae35c3d4 100644--- a/studio/backend/routes/chat_generation_runs.py+++ b/studio/backend/routes/chat_generation_runs.py@@ -33,6 +33,13 @@ from utils.current_date_prompt_settings import ()router = APIRouter()+++def _encode_json(value: Any) -> str:+ """The SSE frame encoding; chat_generation_runs_db stores event payloads with the same options."""+ return json.dumps(value, ensure_ascii = False, separators = (",", ":"))++_EVENT_WAIT_EXECUTOR = ThreadPoolExecutor(max_workers = 32,thread_name_prefix = "chat-generation-events",@@ -447,6 +454,7 @@ async def chat_generation_events(run_id,cursor,15,+ True,)# Only non-chunk events carry the run, so a chunk-only batch skips parsing request_json.snapshot = await asyncio.to_thread(@@ -457,18 +465,21 @@ async def chat_generation_events()if snapshot is None:return+ run_json = Nonefor event in events:cursor = int(event["seq"])- data = {- "seq": cursor,- "type": event["type"],- "payload": event["payload"],- "createdAt": event["createdAt"],- }- if event["type"] != "chunk":- data["run"] = snapshot- encoded = json.dumps(data, ensure_ascii = False, separators = (",", ":"))- yield f"id: {cursor}\nevent: {event['type']}\ndata: {encoded}\n\n"+ event_type = event["type"]+ # Spliced, not re-encoded: the stored payload is already this encoder's output, so the+ # bytes match json.dumps of the parsed event dict with the same key order.+ encoded = (+ f'{{"seq":{cursor},"type":{_encode_json(event_type)},'+ f'"payload":{event["payloadJson"]},"createdAt":{int(event["createdAt"])}'+ )+ if event_type != "chunk":+ if run_json is None:+ run_json = _encode_json(snapshot)+ encoded += f',"run":{run_json}'+ yield f"id: {cursor}\nevent: {event_type}\ndata: {encoded}}}\n\n"if snapshot["status"] in db.TERMINAL_STATUSES and cursor >= int(snapshot["lastEventSeq"]):diff --git a/studio/backend/storage/chat_generation_runs_db.py b/studio/backend/storage/chat_generation_runs_db.pyindex 77abc5db119..8b002944876 100644--- a/studio/backend/storage/chat_generation_runs_db.py+++ b/studio/backend/storage/chat_generation_runs_db.py@@ -13,6 +13,7 @@ import sqlite3import threadingimport timeimport weakref+from functools import partialfrom pathlib import Pathfrom typing import Any, Iterable, Union@@ -276,6 +277,16 @@ def _prepare_connection() -> tuple[sqlite3.Connection, bool]:return conn, db_path.resolve() in _schema_ready+_SPLICE_OK_SQL = (+ "json_valid(payload_json) AND instr(payload_json, char(10))=0"+ " AND instr(payload_json, char(13))=0"+)+++def _dumps_payload(payload: Any) -> str:+ return json.dumps(payload, ensure_ascii = False, separators = (",", ":"))++def _loads(value: str | None, fallback: Any) -> Any:if value is None:return fallback@@ -366,7 +377,8 @@ def _append_events_locked(run_id,seq,event_type,- json.dumps(payload, ensure_ascii = False, separators = (",", ":")),+ # The events route splices this text into SSE frames as is, so it must stay its encoding.+ _dumps_payload(payload),created,),)@@ -932,9 +944,38 @@ def list_events(run_id: str,after: int = 0,limit: int = 1000,+ *,+ raw: bool = False,) -> list[dict[str, Any]]:+ """raw=True returns the stored text as "payloadJson": it is already _dumps_payload output, the SSE+ route's encoding. Rows that are not one line of valid JSON (corrupt, hand edited, NaN) are+ re-encoded so the route never splices text the client cannot parse or that splits the frame."""+ global _SPLICE_OK_SQLconn = _connect()try:+ if raw:+ query = """SELECT seq, event_type, payload_json, created_at, {} AS splice_ok+ FROM chat_generation_events+ WHERE run_id=? AND seq>? ORDER BY seq LIMIT ?"""+ try:+ rows = conn.execute(query.format(_SPLICE_OK_SQL), (run_id, after, limit)).fetchall()+ except sqlite3.OperationalError as exc:+ # SQLite built without JSON1: re-encode every row, as the parsed read does.+ if "json_valid" not in str(exc):+ raise+ _SPLICE_OK_SQL = "0"+ rows = conn.execute(query.format("0"), (run_id, after, limit)).fetchall()+ return [+ {+ "seq": int(row["seq"]),+ "type": row["event_type"],+ "payloadJson": row["payload_json"]+ if row["splice_ok"]+ else _dumps_payload(_loads(row["payload_json"], {})),+ "createdAt": int(row["created_at"]),+ }+ for row in rows+ ]rows = conn.execute("""SELECT seq, event_type, payload_json, created_atFROM chat_generation_events@@ -958,19 +999,21 @@ def wait_for_events(run_id: str,after: int = 0,timeout: float = 15,+ raw: bool = False,) -> list[dict[str, Any]]:+ read = partial(list_events, raw = True) if raw else list_events# Bumped under the lock after each notifying commit: unchanged means the unlocked read saw them all.seen = _EVENTS_COMMITS- events = list_events(run_id, after)+ events = read(run_id, after)if events:return eventswith _EVENTS_CHANGED:if _EVENTS_COMMITS != seen:- events = list_events(run_id, after)+ events = read(run_id, after)if events:return events_EVENTS_CHANGED.wait(timeout)- return list_events(run_id, after)+ return read(run_id, after)def reconcile_runs(diff --git a/studio/backend/tests/test_chat_generation_events_passthrough.py b/studio/backend/tests/test_chat_generation_events_passthrough.pynew file mode 100644index 00000000000..7cfbde06382--- /dev/null+++ b/studio/backend/tests/test_chat_generation_events_passthrough.py@@ -0,0 +1,322 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The events route splices stored payload_json into frames; these pin it to the old parse-and-encode bytes."""++import asyncio+import json+import random+import threading+import time+from types import SimpleNamespace+from unittest.mock import AsyncMock++import pytest++from routes import chat_generation_runs as run_routes+from storage import chat_generation_runs_db as runs_db+from storage import studio_db++PAYLOADS = [+ {"choices": [{"index": 0, "delta": {"content": "Hello"}, "finish_reason": None}]},+ {"choices": [{"delta": {"reasoning_content": "naïve 数据 \U0001f600"}}]},+ {"text": 'quote " backslash \\ slash / tab \t nl \n cr \r nul \x00 bell \x07 del \x7f'},+ {"text": "line sep para sep nel \x85 bom "},+ {"floats": [0.1, -0.0, 1e300, 1e-300, 3.141592653589793, float("inf"), float("-inf")]},+ {"ints": [0, -1, 2**63, -(2**80), 10**200], "flags": [True, False, None]},+ {"nested": {"a": [{"b": [[], {}, ""]}], "z": {"y": {"x": "deep"}}}},+ {},+ {+ "type": "tool_end",+ "tool_name": "web_fetch",+ "tool_call_id": "call-1",+ "result": "page éè " * 20000,+ "provenance": "studio",+ },+ {"_toolEvent": {"type": "tool_start", "arguments": {"url": "https://example.com/?q=a&b=<c>"}}},+]+++def running_run():+ studio_db.upsert_chat_thread(+ {"id": "thread-1", "title": "Chat", "modelType": "base", "modelId": "local", "createdAt": 1}+ )+ studio_db.upsert_chat_message(+ {+ "id": "user-1",+ "threadId": "thread-1",+ "role": "user",+ "content": [{"type": "text", "text": "Hello"}],+ "createdAt": 2,+ }+ )+ runs_db.create_run(+ run_id = "run-1",+ owner_subject = "alice",+ thread_id = "thread-1",+ user_message_id = "user-1",+ assistant_message_id = "assistant-1",+ request_payload = {+ "model": "local",+ "messages": [{"role": "user", "content": "héllo \U0001f600 "}],+ "stream": True,+ },+ )+ token = runs_db.get_worker_token("run-1")+ assert runs_db.mark_running("run-1", token) is True+ return token+++async def _stream(after = 0, last_event_id = None):+ response = await run_routes.chat_generation_events(+ "run-1",+ SimpleNamespace(is_disconnected = AsyncMock(return_value = False)),+ after = after,+ last_event_id = last_event_id,+ current_subject = "alice",+ )+ raw = ""+ async for part in response.body_iterator:+ raw += part.decode() if isinstance(part, bytes) else part+ return raw+++def _frames(raw):+ """Event frames only; keep-alive comments depend on wake timing, not on the events."""+ return [block + "\n\n" for block in raw.split("\n\n") if block and not block.startswith(":")]+++def _old_frame(event, run = None):+ """The frame as the route encoded it before: the parsed payload re-encoded in one json.dumps."""+ data = {+ "seq": event["seq"],+ "type": event["type"],+ "payload": event["payload"],+ "createdAt": event["createdAt"],+ }+ if event["type"] != "chunk":+ data["run"] = run+ encoded = json.dumps(data, ensure_ascii = False, separators = (",", ":"))+ return f"id: {event['seq']}\nevent: {event['type']}\ndata: {encoded}\n\n"+++def _assert_old_bytes(+ frames,+ after = 0,+ settled_run = None,+):+ events = runs_db.list_events("run-1", after, 100_000)+ assert len(frames) == len(events)+ for frame, event in zip(frames, events):+ run = None+ if event["type"] != "chunk":+ # A live frame carries whichever snapshot that batch read; byte-check the encoding of that.+ run = json.loads(frame.split("\ndata: ", 1)[1])["run"]+ if settled_run is not None:+ assert run == settled_run+ assert frame == _old_frame(event, run)+++def _fill(token):+ for payload in PAYLOADS:+ runs_db.append_events("run-1", token, [("chunk", payload)])+ runs_db.append_events("run-1", token, [("chunk", payload) for payload in PAYLOADS])+ runs_db.finish_run(+ "run-1",+ worker_token = token,+ status = "completed",+ finish_reason = "stop",+ pending_events = [("chunk", {"choices": [{"delta": {}, "finish_reason": "stop"}]})],+ )+++def test_replay_frames_match_parse_and_reencode_bytes(running_run):+ _fill(running_run)+ settled = runs_db.get_run("run-1")+ frames = _frames(asyncio.run(_stream()))+ _assert_old_bytes(frames, settled_run = settled)+ assert [int(frame[4 : frame.index("\n")]) for frame in frames] == list(+ range(1, settled["lastEventSeq"] + 1)+ )+ assert frames[-1].startswith(f"id: {settled['lastEventSeq']}\nevent: run.completed\n")++[email protected]("cursor", [1, 2, 7, 14])+def test_replay_from_a_cursor_resumes_with_the_same_bytes(running_run, cursor):+ _fill(running_run)+ full = _frames(asyncio.run(_stream()))+ assert _frames(asyncio.run(_stream(after = cursor))) == full[cursor:]+ assert _frames(asyncio.run(_stream(last_event_id = str(cursor)))) == full[cursor:]+++def test_replay_of_a_fully_read_settled_run_sends_nothing(running_run):+ _fill(running_run)+ last = runs_db.get_run("run-1")["lastEventSeq"]+ assert asyncio.run(_stream(after = last)) == ""+++def test_live_followers_get_identical_old_bytes_and_the_terminal_frame(running_run):+ async def scenario():+ followers = [asyncio.create_task(_stream()) for _ in range(3)]+ await asyncio.sleep(0.05)++ def produce():+ for i, payload in enumerate(PAYLOADS * 3):+ runs_db.append_events("run-1", running_run, [("chunk", payload)])+ if i % 4 == 0:+ time.sleep(0.005)+ runs_db.finish_run(+ "run-1", worker_token = running_run, status = "completed", finish_reason = "stop"+ )++ await asyncio.to_thread(produce)+ return await asyncio.wait_for(asyncio.gather(*followers), 30)++ outputs = [_frames(raw) for raw in asyncio.run(scenario())]+ last = runs_db.get_run("run-1")["lastEventSeq"]+ for frames in outputs:+ _assert_old_bytes(frames)+ assert frames[-1].startswith(f"id: {last}\nevent: run.completed\n")+ assert json.loads(frames[-1].split("\ndata: ", 1)[1])["run"]["status"] == "completed"+ chunks = [[frame for frame in frames if "\nevent: chunk\n" in frame] for frames in outputs]+ assert chunks[0] == chunks[1] == chunks[2]+++def test_cancelled_run_terminal_frames_keep_old_bytes(running_run):+ runs_db.append_events("run-1", running_run, [("chunk", PAYLOADS[1])])+ runs_db.request_cancel("run-1", "alice")+ runs_db.finish_run(+ "run-1",+ worker_token = running_run,+ status = "cancelled",+ finish_reason = "cancelled",+ pending_events = [("chunk", PAYLOADS[2])],+ )+ frames = _frames(asyncio.run(_stream()))+ _assert_old_bytes(frames, settled_run = runs_db.get_run("run-1"))+ assert [frame.split("\n")[1] for frame in frames][-3:] == [+ "event: run.cancelling",+ "event: chunk",+ "event: run.cancelled",+ ]+++def test_an_empty_stored_payload_still_reads_as_an_empty_object(running_run):+ runs_db.append_events("run-1", running_run, [("chunk", {"x": 1})])+ conn = studio_db.get_connection()+ try:+ conn.execute("UPDATE chat_generation_events SET payload_json='' WHERE run_id='run-1'")+ conn.commit()+ finally:+ conn.close()+ runs_db.finish_run("run-1", worker_token = running_run, status = "completed", finish_reason = "stop")+ frames = _frames(asyncio.run(_stream()))+ _assert_old_bytes(frames, settled_run = runs_db.get_run("run-1"))+ assert all(json.loads(frame.split("\ndata: ", 1)[1])["payload"] == {} for frame in frames[:-1])+++CORRUPT_PAYLOADS = [+ '{"choices":[{"delta":{"content":"cut',+ '{"text":"x}',+ "{'single':'quotes'}",+ '{\n "pretty": 1\n}',+ '{"a":1}\r\n',+ "not json",+]+++def test_a_stored_payload_that_is_not_one_line_of_json_keeps_the_old_frame(running_run):+ first = int(runs_db.get_run("run-1")["lastEventSeq"]) + 1+ runs_db.append_events(+ "run-1", running_run, [("chunk", {"i": i}) for i in range(len(CORRUPT_PAYLOADS))]+ )+ conn = studio_db.get_connection()+ try:+ for seq, text in enumerate(CORRUPT_PAYLOADS, start = first):+ conn.execute(+ "UPDATE chat_generation_events SET payload_json=? WHERE run_id='run-1' AND seq=?",+ (text, seq),+ )+ conn.commit()+ finally:+ conn.close()+ runs_db.finish_run("run-1", worker_token = running_run, status = "completed", finish_reason = "stop")++ frames = _frames(asyncio.run(_stream(after = first - 1)))++ # Spliced as is, a bad row would reach the client as a frame it cannot parse (or split it), and+ # the client reconnects at that cursor forever; the old route sent a parse-and-encode.+ _assert_old_bytes(frames, first - 1, runs_db.get_run("run-1"))+ payloads = [json.loads(frame.split("\ndata: ", 1)[1])["payload"] for frame in frames[:-1]]+ assert payloads == [{}, {}, {}, {"pretty": 1}, {"a": 1}, {}]+++def _random_value(rng, depth = 0):+ kind = rng.randrange(8 if depth < 4 else 5)+ if kind == 0:+ return rng.choice([None, True, False])+ if kind == 1:+ return rng.randint(-(2**70), 2**70)+ if kind == 2:+ return rng.choice([rng.uniform(-1e6, 1e6), rng.random() * 10 ** rng.randint(-300, 300)])+ if kind in (3, 4):+ alphabet = 'ab "\\/\n\r\t\x00\x1f\x7fé \U0001f600数'+ return "".join(rng.choice(alphabet) for _ in range(rng.randrange(12)))+ if kind == 5:+ return [_random_value(rng, depth + 1) for _ in range(rng.randrange(4))]+ return {f'k{i}é"\n': _random_value(rng, depth + 1) for i in range(rng.randrange(4))}+++def test_stored_payloads_are_a_fixed_point_of_parse_and_reencode(running_run):+ rng = random.Random(1234)+ payloads = [{"v": _random_value(rng)} for _ in range(400)]+ runs_db.append_events("run-1", running_run, [("chunk", payload) for payload in payloads])+ parsed = runs_db.list_events("run-1", 2, 1000)+ raw = runs_db.list_events("run-1", 2, 1000, raw = True)+ assert [event["seq"] for event in raw] == [event["seq"] for event in parsed]+ assert [event["createdAt"] for event in raw] == [event["createdAt"] for event in parsed]+ for parsed_event, raw_event in zip(parsed, raw):+ assert raw_event["payloadJson"] == run_routes._encode_json(parsed_event["payload"])+ assert "payload" not in raw_event+++def test_streaming_chunks_does_not_parse_stored_payloads(running_run, monkeypatch):+ for start in range(0, 300, 50):+ runs_db.append_events(+ "run-1", running_run, [("chunk", {"delta": f"t{i}"}) for i in range(start, start + 50)]+ )+ runs_db.finish_run("run-1", worker_token = running_run, status = "completed", finish_reason = "stop")+ parsed = []+ original = runs_db._loads+ monkeypatch.setattr(+ runs_db, "_loads", lambda value, fallback: parsed.append(value) or original(value, fallback)+ )++ frames = _frames(asyncio.run(_stream()))++ assert len(frames) == 303+ # Only request_json, for the opening check and the run-event batch snapshot.+ payload_parses = [value for value in parsed if '"delta"' in str(value)]+ assert payload_parses == []+ assert len(parsed) <= 3+++def test_wait_for_events_raw_waits_like_the_parsed_read(running_run):+ cursor = int(runs_db.get_run("run-1")["lastEventSeq"])+ timer = threading.Timer(+ 0.2, lambda: runs_db.append_events("run-1", running_run, [("chunk", {"d": "é"})])+ )+ timer.start()+ try:+ events = runs_db.wait_for_events("run-1", cursor, 10, True)+ finally:+ timer.cancel()+ assert [event["payloadJson"] for event in events] == ['{"d":"é"}']+++def test_sqlite_without_json1_streams_the_old_bytes(running_run, monkeypatch):+ monkeypatch.setattr(runs_db, "_SPLICE_OK_SQL", "json_valid_missing(payload_json)")+ _fill(running_run)+ _assert_old_bytes(_frames(asyncio.run(_stream())), settled_run = runs_db.get_run("run-1"))+ assert runs_db._SPLICE_OK_SQL == "0"diff --git a/studio/backend/tests/test_chat_generation_events_reads.py b/studio/backend/tests/test_chat_generation_events_reads.pyindex d1bbcd7a8f3..e7c38e81542 100644--- a/studio/backend/tests/test_chat_generation_events_reads.py+++ b/studio/backend/tests/test_chat_generation_events_reads.py@@ -64,7 +64,9 @@ def test_chunk_only_batches_do_not_parse_the_request(running_run, monkeypatch):runs_db.finish_run("run-1", worker_token = running_run, status = "completed", finish_reason = "stop")original_list = runs_db.list_eventsmonkeypatch.setattr(- runs_db, "list_events", lambda run_id, after = 0, limit = 1000: original_list(run_id, after, 1)+ runs_db,+ "list_events",+ lambda run_id, after = 0, limit = 1000, **kwargs: original_list(run_id, after, 1, **kwargs),)full_reads = []original_get = runs_db.get_run