Skip to content

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.

Studio chat search: skip metadata_json when building the search index batch (#17249)

Daniel Han804327784052
diff --git a/studio/backend/routes/chat_history.py b/studio/backend/routes/chat_history.py
index 9cfc34230e3..4200f45facb 100644
--- a/studio/backend/routes/chat_history.py
+++ b/studio/backend/routes/chat_history.py
@@ -1843,7 +1843,7 @@ def batch_thread_messages(
"""One round-trip per sidebar/search rebuild instead of N. Unknown thread ids return empty lists."""
by_thread: dict[str, list[ChatMessage]] = {tid: [] for tid in payload.threadIds}
search_view = payload.view == "search"
- for m in list_chat_messages_for_threads(payload.threadIds):
+ for m in list_chat_messages_for_threads(payload.threadIds, include_metadata = not search_view):
tid = m["threadId"]
if tid in by_thread:
by_thread[tid].append(ChatMessage(**(_search_view_message(m) if search_view else m)))
diff --git a/studio/backend/storage/studio_db.py b/studio/backend/storage/studio_db.py
index 55dc1bbf060..e6b4902ca13 100644
--- a/studio/backend/storage/studio_db.py
+++ b/studio/backend/storage/studio_db.py
@@ -5478,9 +5478,17 @@ def count_chat_messages_for_threads(thread_ids: list[str]) -> dict[str, int]:
return counts
-def list_chat_messages_for_threads(thread_ids: list[str]) -> list[dict]:
+def list_chat_messages_for_threads(
+ thread_ids: list[str], *, include_metadata: bool = True
+) -> list[dict]:
+ """include_metadata=False skips reading and decoding metadata_json (the search view drops it)."""
if not thread_ids:
return []
+ columns = (
+ "*"
+ if include_metadata
+ else "id, thread_id, parent_id, role, content_json, attachments_json, created_at"
+ )
unique_thread_ids = list(dict.fromkeys(thread_ids))
messages: list[dict] = []
conn = get_connection()
@@ -5490,7 +5498,7 @@ def list_chat_messages_for_threads(thread_ids: list[str]) -> list[dict]:
placeholders = ",".join("?" for _ in chunk)
rows = conn.execute(
f"""
- SELECT * FROM chat_messages
+ SELECT {columns} FROM chat_messages
WHERE thread_id IN ({placeholders})
ORDER BY created_at ASC, id ASC
""",
diff --git a/studio/backend/tests/test_chat_search_view.py b/studio/backend/tests/test_chat_search_view.py
index 234305ee351..fa4fe8d69c6 100644
--- a/studio/backend/tests/test_chat_search_view.py
+++ b/studio/backend/tests/test_chat_search_view.py
@@ -127,6 +127,28 @@ def test_search_view_keeps_what_the_index_reads_and_drops_the_rest(history):
assert view_bytes < 5_000
+def test_search_view_never_decodes_metadata(history, monkeypatch):
+ """Provider metadata (reasoning ledgers) is large and the search view drops it: skip the decode."""
+ full = _batch()
+ expected = {
+ tid: [
+ chat_history.ChatMessage(**chat_history._search_view_message(m)).model_dump(mode = "json")
+ for m in messages
+ ]
+ for tid, messages in full.items()
+ }
+ decode = studio_db._json_loads
+
+ def guarded(value, fallback):
+ assert value is None or "predicted_n" not in value, "search view decoded metadata_json"
+ return decode(value, fallback)
+
+ monkeypatch.setattr(studio_db, "_json_loads", guarded)
+ assert _batch("search") == expected
+ with pytest.raises(AssertionError, match = "decoded metadata_json"):
+ _batch("full")
+
+
@pytest.mark.parametrize(
"attachments",
[