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.

Let a Deep Research handoff survive a preamble beside the tool call (#10677)

Daniel de Araujo51e634453fd6
diff --git a/studio/backend/storage/research_runs_db.py b/studio/backend/storage/research_runs_db.py
index ae4139345bc..98892a5091e 100644
--- a/studio/backend/storage/research_runs_db.py
+++ b/studio/backend/storage/research_runs_db.py
@@ -174,14 +174,27 @@ def _bind_assistant_locked(
)
# Only bind to an empty placeholder or this run's own message: an untagged reply carries parts
# _update_assistant drops on completion, so binding one silently overwrites an existing answer.
+ parts = _loads(message["content_json"], [])
+ # A preamble beside the deep_research call is not a prior answer, so only text WITHOUT the
+ # handoff refuses the bind. Source parts, which only a finished answer carries, still do.
+ has_research_handoff = any(
+ isinstance(part, dict)
+ and part.get("type") == "tool-call"
+ and part.get("toolName") == "deep_research"
+ for part in parts
+ )
existing_answer = any(
isinstance(part, dict)
and (
- (part.get("type") == "text" and (part.get("text") or "").strip())
+ (
+ part.get("type") == "text"
+ and (part.get("text") or "").strip()
+ and not has_research_handoff
+ )
or part.get("type") == "source"
)
and part.get("researchRunId") is None
- for part in _loads(message["content_json"], [])
+ for part in parts
)
if (
message["thread_id"] != thread_id
diff --git a/studio/backend/tests/test_research_runs_storage.py b/studio/backend/tests/test_research_runs_storage.py
index 738d8012083..ee022a6453d 100644
--- a/studio/backend/tests/test_research_runs_storage.py
+++ b/studio/backend/tests/test_research_runs_storage.py
@@ -2980,6 +2980,65 @@ def test_create_run_rejects_binding_to_populated_reply(research_home):
assert run["assistantMessageId"] == "empty-placeholder"
+def test_create_run_binds_through_a_preamble_beside_the_handoff(research_home):
+ # A thinking model narrates before it calls a tool, so both arrive in one message.
+ studio_db.upsert_chat_message(
+ {
+ "id": "preamble-and-call",
+ "threadId": "thread-1",
+ "parentId": "user-1",
+ "role": "assistant",
+ "content": [
+ {"type": "reasoning", "text": "the user wants research"},
+ {"type": "text", "text": "I'll research that now."},
+ {"type": "tool-call", "toolName": "deep_research", "toolCallId": "c1"},
+ ],
+ "createdAt": 6,
+ }
+ )
+ run = _create(assistant_message_id = "preamble-and-call")
+ assert run["assistantMessageId"] == "preamble-and-call"
+
+
+def test_create_run_still_rejects_an_answer_beside_an_unrelated_tool_call(research_home):
+ studio_db.upsert_chat_message(
+ {
+ "id": "answer-and-other-call",
+ "threadId": "thread-1",
+ "parentId": "user-1",
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "existing answer"},
+ {"type": "tool-call", "toolName": "search_knowledge_base", "toolCallId": "c2"},
+ ],
+ "createdAt": 7,
+ }
+ )
+ with pytest.raises(research_db.ResearchConflictError):
+ _create(assistant_message_id = "answer-and-other-call")
+ assert research_db.get_run("run-1") is None
+
+
+def test_create_run_rejects_a_completed_answer_even_beside_the_handoff(research_home):
+ studio_db.upsert_chat_message(
+ {
+ "id": "sources-and-call",
+ "threadId": "thread-1",
+ "parentId": "user-1",
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "existing answer"},
+ {"type": "source", "sourceType": "url", "url": "https://kept.example"},
+ {"type": "tool-call", "toolName": "deep_research", "toolCallId": "c3"},
+ ],
+ "createdAt": 8,
+ }
+ )
+ with pytest.raises(research_db.ResearchConflictError):
+ _create(assistant_message_id = "sources-and-call")
+ assert research_db.get_run("run-1") is None
+
+
def test_update_assistant_replaces_report_parts_without_duplication(research_home):
from core.research_runs import _update_assistant